In: Computer Science
use c++
This question is about providing game logic for the game of craps
we developed its shell in class. At the end of the game, you should
ask the user if wants to play another game, if so, make it happen.
Otherwise, quit
**Provide the working output of this game and the picture of the output. **
This is an implementation of the famous 'Game of Chance'
called 'craps'.
It is a dice game. A player rolls 2 dice. Each die has six faces:
1, 2, 3, 4, 5, 6.
Based on the values on the dice, we play the game until a player
wins or loses.
You add the values of the dice's face.
1) If the sum is 7 or 11 on the first roll, the player wins.
2) If the sum is 2, 3, or 12 on the first roll, the player loses (
or the 'house' wins)
3) If the sum is 4, 5, 6, 8, 9, or 10 on the first roll, then that
sum becomes the player's 'point'.
4)To win, the player must continue rolling the dice until he/she
'make the point'.
5)If the player rolls a 7 he/she loses.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
enum class Status { CONTINUE, WON, LOST};
unsigned int rollDiceAdvanced(); // same as above, but used advance
c++ Random Number generation.
// global variable to use in the advanced version
of RNG (Random number Generator)
std::default_random_engine engine{ static_cast<unsigned
int>(time(0)) };
std::uniform_int_distribution<unsigned int> randomInt{ 1, 6
};
int main()
{
Status gameStatus; // can be CONTINUE, WON, or
LOST
unsigned int myPoint{ 0 };
unsigned int sumOfDice = rollDiceAdvanced(); // first roll of dice.
// bellow the game logic: Provide the game logic
return 0;
}
// this is distribution based random number generation in c++
unsigned int rollDiceAdvanced()
{
unsigned int firstRoll{ randomInt(engine) };
unsigned int secondRoll{ randomInt(engine) };
unsigned int sum{ firstRoll + secondRoll };
std::cout << "rollDiceAdvance: " << sum
<< std::endl;
return sum;
}
Solution in c++: (Along with output)
#include <iostream>
/* Writ statement to include file stream header */
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::endl;
/* Write appropriate using statement(s) */
void playCraps();
void reviewStatistics();
int rollDice();
int main()
{
int choice;
// continue game unless user chooses to quit
do {
// offer game options
cout << "Choose an option" << endl
<< "1. Play a game of craps" << endl
<< "2. Review cumulative craps statistics" << endl
<< "3. Quit program" << endl;
cin >> choice;
if ( choice == 1 )
playCraps();
else if ( choice == 2 )
reviewStatistics();
} while ( choice != 3 );
return 0;
} // end main
// review cumulative craps statistics
void reviewStatistics()
{
/* Write a body for reviewStatistics which displays
the total number of wins, losses and die rolls recorded
in craps.dat */
} // end function reviewStatistics
// play game
void playCraps()
{
enum Status { CONTINUE, WON, LOST };
int sum;
int myPoint;
int rollCount = 0;
Status gameStatus;
/* Write statement to create an output file stream */
// seed random number generator and roll dice
srand( time( 0 ) );
sum = rollDice();
rollCount++;
// check game conditions
switch( sum ) {
case 7: //win with 7 on first roll
case 11: // win with 11 on first roll
gameStatus = 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 = LOST;
break;
default: // did not win or lose, so remember point
gameStatus = CONTINUE; // game is not over
myPoint = sum; // remember the point
cout << "Point is " << myPoint << endl;
break; // optional at end of switch
} // end switch
// keep rolling until player matches point or loses
while ( gameStatus == CONTINUE ) //// not WON or LOST
{
sum = rollDice();
rollCount++;
if ( sum == myPoint )
gameStatus = WON;
else
if ( sum == 7 ) // lose by rolling 7 before point
gameStatus = LOST;
} // end while
// display status message and write results to file
if ( gameStatus == WON ) {
cout << "Player wins\n" << endl;
/* Write player WIN status and the total number of die
rolls to a file */
} // end if
else {
cout << "Player loses\n" << endl;
/* Write player LOSE status and the total number of die
rolls to a file */
} // end else
} // end function playCraps
// dice rolling function
int rollDice()
{
int die1;
int die2;
int workSum;
// roll two dice
die1 = 1 + rand() % 6;// first die roll
die2 = 1 + rand() % 6; //// second die roll
// total and print results
workSum = die1 + die2; // compute sum of die values
cout << "Player rolled " << die1 << " + " << die2 << " = " << workSum << endl;
return workSum;
} // end function rollDice
Output: