Use CPP
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.
craps.cpp
/***
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;
}
Please provide the screenshot of final result too.
Thank you
SOLUTION
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
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))); //randomizes rand() function.
int rollDice;
char repeat = 'y';
// bellow the game logic: Student going to implement this.
while (repeat == 'y' || repeat == 'Y')
{
rollDice = rollDiceAdvanced();
if (rollDice == 7 || rollDice == 11)
{
cout << ". Winner !" << endl ;
}
else if (rollDice == 2 || rollDice == 3 || rollDice == 12)
{
cout << ". You lose!" << endl;
}
else if (rollDice == 4 || rollDice == 5 ||rollDice == 6 ||rollDice == 8 || rollDice == 9 || rollDice == 10)
{
int sum2;
while (1)
{
sum2 = rollDiceAdvanced();
if( sum2 == rollDice )
{
cout << ". Winner !" << endl;
break;
}
else if( sum2 == 7 )
{
cout << ". You Lose!" << endl;
break;
}
}
}
cout <<"Another game? Y(es) or N(o)" << endl;
cin >> repeat;
while (repeat == 'n' || repeat == 'N')
{
cout << "Thank you for playing!"<< endl;
return 0;
}
}
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;
}
RESULT
Get Answers For Free
Most questions answered within 1 hours.