In: Computer Science
2) This question is about providing game logic for the game of
craps we developed its shell in class. THe cpp of the class is
attached to this project. At the end of the game,
you should ask user if wants to play another game, if so, make it
happen. Otherwise quit.
Here is the attached cpp file for que2:-
/***
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 sixe faces:
1, 2, 3, 4, 5, 6.
Based on the values on the dice we play the game until a player
wins or loose.
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, 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 rollDice(); // forward declaration of rollDice()
function. It rolls 2 dice, displays the sum and return it.
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 };
// either use the base rand() seeding
srand(static_cast<unsigned int>(time(0)));
//dandomizes rand() function.
// but here a sample of using the random number
generation: // just testing:
for (int i = 0; i < 20; ++i)
{
rollDice();
rollDiceAdvanced();
}
unsigned int sumOfDice = rollDiceAdvanced(); // first roll of dice.
// bellow the game logic: Student going to implement this.
return 0;
}
// here is the simple version of rollDice(); throws 2 dice, each
having faces 1 to 6, and return the sum.
unsigned int rollDice()
{
unsigned int firstRoll{ 1 + static_cast<unsigned
int>(rand()) % 6 }; // could be 0, 1, 2, 3, 4, 5, because 8 % 6
== 2, 7 %6 1, 9%6 == 3, 17%6 == 5, 18%6 ==0
unsigned int secondRoll{ 1 + static_cast<unsigned
int>(rand()) % 6 };
unsigned int sum{ firstRoll + secondRoll };
std::cout << "rollDice: " << sum <<
std::endl;
return sum;
}
// 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;
}
Thanks for help
Cpp code (code change highlighted in bold):
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <cstring>
using namespace std;
enum class Status { CONTINUE, WON, LOST};
unsigned int rollDice(); // forward declaration of rollDice()
function. It rolls 2 dice, displays the sum and return it.
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 };
// either use the base rand() seeding
srand(static_cast<unsigned int>(time(0))); //dandomizes
rand() function.
// but here a sample of using the random number generation: //
just testing:
// for (int i = 0; i < 20; ++i)
// {
// rollDice();
// rollDiceAdvanced();
// }
unsigned int sumOfDice = rollDiceAdvanced(); // first roll of dice.
// bellow the game logic: Student going to implement
this.
// continue playing the game until player says yes to replay
// each execution of of code inside while loop simulates one
gameplay
while(true){
unsigned int point;
// case 1
if((sumOfDice==7) or (sumOfDice==11)){
cout<<"You win!"<<endl;
gameStatus=Status::WON;
}
// case 2
else if((sumOfDice==2) or (sumOfDice==3) or (sumOfDice==3)){
cout<<"You lose!"<<endl;
gameStatus=Status::LOST;
}
// case 3
else if((sumOfDice>=4) and (sumOfDice<=10) and
(sumOfDice!=7)){
// assign point to sumOfDice
point=sumOfDice;
cout<<"Your point is : "<<point<<endl;
gameStatus=Status::CONTINUE;
}
// if case 3 happened on first roll, then continue
rolling
while(gameStatus==Status::CONTINUE){
// roll again
sumOfDice = rollDiceAdvanced();
// case 4
if(sumOfDice==point){
cout<<"You made the point!"<<endl;
gameStatus=Status::WON;
}
// case 5
else if(sumOfDice==7){
cout<<"You lose!"<<endl;
gameStatus=Status::LOST;
}
// else contimue rolling
}
// ask player if he/she want to replay
char replay[10];
cout<<"Do you want to replay? (type 'yes' or
'no')"<<endl;
// take player input
cin>>replay;
// check if he/she prompts yes
// if yes then continue playing the game
if(strcmp(replay, "yes") == 0){
// this is first roll of next game
sumOfDice = rollDiceAdvanced();
}
// else exit from while loop
else break;
}
return 0;
}
// here is the simple version of rollDice(); throws 2 dice, each
having faces 1 to 6, and return the sum.
unsigned int rollDice()
{
unsigned int firstRoll{ 1 + static_cast<unsigned int>(rand())
% 6 }; // could be 0, 1, 2, 3, 4, 5, because 8 % 6 == 2, 7 %6 1,
9%6 == 3, 17%6 == 5, 18%6 ==0
unsigned int secondRoll{ 1 + static_cast<unsigned
int>(rand()) % 6 };
unsigned int sum{ firstRoll + secondRoll };
std::cout << "rollDice: " << sum <<
std::endl;
return sum;
}
// 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;
}
Sample execution: