Need help with my craps game.

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
Hello bored62,

You should edit your post and change the green check mark to a question or the default.

With the green check mark you are telling everyone that you have an answer and no one is likely to respond to your post.

Looking at your program a couple of notes:

"ctime" and "time.h" are the same thing. I would just use "ctime".

"srand(35)" will always produce the same numbers each time the program is run. A better choice would be srand(static_cast<size_t>(time(NULL))); I use this because "srand" requires an "unsigned int" for the seed value. void srand (unsigned int seed);. See http://www.cplusplus.com/reference/cstdlib/srand/

I have not tested this yet, but I would take line 20 - 103 and put it in a do/while loop.
1
2
3
4
5
6
7
8
9
10
11
#include <cctype>  // <--- Header file to add.

char choice{};

do
{
	// <--- Your code here.

	std::cout << "\n Would you like to play again (y for yes)? ";
	std::cin >> choice;
} while (std::tolower(choice) == 'y');


hope that helps,

Andy
Hi everyone
@Markus23,

Hello.

Since you are new I will point out that it is bad form to invade someones thread with a statement that is not on topic or your own question. Save the hellos for when you post your first question.

While I am here please take the time to read these links:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
http://www.catb.org/esr/faqs/smart-questions.html

They will serve you well.

Andy
Hello bored62,

When I adjusted your program I found that the while loop worked better as a so/while loop with the condition of while (rollingDice2 != rollingDice && rollingDice2 != 7 && (std::tolower(choice) == 'y')).

While reading the OP I noticed that you did not create a "Die" class for the program.

While testing the program I came across this:

Player rolled: 5 + 2 = 7

Big Red!

You rolled your point! You won!
You seven'd out and lost!

 Would you like to play again (y for yes)?


I was wondering if you could win and loose at the same time.

In the statement if (rollingDice == 7 || rollingDice == 11). You have "return 0;". Do you really want to leave the program abruptly at this point? The same in the next if statement.

The do/while loop that I showed you is what you should change your while loop to.

Hope that helps,

Andy
Topic archived. No new replies allowed.