Trying to get this Craps game to work.

So my program works it just doesn't generate new rolls when I play another game. I'm using a seed of 35 because the assignment requires me to do so. How can I get my game to run on different dice rolls after the first game?

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

//Declaring Global variables

int roll_count = 0;

//Function declarations

void playOneGameOfCraps();

int rollOneDice();

void FirstRoll();

void OtherRolls(int point);

//Main method

int main()

{

char ch;

while(true)

{

//Calling the function

playOneGameOfCraps();



cout<<"\nWould you like to play again (y for yes)?";

cin>>ch;

if(ch=='y'||ch=='Y')

{

continue;

}else

{

break;

}

}



return 0;

}

//Function implementation which have the code to play the game

void playOneGameOfCraps()

{

srand(35);

FirstRoll();

}

void FirstRoll()

{

int sum = 0;

char ch;

int point;

sum = rollOneDice();

if (sum == 7 || sum == 11) {

cout << "Yeah! you won with a " << sum << " on the first roll." << endl;

ch = 'W';

}

else if (sum == 2 || sum == 3 || sum == 12) {

cout << "Sorry, you lost with a " << sum << " on the first roll." << endl;

}

else if (sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10) {

point = sum;

cout <<"\nYour point is " << sum << endl;

cout<<endl;

OtherRolls(point);

}

}

void OtherRolls(int point)

{

int sum = 0;

while (true) {

sum = rollOneDice();

// cout << "Roll " << roll_count << ". " << sum << " " << endl;

if (sum == point) {

cout << "\nYou rolled your point! You won!" << endl;

break;

}

else if (sum == 7) {

cout << "Craps! You lost!" << endl;

break;

}

else

continue;

}

}

int rollOneDice()

{

int sum = 0;

int dice1,dice2;

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


dice1= rand() % 5 + 1;

dice2= rand() % 5 + 1;



sum=(dice1+dice2);

cout<<"Player rolled:"<<dice1<<"+"<<dice2<<"="<<sum<<endl;

if (sum == BoxCars) //If the player rolls two sixes.
            {
                cout << "Boxcars!" << endl << endl;
            }

    if (sum == BigRed)
            {
                cout << "Big Red!" << endl << endl; //If the player's roll equals 7.
            }

    if (sum == SnakeEyes)//If the player rolls two ones.
            {
                cout << "Snake Eyes!" << endl << endl;
            }

roll_count++;

return sum;

}
Last edited on
You can't.

What you're using to generate your random numbers - rand() - isn't random. It will generate a sequence of numbers based on the seed value you give it with srand().

If you give it the same seed value every time, you'll get the same sequence every time.

You are giving it the same seed value every time (35) so you get the same sequence every time.
Are you sure you're not allowed to put srand(35); at the beginning of main instead of within your loop?
Your srand call should be early in main since you only want it to happen once in the entire run of your program.

You are generating random values from 1 to 5, not 1 to 6. It should be:

 
dice1 = rand() % 6 + 1;

The rand() % 6 yields values from 0 to 5, adding 1 makes them 1 to 6.

In FirstRoll, the last else if should just be an else since the possibilities it tests are the only possibilities left.

(And it looks like you might mean to say "Yay!" instead of "Yeah", which just means yes, as in, "yeah, right".)
closed account (367kGNh0)
tip: use
 
#include <ctime> 

for even more randomness
Putting srand (35) at the beginning of my main function fixed my issue. Thank you!
Topic archived. No new replies allowed.