The game of CRAPS: The game uses two(2) six(6) sided die numbered 1 – 6 each. Th
ID: 3674374 • Letter: T
Question
The game of CRAPS:
The game uses two(2) six(6) sided die numbered 1 – 6 each. The player places their bet. The player wins if a 7 or 11 is rolled; the player loses if a 2, 3 0r 12 is rolled. A point is set if the roll is 4, 5, 6, 8, 9 or 10. The object of the game then is to roll the “point” prior to rolling another 7. The player wins if the point is made; loses if a 7 is rolled before the point is made. The game starts over after either a win or a loss.
C++: Write a program that simulates the game of CRAPS. Create a DIE class . Create two objects of this type to represent the two die used in the game. Overload the ‘+’ operator so that the dice can be added together to obtain the rolled value. The output from each roll should indicate if it is the 1st roll and whether the player wins, loses or sets a point. If a point is set, the subsequent rolls should indicate if that point is made or if a 7 is rolled causing the player to lose. Use the rand function from chapter 3 to simulate the values of each die then add them together to get the total value between 2 and 12; each die can only have a value between 1 and 6.
Allow the game to continue (in a loop) until the player decides to quit the game.
Create a problem description, in your own words, an algorithm, the source code and multiple screen shots showing the progress of the game
Explanation / Answer
#include <iostream>
#include <cstdlib> // contains prototypes for functions srand and rand
#include <ctime> // contains prototype for function time
using namespace std;
// Function Prototype rollsdice, calculates and displays sum
int rollDice();
int main() {
// enumeration with constants that represent the game status
enum Status { CONTINUE, WON, LOST }; // all caps in constants
int myPoint; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
// randomize random number generator using current time
srand( time( 0 ) );
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 = WON;
cout << "Player Rolled: " << sumOfDice << " Hence Wins." << endl;
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 = LOST;
cout << "Player Rolled: " << sumOfDice << " Hence Lost." << endl;
break;
default: // did not win or lose, so remember point
gameStatus = CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
cout << "Player Rolled: " << sumOfDice << " Hence Continue."
<< endl;
cout << "Point To Reproduce to Win is: " << myPoint << endl;
break; // optional at end of switch
} // end switch
// while game is not complete not won not lost
while ( gameStatus == CONTINUE ) {
sumOfDice = rollDice(); // roll dice again
// determine game status
// win by making point
if ( sumOfDice == myPoint ) {
gameStatus = WON;
cout << "Point Reproduced again. Hence Won" << endl;
break; // breaking the loop
}
// lose by rolling 7 before point
if ( sumOfDice == 7 ) {
gameStatus = LOST;
cout << "Lost by Rolling 7" << endl;
break; // breaking the loop
}
}//end while
// display won or lost message
if( gameStatus == WON )
cout << "Player wins" << endl;
else cout << "Player Losses" << endl;
} // end main
/*
* Function Body - rollDice
* This function calculate sum and display results
* Input Param are: -NA-
* return: the sum of 2 dice
*/
int rollDice() {
// pick random die values
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; // end function rollDice
} // end function rollDice
Sample output
Player rolled 3+3=6
Player Rolled: 6 Hence Continue.
Point To Reproduce to Win is: 6
Player rolled 4+1=5
Player rolled 3+6=9
Player rolled 6+6=12
Player rolled 1+1=2
Player rolled 5+4=9
Player rolled 5+5=10
Player rolled 2+1=3
Player rolled 1+4=5
Player rolled 1+5=6
Point Reproduced again. Hence Won
Player wins
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.