C++ Figure 6.9 #include #include // contains prototypes for functions srand and rand #include // contains prototype for function time using namespace std; unsigned int rollDice(); // rolls dice, calculates and displays sum int main() { // scoped enumeration with constants that represent the game status enum class Status {CONTINUE, WON, LOST}; // all caps in constants // randomize random number generator using current time srand(static_cast(time(0))); unsigned int myPoint{0}; // point if no win or loss on first roll Status gameStatus; // can be CONTINUE, WON or LOST unsigned int sumOfDice{rollDice()}; // first roll of the dice // determine game status and point (if needed) based on first roll switch (sumOfDice) { case 7: // win with 7 on first roll case 11: // win with 11 on first roll gameStatus = Status::WON; break; case 2: // lose with 2 on first roll case 3: // lose with 3 on first roll case 12: // lose with 12 on first roll gameStatus = Status::LOST; break; default: // did not win or lose, so remember point gameStatus = Status::CONTINUE; // game is not over myPoint = sumOfDice; // remember the point cout << "Point is " << myPoint << endl; break; // optional at end of switch } // while game is not complete while (Status::CONTINUE == gameStatus) { // not WON or LOST sumOfDice = rollDice(); // roll dice again // determine game status if (sumOfDice == myPoint) { // win by making point gameStatus = Status::WON; } else { if (sumOfDice == 7) { // lose by rolling 7 before point gameStatus = Status::LOST; } } } // display won or lost message if (Status::WON == gameStatus) { cout << "Player wins" << endl; } else { cout << "Player loses" << endl; } } // roll dice, calculate sum and display results unsigned int rollDice() { int die1{1 + rand() % 6}; // first die roll int die2{1 + rand() % 6}; // second die roll int sum{die1 + die2}; // compute sum of die values // display results of this roll cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl; return sum; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

C++

Figure 6.9

#include <iostream>
#include <cstdlib> // contains prototypes for functions srand and rand
#include <ctime> // contains prototype for function time
using namespace std;

unsigned int rollDice(); // rolls dice, calculates and displays sum

int main() {
// scoped enumeration with constants that represent the game status
enum class Status {CONTINUE, WON, LOST}; // all caps in constants

// randomize random number generator using current time
srand(static_cast<unsigned int>(time(0)));

unsigned int myPoint{0}; // point if no win or loss on first roll
Status gameStatus; // can be CONTINUE, WON or LOST
unsigned int sumOfDice{rollDice()}; // first roll of the dice

// determine game status and point (if needed) based on first roll
switch (sumOfDice) {
case 7: // win with 7 on first roll
case 11: // win with 11 on first roll
gameStatus = Status::WON;
break;
case 2: // lose with 2 on first roll
case 3: // lose with 3 on first roll
case 12: // lose with 12 on first roll
gameStatus = Status::LOST;
break;
default: // did not win or lose, so remember point
gameStatus = Status::CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
cout << "Point is " << myPoint << endl;
break; // optional at end of switch
}

// while game is not complete
while (Status::CONTINUE == gameStatus) { // not WON or LOST
sumOfDice = rollDice(); // roll dice again

// determine game status
if (sumOfDice == myPoint) { // win by making point
gameStatus = Status::WON;
}
else {
if (sumOfDice == 7) { // lose by rolling 7 before point
gameStatus = Status::LOST;
}
}
}

// display won or lost message
if (Status::WON == gameStatus) {
cout << "Player wins" << endl;
}
else {
cout << "Player loses" << endl;
}
}

// roll dice, calculate sum and display results
unsigned int rollDice() {
int die1{1 + rand() % 6}; // first die roll
int die2{1 + rand() % 6}; // second die roll
int sum{die1 + die2}; // compute sum of die values

// display results of this roll
cout << "Player rolled " << die1 << " + " << die2
<< " = " << sum << endl;
return sum;
}

37. 6.47 (Craps Game Modification) Modify the craps program of
Fig. 6.9 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 on 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?", "Aw cmon, take a chance!" or
"You're up big. Now's the time to cash in your chips!".
Transcribed Image Text:37. 6.47 (Craps Game Modification) Modify the craps program of Fig. 6.9 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 on 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?", "Aw cmon, take a chance!" or "You're up big. Now's the time to cash in your chips!".
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education