Hi everyone! I have to do a program for my language C course in university but I have some problems ...
I have to do a program on the game of craps. I am supposed to have a user roll the dice and if they roll a 7 or an 11, they win. If they roll a 2, 3, or 12, they lose. If the 1st roll is not a win or a loss, the player's first roll becomes the 'point' value. The player then rolls again and again until either a 7 is rolled (rolling player loses) or the 'point' value is rolled a second time (rolling player wins). Also the player have initially 500 $ and he has to bet before each game until he don’t have any money.
The program works well but the professor wants to do the program with a switch instruction (with Case 7 and 11, Case 2,3,12 and default) but I did it with an if instruction and I don’t know how to change it. Can someone please help me?? Thanks
Here is my program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_bet(int max);
int roll_dice(void);
int main(void)
{
int cash = 500; // The player starts with 500$
printf("Welcome to Crap Craps\n");
srand( time(NULL));
do
{
int bet = get_bet(cash);
int point = roll_dice();
if( !bet) break;
printf("you rolled %d\n", point);
if( point == 7 || point == 11) // The player win with 7 or 11
{ printf("You Win!\n");
cash += bet; }
else if( point == 2 || point == 3 || point == 12) // The player loose with 2, 3, or 12
{ printf("You Loose!\n");
cash -= bet; }
else {
for(;;)
{ int roll = roll_dice(); // If you get 7 other than the firt round you loose
printf("you rolled %d\n", roll);
if( roll == 7)
{ printf("Craps! you loose.\n");
cash -= bet;
break; }
else if( roll == point) // If you get your point, you win
{ printf("You Win!\n");
cash += bet;
break; } } } }
while(cash != 0); // while you have cash
printf("thanks for playing you walk away with $%d\n", cash);
system("pause");}
int get_bet(int max)
{ int bet;
printf("0 to quit, max bet is %d\nenter bet:", max);
do { scanf("%d", &bet);
}
while( bet < 0 || bet > max);
fflush( stdin); return bet;
}
int roll_dice(void)
{ return (rand() % 6) + (rand() % 6) + 2;}