/* Kevin Bernfeld COP3223 Section 2 10/18/2007 This is a program to simulate a casino. Games: Craps, Arup's game of dice Meta: Buy chips, sell chips, cash out TODO: pair_of_dice() -- Random numbers aren't necessarily very random, but it works. craps() -- Should work. Yay fflush arups_dice() -- Works nicely. buy_chips() -- DONE. sell_chips() -- Works nicely. status_report() -- Yay for single line functions. (Done) main() -- Should be complete. */ #include #include #include /* Functions */ int pair_of_dice(); int craps(); int arups_dice(); int buy_chips(int *cash); int sell_chips(int num_chips); void status_report(int num_chips, int cash); /* Global variables */ static int chips_on_hand = 0; static int cash_on_hand = 1000; /** * Main. Takes care of menu function. */ int main() { extern cash_on_hand, chips_on_hand; int choice = 0; //Menu choice. //int num_chips = 0; //Number of chips currently held //int cash = 1000; //Cash on hand while (choice != 6) { printf("Welcome to the casino! Here are your choices: \n"); 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) Cash out (quit)\n"); printf("Enter your choice: "); scanf("%d", &choice); /* Menu Choices */ if (choice == 1) { int go_on = 0; while (go_on == 0) { printf("How much money do you want to spend on chips? They cost $11. "); int want_to_spend = 0; scanf("%d", &want_to_spend); if (want_to_spend > cash_on_hand) { printf("Sorry, you can't spend that much.\n"); }//End if else { int *spending; spending = &want_to_spend; buy_chips(spending); go_on = 1; }//End else }//End while }//End if (menu choice 1) if (choice == 2) { int go_on = 0; while (go_on == 0) { printf("How many chips do you want to sell? You get $10 for each chip. "); int want_to_sell = 0; scanf("%d", &want_to_sell); if (want_to_sell <= 0) { printf("You need to actually sell some chips.\n"); }//End if else if (want_to_sell >= chips_on_hand) { printf("You're trying to sell too many chps.\n"); }//End elseif else { sell_chips(want_to_sell); }//End else }//End while }//End if if (choice == 3) { craps(); }//End if (menu choice 3) if (choice == 4) { arups_dice(); }//End if (menu choice 4) if (choice == 5) { status_report(chips_on_hand, cash_on_hand); }//End if (menu choice 5) /* On exit, print this. */ if (choice == 6) { sell_chips(chips_on_hand); printf("You cashed out with $%d. Goodbye!\n", cash_on_hand); } //End if } //End While system("pause"); return 0; } //End Main /* Meta! */ /** * *cash mod 11 gives the leftover cash. * Subtract that from *cash to give a number divisible by 11 * Divide *cash by 11 to get the number of chips obtained. * Modify chips_on_hand, cash_on_hand and return chips_obtained * Since chips_on_hand and cash_on_hand are global, return value shouldn't be used. */ int buy_chips(int *cash) { /* Pull global variables */ extern cash_on_hand, chips_on_hand; int returned = *cash % 11; *cash -= returned; int chips_obtained = (*cash / 11); chips_on_hand += chips_obtained; cash_on_hand -= *cash; return chips_obtained; }//End buy_chips() /** * Subtracts the number of chips returned from the number held * Adds the cash gained to the cash already had. * Returns the cash gained. */ int sell_chips(num_chips) { extern cash_on_hand, chips_on_hand; chips_on_hand -= num_chips; cash_on_hand += (num_chips * 10); return (num_chips * 10); }//End sell_chips() /** * Prints chips_on_hand and cash_on_hand to STDOUT */ void status_report(int num_chips, int cash) { printf("You currently have $%d and %d chips.\n", cash, num_chips); }//End status_report() /* Games! */ /** * Returns the sum of two random dice rolls. */ int pair_of_dice() { int dice1 = 0; int dice2 = 0; srand(time(NULL)); dice1 = ((rand() % 6) + 1); //printf("Dice1: %d\n", dice1); dice2 = ((rand() % 6) + 1); //printf("Dice2: %d\n", dice2); return (dice1 + dice2); } //End pair_of_dice() /** * Plays a game of craps. For reference, craps is played as follows: * Roll a pair of fair six sided dice * If you roll a 7 or 11, you win! * If you roll a 2, 3, or 12, you lose =( * Otherwise, record what you've rolled. Let this sum be k, otherwise known as your point. * If you rolled a point, continue rolling the pair of dice until you roll your point or a sum of 7 on the two dice. * If your point comes up first, you win =D * If you roll a 7, you lose =( * * Exceptions handled: * Not enough chips * Not enough chips bet */ int craps() { extern int chips_on_hand; //Need the current number of chips for later use. int keep_going = 0; //Boolean test value. Checks to see if conditions are met. /* Begin */ printf("Welcome to craps! How many chips would you like to bet? "); int chips_bet = 0; /* Error handling. Makes sure enough chips are bet, and that there are enough chips to bet. */ while (keep_going == 0) { scanf("%d", &chips_bet); //This has to be here, or else you end up in an infinite loop. if (chips_bet > chips_on_hand) { printf("Sorry, you don't have enough chips. Try again.\n"); return 0; } else if (chips_bet == 0) { printf("Sorry, you need to bet chips, or else you can't play. Try again.\n"); return 0; } else { keep_going = 1; } }//End while /* First roll. */ fflush(stdin); //flush the keyboard buffer or else it won't pause for getchar() char entry = 'q'; //I don't plan to do anything with this. Do they HAVE to enter r? printf("Press 'r' and hit enter for your first roll! "); entry = getchar(); /* Win/loss check */ int roll = pair_of_dice(); printf("You rolled a %d.\n", roll); if (roll == 7 || roll == 11) { printf("You win!\n"); chips_on_hand += chips_bet; return 1; } else if (roll == 2 || roll == 3 || roll == 12) { printf("You lose.\n"); chips_on_hand -= chips_bet; return 0; } else { int finished = 0; int win_loss = 0; int point = roll; //I'd rather use point. It's more descriptive. /* Next rolls. */ while (finished == 0) { fflush(stdin); //Again, needs to be here so it will pause. printf("Press 'r' and hit enter for your next roll. "); entry = getchar(); roll = pair_of_dice(); printf("You rolled a %d.\n", roll); /* Win/loss check */ if (roll == point) { printf("You win!\n"); chips_on_hand += chips_bet; return 1; } else if (roll == 7) { printf("You lose.\n"); chips_on_hand -= chips_bet; return 0; } } //End while } //End else } //End craps() /** * 1) Roll a pair of dice. * 2) If you roll a sum of 11 or 12, you win. * 3) If you roll a sum of 2, you lose. * 4) Otherwise, record what you've rolled. Let this sum be k; also known as your point. * 5) Roll one more time. If this roll exceeds your point(k), you win! * 6) If this roll is the same as your point(k) or lower, you lose. */ int arups_dice() { extern int chips_on_hand; //Need the current number of chips for later use. int keep_going = 0; //Boolean test value. Checks to see if conditions are met. /* Begin */ printf("Welcome to Arup's Game of Dice! How many chips would you like to bet? "); int chips_bet = 0; /* Error handling. Makes sure enough chips are bet, and that there are enough chips to bet. */ while (keep_going == 0) { scanf("%d", &chips_bet); //This has to be here, or else you end up in an infinite loop. if (chips_bet > chips_on_hand) { printf("Sorry, you don't have enough chips. Try again.\n"); return 0; } else if (chips_bet == 0) { printf("Sorry, you need to bet chips, or else you can't play. Try again.\n"); return 0; } else { keep_going = 1; } }//End while fflush(stdin); char entry; printf("Press 'r' and then enter for your first roll.\n"); entry = getchar(); /* Win/loss check */ int roll = pair_of_dice(); printf("You rolled a %d.\n", roll); if (roll == 2) { printf("Snake eyes. You lose.\n"); chips_on_hand -= chips_bet; return 0; } else if (roll == 11 || roll == 12) { printf("You win!\n"); chips_on_hand += chips_bet; return 1; } else { int point = roll; while (1) { printf("Press 'r' and then enter for your next roll.\n"); fflush(stdin); entry = getchar(); /* Win/loss check */ roll = pair_of_dice(); printf("You rolled a %d.\n", roll); if (roll > point) { printf("You win!\n"); chips_on_hand += chips_bet; return 1; } else if (roll <= point) { printf("You lose.\n"); chips_on_hand -= chips_bet; return 0; }//End elseif }//End while }//End else }//End arups_dice()