Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)
/*5.47 (Craps Game Modification) Modify the craps program of Fig. 5.10 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 if 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!"*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
enum Status { CONTINUE, WON, LOST };
enum Status craps(void);
int main(void)
{
enum Status game;
char yorn;
int bankBalance = 1000, wager;
printf("I give you 1000$. Have fun!\n");
do {
do {
printf("Enter a wager: ");
scanf("%d", &wager);
bankBalance - wager < 0 ? printf("Sorry, it is off limits!\n\n") : 0;
} while (bankBalance - wager < 0);
game = craps();
if (game == WON)
printf("New Bank Balance: %d$\n\n", bankBalance += wager);
else {
printf("New Bank Balance: %d$\n\n", bankBalance -= wager);
if (bankBalance <= 0) {
printf("**** SORRY! YOU ARE BUSTED! ****\n");
break;
}
}
printf("Do you want to play more? (y or n)\n");
yorn = getchar();
yorn = getchar();
} while (yorn != 'n');
return 0;
}
enum Status craps(void){
int die1, die2, workSum, sum, myPoint;
enum Status gameStatus;
srand(time(NULL));
die1 = 1 + (rand() % 6);
die2 = 1 + (rand() % 6);
sum = die1 + die2;
printf("Player rolled %d + %d = %d\n", die1, die2, sum);
switch (sum) {
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
printf("Point is %d\n", myPoint);
break;
}
while (gameStatus == CONTINUE) {
die1 = 1 + (rand() % 6);
die2 = 1 + (rand() % 6);
sum = die1 + die2;
printf("Player rolled %d + %d = %d\n", die1, die2, sum);
if (sum == myPoint)
gameStatus = WON;
else if (sum == 7)
gameStatus = LOST;
}
if (gameStatus == WON) {
printf("Player wins\n\n");
return WON;
}
else {
printf("Player loses\n\n");
return LOST;
}
return 0;
}
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 if 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!"*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
enum Status { CONTINUE, WON, LOST };
enum Status craps(void);
int main(void)
{
enum Status game;
char yorn;
int bankBalance = 1000, wager;
printf("I give you 1000$. Have fun!\n");
do {
do {
printf("Enter a wager: ");
scanf("%d", &wager);
bankBalance - wager < 0 ? printf("Sorry, it is off limits!\n\n") : 0;
} while (bankBalance - wager < 0);
game = craps();
if (game == WON)
printf("New Bank Balance: %d$\n\n", bankBalance += wager);
else {
printf("New Bank Balance: %d$\n\n", bankBalance -= wager);
if (bankBalance <= 0) {
printf("**** SORRY! YOU ARE BUSTED! ****\n");
break;
}
}
printf("Do you want to play more? (y or n)\n");
yorn = getchar();
yorn = getchar();
} while (yorn != 'n');
return 0;
}
enum Status craps(void){
int die1, die2, workSum, sum, myPoint;
enum Status gameStatus;
srand(time(NULL));
die1 = 1 + (rand() % 6);
die2 = 1 + (rand() % 6);
sum = die1 + die2;
printf("Player rolled %d + %d = %d\n", die1, die2, sum);
switch (sum) {
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
printf("Point is %d\n", myPoint);
break;
}
while (gameStatus == CONTINUE) {
die1 = 1 + (rand() % 6);
die2 = 1 + (rand() % 6);
sum = die1 + die2;
printf("Player rolled %d + %d = %d\n", die1, die2, sum);
if (sum == myPoint)
gameStatus = WON;
else if (sum == 7)
gameStatus = LOST;
}
if (gameStatus == WON) {
printf("Player wins\n\n");
return WON;
}
else {
printf("Player loses\n\n");
return LOST;
}
return 0;
}
Let me know in the comments if this page helped you somehow.
It is nice to know if my work helped someone, thanks :)
Comments
Post a Comment