Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the craps program of Fig. 5.14 to allow wagering. Package as a function t

ID: 3869657 • Letter: M

Question

Modify the craps program of Fig. 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, run one game of craps. If the player wins, increase bankBalance by wager and print the new bankBalance. If the player loses, decrease bankBalance by 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 messages to create some “chatter” such as, "Oh, you're going for broke, huh?" or "Aw cmon, take a chance!" or "You're up big. Now's the time to cash in your chips!"

Fig05_14

// Fig. 5.14: fig05_14.c
// Simulating the game of craps.
#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)
{
// randomize 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
gameStatus = 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");
}
else { // player lost
puts("Player loses");
}
}

// 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
}

Explanation / Answer

#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 bankBalance=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);

}

// randomize 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

gameStatus = 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

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote