for loop dice 21

Heloo im trying to make this program function the player has to win two rounds of dice 21 in order to win if he loses two rounds they lose. This is the code I have im trying to make it function but Im having trouble with the for loop.

The player has two roll 21 to win the round.
If they fail to get 21 they lose the round
If the player decides not to roll and finds out the next number would have cost him to lose they win the round.
The player has to win 2 out of 3 rounds to win the game.
The trouble Im having is trying to make the foor loop work.

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
#include<iostream>
#include<cstdlib>
#include<time.h>

using namespace std;

int getRandomNum( int lowRange, int highrange);

int main()
{

char choice=' ';
char keep=' ';


srand( static_cast<int>( time(NULL) ) );

///////////////////////////// Start of the code

cout<<"**************** Welcome to the Dice 21 game! ****************"<<endl;
cout<<endl;


cout<<"You must win the majority of rounds to win the game. In each";
cout<<"round you will roll one 10-sided die as many times as you like.";
cout<<"Each time you roll, the number on the die will be added to your";
cout<<"total score. Your goal is to get a score as close as possible to";
cout<<"21 without going over 21.";
cout<<endl;
cout<<endl;

cout<<"At the end of a round, you will be able to look ahead to see";
cout<<" what the next roll would have been. You win the round if the";
cout<<" next roll would have caused you to go bust. Otherwise you lose";
cout<<" the round"<<endl;
cout<<endl;

cout<<"********** Round 1 *********"<<endl;


int rollValue= 0;
int total_turn= 0;
int value1= 0;
int value2= 0;
int tries= 0;
int loss= 0;
int round= 0;

for(round=0; round>=2; round++)
{


do
{
	if(rollValue==1)
	{
		cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
		cout<<"Enter 'y' if you would like the value to be 11"<<endl;
		cout<<"Enter any other character if you would like to keep the value 1"<<endl;
		cin>> keep;
		if(keep=='y')
		{
			value1= 11;
			cout<<"You rolled:"<<value1<<endl;
			total_turn= total_turn + value1;
			cout<<"Your roll total is:"<<total_turn<<endl;
			cout<<endl;
		}
		else
		{
		value2= 1;
		cout<<"You rolled:"<<value2<<endl;

		total_turn= total_turn+ value2;
		cout<<"Your roll total is :"<< total_turn<<endl;
		cout<<endl;
		}
	}

	cout<<"Rolling..."<<endl;
	rollValue = rand() % 10 + 1;
	cout<<"You rolled a :"<<rollValue<<endl;
	total_turn= total_turn + rollValue;
	cout<<"Your roll tatal is :"<<total_turn<<endl;
	cout<<endl;


	cout<<"Do you want to roll again: (y/n)";
	cin>> choice;

	if(total_turn==21)
	{
		cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
		round++;
		break;
		
	}
	else if(total_turn>21)
	{
		cout<<"Sorry you lose you rolled over 21"<<endl;
		break;

		
	

	}

	if(choice=='y')
	{

if(rollValue==1)
	{
		cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
		cout<<"Enter 'y' if you would like the value to be 11"<<endl;
		cout<<"Enter any other character if you would like to keep the value 1"<<endl;
		cin>> keep;
		if(keep=='y')
		{
			value1= 11;
			cout<<"You rolled:"<<value1<<endl;
			total_turn= total_turn + value1;
			cout<<"Your roll total is:"<<total_turn<<endl;
			cout<<endl;
		}
		else
		{
		value2= 1;
		cout<<"You rolled:"<<value2<<endl;
		
		total_turn= total_turn+ value2;
		cout<<"Your roll total is"<< total_turn<<endl;
		cout<<endl;
		}
}

	cout<<"Rolling..."<<endl;
	rollValue = rand() % 10 + 1;
	cout<<"You rolled a :"<<rollValue<<endl;
	total_turn= total_turn + rollValue;
	cout<<"Your roll tatal is :"<<total_turn<<endl;
	if(total_turn==21)
	{
		cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
		break;
		
	}
	else if(total_turn>21)
	{
		cout<<"Sorry you lose you rolled over 21"<<endl;
		break;
		
	}

	cout<<endl;
	cout<<"Do you want to roll again: (y/n)";
	cin>> choice;



	}
	else if(choice=='n')
	{
		cout<<"Your roll total is:"<<total_turn<<endl;
		rollValue = rand() % 10 + 1;
			cout<<"Look ahead to the next roll..."<<endl;
			cout<<"Your next roll would have been:"<<rollValue<<endl;
			total_turn=total_turn+rollValue;
		if(total_turn>21)
		{
			cout<<"Good choice you would have one BUST, if you have rolled again"<<endl;
			round++;
			break;
		}
		else if(total_turn<21)
		{
			cout<<"BUMMER- you could have rolled again"<<endl;
			break;
		}

	

	}
	else
	{
		cout<<"invalid input"<<endl;

		break;

	}
}while(total_turn<= 21);


}


system("pause");
return 0;
}
Last edited on
for(round=0; round>=2; round++)

I think you meant for the >= to be a <=.

Since it's three rounds, it would make more sense to me if it was just
for (round = 0; round < 3; ++round) // round++ works too .
thanks but the problem im having is that it wont go in to the rest of the loop if you could please debug this program and see what my problem is
As it stands your program will never ever ever ever.... go in to the for loop because you are imposing the condition that it go in to the loop only if round is greater than 1. The post made by long will allow it to go in to the for loop.
So if I add more round I only need 3 how would i update it to increase the round count in the for loop.

I will use the same code in round 2 and 3.
If you want to play the game 3 times then put what long said. Also you need to reset the value of total_turn to 0 every new game. So put total_turn = 0 in the for loop just before the while loop and start fixing the code from there.


EDIT:There are other problems with the program for instance the player doesn't want to roll the dice yet you still add the roll value to total_turn. Instead you should add in the code int temp = 0 after line 162 and in line 167 have
temp = total_turn + rollValue and check temp rather than total_turn.

EDIT2:"how would i update it to increase the round count in the for loop"
The code for(round=0; round<3; round++) means "start with round = 0 then increment round by 1 every time it goes in to the for loop until round is greater than 2". So round increases each time because you have "round++" there.
Last edited on
thanks Krahl. but I only have to play 3 rounds of dice 21 and only 3. You have to win 2 out of three rounds in order to win the game. If you lose 2 out of 3 you lose the game.

The player wins if the game if they win 2 rounds and roll 21 or decide to not roll and find out the next number would make them lose or go bust.

The player loses the game if they lose 2 rounds if they roll greater than 21 or decide not to roll and find out if they could have rolled again.

this is my main problem and I know it's in the for loop. The thing is i dont know what to put in the for loop the rounds won, or rounds lost, or how many rounds.
Wow that is a complicated game. Ok I think I understand. However you still need to do the corrections we have suggested. In addition to these corrections you need to have a variable to record the wins.

So the win conditions are.
1- win 2 games and roll 21
or
2- win 2 games and choose not to roll and find out if you had rolled you would have bust.

Is this correct?
Or win 2 games if you roll either 21 or find out your next number would have made you go bust

You could win the game also by rolling 21 in 1 round and in the next round find out your next number would have made you go bust.

Topic archived. No new replies allowed.