Project Two Solution
Below is a possible solution for the project :
/* MCS 260 Fall 2001 Project two: roulette
Roulette is a gambling game in which a ball is dropped
on to a revolving numbered wheel.
The purpose of the project is to write a program that simulates
this game. For sake of simplicity, we assume that the gambler
places only one bet for every drop of the ball.
The range of values for the ball is 0..64, all integer numbers
between 0 and 64, 0 and 64 included.
Denoting the gambler's bet by b and the value of the ball by v,
we rank the possible rewards in decreasing order as follows :
b == v : 15 tokens (+ token used to place the bet)
b in [v,v+1] : 7
b in [v,v+2] : 3
b in [v,v+4] : 1
The token used to place the bet is returned when the parity
of bet and token are the same and bet and token are in the same
half of the range 0..64 (i.e., b,v in [0,32] or b,v in [33,64]).
In all other cases, the gambler loses the token.
Note that zero is a kind of naughty number. Unless a bet is
placed on zero, the token is lost.
The game ends if the user enters a negative number or if there
are no more tokens available.
At the end of the game, the program prints the balance of available
tokens. There is a one token charge to play the game, so the
balance is one less than the amount of available tokens before the
user entered a negative bet. In case the gambler ran out of tokens,
the balance is consequently -1 and the program reminds the gambler
to pay the fee. With a positive balance, the program invites the
user for sake of profit to play again.
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAXROLL 65
#define HALFMAX 32
int main(void)
{
int tokens; /* number of tokens to gamble */
int bet=0; /* the bet the user makes before the ball */
int ball; /* random number in the range 0..64 */
srand(time(NULL));
printf("Give the number of available tokens : ");
scanf("%d", &tokens);
while ((bet >= 0) && (tokens-- > 0))
{
printf(" place your bet (negative to leave): ");
scanf("%d", &bet);
if (bet >= 0)
{
ball = rand()%MAXROLL;
printf(" outcome is : %d,", ball);
if (ball == bet)
{
tokens += 16;
printf(" gained 15 tokens, balance is %d.\n", tokens);
}
else if ((ball > 0) && (ball <= bet) && (bet <= ball+1))
{
tokens += 8;
printf(" gained 7 tokens, balance is %d.\n", tokens);
}
else if ((ball > 0) && (ball <= bet) && (bet <= ball+2))
{
tokens += 4;
printf(" gained 3 tokens, balance is %d.\n", tokens);
}
else if ((ball > 0) && (ball <= bet) && (bet <= ball+4))
{
tokens += 2;
printf(" gained 1 token, balance is %d.\n", tokens);
}
else if ((ball > 0) && (ball % 2 == bet % 2)
&& ( ((ball >= HALFMAX) && (bet >= HALFMAX))
|| ((ball < HALFMAX) && (bet < HALFMAX)) ) )
printf(" token kept, balance is %d.\n", ++tokens);
else
printf(" token lost, %d left to play.\n", tokens);
}
}
printf("Your ending balance is %d.", tokens);
if (tokens < 0)
printf(" Pay the gambling fee.\n");
else
printf(" Please play again.\n");
return 0;
}