pig dice game

lHello,
my question is that I can not seem to locate where my error is.
The problem is that although the code runs it doesn't run completely, the game stops just when you are about to actually play the game. so the pig dice function runs partly and never gets to pd_turn function. my code is below.
Any help would be appreciated.

Thanks in advance.

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

void pigDice();
bool rollOff;
bool winner;
int rollDie();
int pd_turn(string name);
int player;
void save();
void Menu();

int main()
{
	 Menu();
	 

	return 0;
}
void Menu() {

	char choice = 'z';

	while (choice != 'q') {
		cout << "Menu\n"
			<< "a) Pig Dice Game\n"
			<< "b) Roll Die\n"
			<< "c) Game 3\n"
			<< "d) Stats menu\n"
			<< "q) quit\n"
			<< "Please enter a menu selection: ";
		cin >> choice;

		switch (choice)
		{
		case 'a':
		case 'A':
			pigDice();
			int pd_turn(string player);
			int rollDie();
			break;
		case 'b':
		case 'B':
			cout << "\nYou rolled a " << rollDie() << ".\n\n";
			int rollDie();
			break;
		case 'c':
		case 'C':
			break;
		case 'd':
		case 'D':
			break;
		case 'q':
		case 'Q':
			cout << "quitting game" << ".\n\n";
			choice ='q';
			break;
		default:
			cout << "Incorrect input please try again.\n";
		}//end of switch
	}  //end of while
}

void pigDice()  //function to control the game play for pigDice
{
	int player1_score, player2_score, player;
	char choice = 'z';
	string player1, player2;
	bool rollOff;

	//get name of players
	cout << "Player1 enter your name: ";
	cin >> player1;
	cout << "Player2 enter your name: ";
	cin >> player2;

	//players roll to determine the order of play.  Place in a loop in case of tie.
	while (choice != 'n')
	{
		cout << "\nDetermining the order of play. Player1 roll the dice. \n";
		system("pause");
		player1_score = rollDie();
		cout << "\nPlayer1 you rolled a " << player1_score << ".\n";
		cout << "\nPlayer2 roll the dice.\n";
		system("pause");
		player2_score = rollDie();
		cout << "\nPlayer2 you rolled a " << player2_score << ".\n";

		//establish who plays 1st and who plays 2nd
		if (player1_score < player2_score) {
			player1_score = 0;
			player2_score = 1;
			cout << player1 << " you play first. " << player2 << " you go second.\n";
			choice = 'n';  // terminate this loop
		}
		else if (player2_score < player1_score) {
			player2_score = 0;
			player1_score = 1;
			cout << player2 << " you play first. " << player1 << " you go second.\n";
			choice = 'n';  //terminate this loop
		}
		else
			cout << "\nIt's a tie.  You both roll a " << player1_score << ".\n"
			<< "Please try again.\n";
	}//end of while

	 //first player to play plays - play function
	if (player1_score = 0) {
		player = 1;
		player2_score = 0;
		player1_score = pd_turn(player1);
		if (player1_score >= 50)
			rollOff = true;
	}
	else {
		player = 2;
		player1_score = 0;
		player2_score = pd_turn(player2);
		if (player2_score >= 50)
			rollOff = true;

		if (player1_score == 0)
			player1_score = pd_turn(player1);
		else
			player2_score = pd_turn(player2);
	}

	//check if score is >50.  if it is then we have a roll off
	if (player1_score == 0) {
		player = 1;
		player2_score = 0;
		player1_score = pd_turn(player1);
		if (player1_score >= 50)
			rollOff = true;
	}
	else {
		player = 2;
		player1_score = 0;
		player2_score = pd_turn(player2);
		if (player2_score >= 50)
			rollOff = true;
	}
	//if we have a roll off, second player must roll until win or lose
	if (rollOff)
		if (player == 1) {
			cout << "\nWe have a Roll off! Player" << player2
				<< "it is your turn. you must score more than" << player1_score << "to win.\n";
			system("pause");
			player2_score = pd_turn(player2);
			if (player2_score <= player1_score) {
				cout << "\nSorry" << player2 << "you did not get enough points. You lose!\n";
				save();
				return;
			}
			else {
				cout << "'nSorry" << player1 << " you lose!" << player2 << " exceeded your points.\n";
				save();
				return;
			}
		}

		else if
			(player == 2) {
			cout << "\nWe have a Roll off! Player" << player1
				<< "it is your turn. you must score more than" << player2_score << "to win.\n";
			system("pause");
			player1_score = pd_turn(player1);
			if (player1_score <= player2_score) {
				cout << "\nSorry" << player1 << "you did not get enough points. You lose!\n";
				save();
				return;
			}
			else {
				cout << "\nSorry" << player2 << " you lose!" << player1 << " exceeded your points.\n";
				save();
				return;
			}
		}
		else
			cout << "\nError. Invalid player.\n";
	//if we do not have a roll off, loop, 
	else
		while (!winner)
		{

			if (player == 1) {
				player = 2;
				cout << "\n" << player2 << "your game score is " << player2_score << ".\n";
				player2_score = player2_score + pd_turn(player2);
				if (player2_score >= 50) {
					cout << "\n" << player2 << "you win! Your score is " << player2_score << ".\n";
					winner = true;
					save();
				}
			}
			else
			{
				player = 1;
				cout << "\n" << player1 << " your game score is" << player1_score << ".\n";
				player1_score = player1_score + pd_turn(player1);
				if (player1_score >= 50) {
					cout << "\n" << player1 << "you win! Your score is " << player1_score << ".\n";
					winner = true;
					save();
				}
			}
		}//end of while
}
int rollDie()
{
	int die;

	srand(time(0));
	die = rand() % 6 + 1;
	return die;
}
int pd_turn(string player)
{
	int score = 0, die =rollDie();
	char choice = 'z';
	system("pause");
	system("cls");
	cout << "\n\n" << player << " it is your turn.\n\nYou may roll the dice until  you choose to stop.\n"
		<< "Once you choose to stop your score for this turn\nwill be added to your overall score. "
		<< "If you roll a 1 \nthen you lose your turn and all the points for this turn.\n\n";
	while (choice != 'q');
	{
		cout << "\nrollDie();.\n";
		system("pause");
		die = rollDie();
		cout << "\nYou rolled a" << rollDie << ".\n";
		if (die == 1) {
			cout << "\nSorry, you rolled a " << die << " and so you lose your turn.\nYou get 0 points this turn.\n";
			score = rollDie();
			return die;
		}
		else {
			score = die;
			cout << "\nYour current points for this turn is: " << score << endl
				<< "\nWould you like to roll again or quit? Enter y to continue and n to quit: ";
			cin >> choice;
			system("cls");
		}
	}//end of while
	return score;
}
void save()
{
}

//end of pigDice game 



Last edited on
Hey. First of all welcome. Please edit your post and add code tags to your code, follow this article - http://www.cplusplus.com/articles/jEywvCM9/

Your program does enter the function pd_turn. However, the reason it stops is because of a misplaced semicolon. Read the comment below.

1
2
3
4
5
6
7
cout << "\n\n" << player << " it is your turn.\n\nYou may roll the dice until you choose to stop.\n"
		<< "Once you choose to stop your score for this turn\nwill be added to your overall score. "
		<< "If you roll a 1 \nthen you lose your turn and all the points for this turn.\n\n";

	while (choice != 'q'); // There should not be a semicolon here. Remove it.
	{
		cout << "\nrollDie();.\n";
oh wow, I can't believe I didn't see that.
Thanks, much appreciated.
Topic archived. No new replies allowed.