/* Chris Stewart- Muniz COP3223-002 10/17/07 Program 3 */ #include #include #include int craps(); int pairofdice(); int arupsdice(); int buychips(int *cash); int sellchips(int numchips); void statusreport(int totalchips, int totalcash); int main() { int menuoption = 0, numchips, totalchips = 0, cash, totalcash = 1000, sellvalue, chipsbet, *cash_ptr = &cash; while (menuoption != 6) { //Buy chips if (menuoption == 1) { printf("How much cash do you want to spend for chips?\n"); scanf("%d", &cash); if (cash > totalcash) printf("Sorry, you do not have that much money. No chips bought.\n"); else { totalcash -= cash; totalchips += buychips(cash_ptr); totalcash += cash; } } //Sell chips if (menuoption == 2) { printf("How many chips would you like to sell?\n"); scanf("%d", &numchips); if (numchips > totalchips) printf("Sorry, you do not have that many chips. No chips sold.\n"); else { totalchips -= numchips; sellvalue = sellchips(numchips); totalcash += sellvalue; } } //Play Craps if (menuoption == 3) { printf("How many chips would you like to bet?\n"); scanf("%d", &chipsbet); if (chipsbet > totalchips || chipsbet < 1) printf("Sorry, you do not have that many chips to bet. No game played.\n"); else { int wincraps = craps(); if (wincraps == 1) totalchips += chipsbet; else totalchips -= chipsbet; } } //Play Arup's Game of Dice if (menuoption == 4) { printf("How many chips would you like to bet?\n"); scanf("%d", &chipsbet); if (chipsbet > totalchips || chipsbet < 1) printf("Sorry, you do not have that many chips to bet. No game played.\n"); else { int winarup = arupsdice(); if (winarup == 1) totalchips += chipsbet; else totalchips -= chipsbet; } } //Status Report if (menuoption == 5) { statusreport(totalchips, totalcash); } else { printf("1) Buy chips\n"); printf("2) Sell chips\n"); printf("3) Play Craps\n"); printf("4) Play Arup's Game of Dice\n"); printf("5) Status Report\n"); printf("6) Quit\n"); } scanf("%d", &menuoption); } } int craps() { int roll = pairofdice(), k, newroll = 0; if (roll == 7 || roll == 11) { printf("You win!\n"); return 1; } if (roll == 2 || roll == 3 || roll == 12) { printf("Sorry, you have lost.\n"); return 0; } else { k = roll; while (newroll != k && newroll != 7) { newroll = pairofdice(); } if (newroll == k) { printf("You win!\n"); return 1; } if (newroll == 7) { printf("Sorry, you have lost.\n"); return 0; } } } int pairofdice() { int dicesum; char nextroll = 'a'; srand(time(NULL)); while (nextroll != 'r') { printf("Press 'r' and hit enter to roll.\n"); fflush(stdin); scanf("%s", &nextroll); } dicesum = (rand() % 6 + 1) + (rand() % 6 + 1); printf("You rolled %d.\n", dicesum); return dicesum; } int arupsdice() { int roll = pairofdice(), k; if (roll == 11 || roll == 12) { printf("You win!\n"); return 1; } if (roll == 2) { printf("Sorry, you have lost.\n"); return 0; } else { k = roll; int newroll = pairofdice(); if (newroll > k) { printf("You win!\n"); return 1; } else { printf("Sorry, you have lost.\n"); return 0; } } } int buychips(int *cash) { int chips = (*cash)/11; int change = (*cash) % 11; *cash = change; return chips; } int sellchips(int numchips) { return numchips*10; } void statusreport(int totalchips, int totalcash) { printf("You currently have $%d left and %d chips.\n", totalcash, totalchips); }