Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(7.18) (Craps Game Modification) Modify the program of Fig. 6.9 to play 1000 games of craps. The program should keep track of the statistics and

(7.18) (Craps Game Modification)

Modify the program of Fig. 6.9 to play 1000 games of craps. The program should keep track of the statistics and answer the following questions:

a) How many games are won on the 1st roll, 2nd roll, , 20th roll, and after the 20th roll?

b) How many games are lost on the 1st roll, 2nd roll, , 20th roll, and after the 20th roll?

c) What are the chances of winning at craps? [Note: You should discover that craps is one

of the fairest casino games. What do you suppose this means?]

d) Whats the average length of a game of craps?

e) Do the chances of winning improve with the length of the game?

Below I have provided the initial program from Fig 6.9 that should be added onto! Please help!

// Fig. 6.9: fig06_09.cpp // Craps simulation.

#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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions