Back to DFS's C Page


OAC Problem Set VI

A Roulette Simulation Using Functions
Due: End of Class on 30 Nov 99

You are to work with your partner from the previous problem set. You will find it beneficial to divide up the work, one person working on one subset of functions, the other on another subset. These subsets may overlap or be mutually exclusive. On or before the due date, you are to turn in properly documented source code on disk.

The Problem

In this second assignment, your task is to create a simulation program that plays a simplified version of roulette. In the version of roulette played in U.S. casinos, the wheel has 38 slots: a set numbered 1 through 36, and two slots labeled 0 and 00. The odd-numbered slots are black and the even slots (other than 0 and 00) are red. The (simplified) rules of the game are as follows:

Your simulation should give the player an initial stake of $100. It should allow the player to place any type of bet he can afford, then simulate the spin of the roulette wheel. The player should be informed whether they have won or lost, and the player's stake should be updated accordingly. As long as the player has money with which to wager the game may continue. However, after each bet the player should be given the option to terminate the game.

Functions Your Program Requires

Your program must use the functions described below:

int obtain_wager(int balance);

Use the obtain_wager() function to obtain the player's wager. It has a single parameter, balance, which corresponds to the amount of money the player has available for wagering. The function should return the player's wager as an integer. If a player tries to wager more than the current balance, the player should be prompted to re-enter the wager.

int place_bet(int *number, int *color, int *range);

Use the place_bet() function to determine the "type" of bet the player wishes to make. There are three possibilities. A player may make a bet on a specific number, color, or range of numbers. The function should prompt the user as follows:

Place bet on specific number (enter 1)
Place bet on specific color  (enter 2)
Place bet on specific range  (enter 3)
Choice:

It should then read the player's choice. The type of bet (1-3) should be returned to the calling routine upon exit from the function.

Enter number (1-36, 37 for 0, and 38 for 00):

Use slot 37 on the wheel to represent 0 and slot 38 to represent 00. The value entered (1-38) should be passed back to the calling program using pass-by-reference and the variable number.

Enter color (0 for red, 1 for black):

The value entered for color (0-1) should be passed back to the calling program using pass-by-reference and the variable color.

Range  1-12 (enter 1)
Range 13-24 (enter 2)
Range 25-36 (enter 3)
Choice:

The value entered for range (1-3) should be passed back to the calling program using pass-by-reference and the variable range.

int spin_wheel();

Use the spin_wheel function to simulate the spinning of the roulette wheel. The function should return an integer in the range of 1-38. Note: The function should output which slot the hypothetical ball came to rest in. Report the output as follows:

The ball came to rest in slot x,
where x is a number in the range 1-36.

If the ball comes to rest in slot 37, output:

The ball came to rest in 0.

Finally, if the ball comes to rest in slot 38, output

The ball came to rest in 00.
void outcome(int bet_type, int slot, int color, int range, 
       int number, int wager, int *balance);

Use the outcome() function to determine whether the player won or lost and to update the player's balance accordingly. The function's parameters are defined as follows:

Either 1, 2, or 3 indicating the type of bet the user chose to make. (Return value from function place_bet().)

A number in the range 1 to 38 indicating the slot that the ball finally came to rest in. (Return value from function spin_wheel().)

Either 0 (for red) or 1 (for black). (Variable assigned a value inside the function place_bet().)

Either 1 (for range 1-12), 2 (for range 13-24), or 3 (for range 25-36). (Variable assigned a value inside the function place_bet().)

A number in the range 1 to 38. (Variable assigned a value inside function place_bet().)

void GetValidInteger( int *newinteger, 
            int low, int high, char *str);

Cannibalize the GetPositiveInteger() function in Functions III to create this function. It should be called wherever the user is required to provide input.

After determining whether the player won or lost, you should report the outcome in the following ways.

Way to go! You won real big!

or

Sorry! You lose!

Way to go! Player wins on RED.
or
Way to go! Player wins on BLACK.

or

Sorry! Player loses on RED.

or

Sorry! Player loses on BLACK.
Way to go! Player wins on Range 1-12.

or

Way to go! Player wins on Range 13-24.

or

Way to go! Player wins on Range 25-36.

or

Sorry! Player loses on Range 1-12.

or

Sorry! Player loses on Range 13-24.

or

Sorry! Player loses on Range 25-36.

Before exiting the function, remember to update the player's balance accordingly. Keep in mind that each bet has a different pay-off.

Finally, use the following main() function.

#define   ZERO         37   /*  0 represents slot 37 */
#define   DOUBLE_ZERO  38   /* 00 represents slot 38 */
#define   FALSE        0   
#define   TRUE         1

void main()
{
   int balance=100;  /* Amount of money player starts with */
   int wager;        /* Amount player chooses to wager */ 
   int quit;         /* Flag used to terminate program */
   int slot;         /* Slot hypothetical ball ends up in */
   int number;       /* Number player bets on */
   int color;        /* Color player bets on */
   int range;        /* Range player bets on */
   int type_of_bet;  /* Type of bet player is making */

   /* Start simulating game. Do not seed random-number generator. */

   do
   {
      wager = obtain_wager(balance);      

      type_of_bet = place_bet(&number, &color, &range);   

      slot = spin_wheel();   /* simulate spin of roulette wheel */

      outcome(type_of_bet, slot, color, range, number, wager, &balance);

      printf("Balance: $%d\n", balance);

      /* Check to see if player wants to continue. If yes,
       * game can only continue if player has money. */

      if( balance > 0)
		{
         printf("Play again? (0=yes, 1=no): ");
         GetValidInteger( &quit, 0, 1, "(0=yes, 1=no)");
      }
      else quit = TRUE;

   } while( (balance > 0) && (quit == FALSE) );
}