Just having issues with my Craps game. I have the game working but how would you go about having the game ask you if you'd like to play again. From what we were told in class we are supposed to put a Die class into the program. This was the example we were shown.
int main()
{
//seed the random number generator
//Note: this must be done before creating any Die class objects
srand(57);
//Create a Die class object and display the side that is currently up
Die die1;
cout << "The initial side is " << die1.getSide();
//Roll the die 10 times to test the roll and getValue methods
for( int cnt = 1; cnt <= 10; cnt++ )
{
die1.roll();
cout << endl << endl << "Roll " << cnt << ":" << endl
<< "You rolled a " << die1.getSide() << endl;
}
return 0;
}
//********** Code the Die class constructor and methods after this line **********
Tjis is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <ctime>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
int die1; //The first die.
int die2; //The second die.
int rollingDice; //When the player throws dice
int rollingDice2 = 0; //When the player throws the dice if the first throw didn't give a winning or losing outcome.
const int SnakeEyes = 2; //If the player throws two ones.
const int BoxCars = 12; //If the player throws two sixes.
const int BigRed = 7; //If the player throws a three and four
srand(35);
die1 = rand() % 6 + 1; //Simulates a six sided die
die2 = rand() % 6 + 1;
rollingDice = die1 + die2; //Adds the dice together.
cout << "Player rolled: " << die1 << " + " << die2 << " = " << rollingDice << endl << endl; //Shows what the player rolled on first try.
if (rollingDice == BoxCars) //If the player rolls two sixes.
{
cout << "Boxcars!" << endl << endl;
}
if (rollingDice == BigRed)
{
cout << "Big Red!" << endl << endl; //If the player's roll equals 7.
}
if (rollingDice == SnakeEyes)//If the player rolls two ones.
{
cout << "Snake Eyes!" << endl << endl;
}
if (rollingDice == 7 || rollingDice == 11) //Player wins if they get a 7 or 11.
{
cout << "You won!" << endl;
cin.get();
return 0;
}
if (rollingDice == 2 || rollingDice == 3 || rollingDice == 12) //Player loses if they get a 2, 3, or 12 on first try.
{
cout << "Craps! You lost!" << endl;
cin.get();
return 0;
}
else
{
cout << "The Point is " << rollingDice << endl << endl; //If the player rolled a 4, 5, 6, 8, 9, or 10 on first try the game continues.
}
while (rollingDice2 != rollingDice && rollingDice2 != 7) //Attempts will be made until the player either rolls the same number or a 7.
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
rollingDice2 = die1 + die2;
cout << "Player rolled: " << die1 << " + " << die2 << " = " << rollingDice2 << endl << endl;
if (rollingDice2 == BoxCars)
{
cout << "Boxcars!" << endl << endl;//If the player rolls two sixes.
}
if (rollingDice2 == BigRed)
{
cout << "Big Red!" << endl << endl;//If the player's roll equals 7.
}
if (rollingDice2 == SnakeEyes)
{
cout << "Snake Eyes!" << endl << endl;//If the player rolls two ones.
}
if (rollingDice2 == rollingDice)
{
cout << "You rolled your point! You won!" << endl; // The player matched their number they won.
}
if (rollingDice2 == 7)
{
cout << "You seven'd out and lost!" << endl; // The player got a total of seven they lost.
}
}
return 0;
}
|
The output needs to look something like this.
Player rolled: 1 + 2 = 3
Craps! You lost!
Would you like to play again (y for yes)? y
Player rolled: 4 + 5 = 9
The point is 9
Player rolled: 2 + 2 = 4
Player rolled: 5 + 3 = 8
Player rolled: 5 + 3 = 8
Player rolled: 2 + 3 = 5
Player rolled: 2 + 1 = 3
Player rolled: 5 + 5 = 10
Player rolled: 1 + 4 = 5
Player rolled: 4 + 1 = 5
Player rolled: 3 + 4 = 7
You seven'd out and lost!
Would you like to play again (y for yes)? y
Player rolled: 1 + 3 = 4
The point is 4
Player rolled: 5 + 6 = 11
Player rolled: 6 + 2 = 8
Player rolled: 2 + 5 = 7
You seven'd out and lost!
Would you like to play again (y for yes)? y
Player rolled: 5 + 5 = 10
The point is 10
Player rolled: 6 + 1 = 7
You seven'd out and lost!
Would you like to play again (y for yes)? y
Player rolled: 4 + 3 = 7
You won!
Would you like to play again (y for yes)? y
Player rolled: 2 + 2 = 4
The point is 4
Player rolled: 3 + 4 = 7
You seven'd out and lost!
Would you like to play again (y for yes)? y
Player rolled: 5 + 4 = 9
The point is 9
Player rolled: 4 + 6 = 10
Player rolled: 3 + 3 = 6
Player rolled: 3 + 1 = 4
Player rolled: 3 + 6 = 9
You rolled your point! You won!
Would you like to play again (y for yes)? n