C programming (Craps Game Modification) Modify the craps program of 5.14 to allo
ID: 3734651 • Letter: C
Question
C programming (Craps Game Modification) Modify the craps program of 5.14 to allow wagering. Package as a function the portion of the program that runs one game of craps. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Use a while loop to check that wager is less than or equal to bankBalance, and if not, prompt the user to reenter wager until a valid wager is entered. After a correct wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase wager, print the new bankBalance, check whether bankBalance has become zero, and if so print the message, "Sorry, You busted!". As the game progresses, print various message to create some "chatter" such as, "Oh, you were going for broke, huh? or "Aw cmon, take a change!" or "You're up big, Now's the time to cash in your chips!"
Explanation / Answer
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h> // contains prototype for function time
// enumeration constants represent game status
enum Status { CONTINUE, WON, LOST };
int rollDice(void); // function prototype
int main(void)
{
int bankBalnce=1000;
printf("Enter a wager ");
int Wager;//Wager
scanf("%d",&Wager); // take a initial wager
while(Wager>bankBalance) // repet wager when it lessthen or equal to bankBalance
{
printf("Aw cmon, take a chance! ReEnter a wager ");
scanf("%d",&Wager);
}
// randoize random number generator using current time
srand(time(NULL));
int myPoint; // player must make this point to win
enum Status gameStatus; // can contain CONTINUE, WON, or LOST
int sum = rollDice(); // first roll of the dice
// determine game status based on sum of dice
switch(sum) {
// win on first roll
case 7: // 7 is a winner
case 11: // 11 is a winner
gameSttus = WON;
break;
// lose on first roll
case 2: // 2 is a loser
case 3: // 3 is a loser
case 12: // 12 is a loser
gameStatus = LOST;
break;
// remember point
default:
gameStatus = CONTINUE; // player should keep rolling
myPoint = sum; // remember the point
printf(Point is %d ", myPoint);
break; // optional
}
// while game not complete
while (CONTINUE == gameStatus) { // player should keep rolling
sum = rollDice(); // roll dice again
// determine game status
if (sum == myPoint) { // win by making point
gameStatus = WON;
}
else {
if (7 == sum) { // lose by rolling 7
gameStatus = LOST;
}
}
}
// display won or lost message
if (WON == gameStatus) { // did player win?
puts("Player wins");
bankBalance+=Wager; // update bankBalance
if(bankBalance==0)
{ puts("Sorry. You busted!");
exit(0);
}
puts("your new bankbalance is ");
printf("%d",bankBalance);
}
else { // player lost
puts("Player loses");
bankBalance-=Wager; // update bankBalance
if(bankBalance==0)
{ puts("Sorry. You busted!");
exit(0);
}
puts("your new bankbalance is ");
printf("%d",bankBalance);
}
}
// roll dice, calculate sum and display results
int rollDice(void)
{
int die1 = 1 + (rand() % 6); // pick random die1 value
int die2 = 1 + (rand() % 6); // pick random die2 value
// display results of this roll
printf("Player rolled %d + %d = %d ", die1, die2, die1 + die2);
return die1 + die2; // return sum of dice
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.