General Game problem

Heres my problem. When Start = 0, computer starts, gives indication that human won and then that computer won but doesn't return to top of game and prompt user to play again.
Second problem, some math errors somewhere in above scenario, just too tired to figure that part out right now.
Third, large problem, when complete game, and prompted again to play or not, if choose not (any character other than 1), goes into infinite loop. I'd appreciate any input!

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

//seed the random number generator
srand ( time ( NULL ) );
bool endgame = false;
int play = 0;


do//loop to make repeat as long as user chooses to play
{
//declare variables for size of pile, who starts, and playing mode
int pile = 10 + rand () % 90;
int start = rand () % 2;
int mode = rand () % 2;
cout << pile << " " << start << " " << mode;
int human_turn;
int comp_turn;

//prompt user to play or replay the game
cout << "Would you like to play a game?\nPlease enter 1 for yes, or any"
<< " other key for no. ";
cin >> play;

if (play == 1 )//player says wants to play
{ //output the initial amount of marbles in the pile
cout << "The pile has " << pile << " marbles. On each turn, the "
<< "player must choose at least one\nmarble, but no more than "
<< "half of the marbles left in the pile. The player that\n"
<< "must take the last marble loses!\n";

if ( start == 0 )//computer starts
{
do
{
if ( mode == 0 )//smart computer
{
if ( pile >= 64 )
{
comp_turn = pile - 63;
}
else if ( pile >= 32 )
{
comp_turn = pile - 31;
}
else if ( pile >= 16 )
{
comp_turn = pile - 15;
}
else if ( pile >= 8 )
{
comp_turn = pile - 7;
}
else if ( pile >= 4 )
{
comp_turn = pile - 3;
}
else
{
comp_turn = 1;
}
}//smart computer mode IF ends
else//not smart mode
{
comp_turn = rand () % (pile / 2 ) + 1 ;
}//ends condition for not smart mode

pile = pile - comp_turn;//computer takes its turn
cout << "My turn.\nI choose to remove " << comp_turn << " marbles, "
<< "leaving " << pile << " marbles.\n";
if ( pile == 1 )
{
endgame = true;
cout << "I win! Better luck next time!\n";
pile = 0;
}
else//pile !=0; humans turn
{
cout << "Your turn. How many marbles do you want to take? ";
cin >> human_turn;

//ensure human makes legal choice
if ( human_turn > pile/2 or human_turn < 1 )
{
cout << "That was an incorrect choice, choose again.\n";
cin >> human_turn;
}

//computers turn
pile = pile - comp_turn;
cout << "That leaves " << pile << " marbles.\n";
}//end else pile!=0
if ( pile == 1 )
{
endgame = true;//reset condition to end game and start over
cout << "You win! You're smarter than I thought!\n";
pile = 0;
}

} while (! endgame);//do loop starts over if not end of game
}//computer start IF loop ends
else//human starts
{
do
{
if ( mode == 0 )//smart computer
{
if ( pile >= 64 )
{
comp_turn = pile - 63;
}
else if ( pile >= 32 )
{
comp_turn = pile - 31;
}
else if ( pile >= 16 )
{
comp_turn = pile - 15;
}
else if ( pile >= 8 )
{
comp_turn = pile - 7;
}
else if ( pile >= 4 )
{
comp_turn = pile - 3;
}
else
{
comp_turn = 1;
}
}//smart computer mode IF ends
else//not smart mode
{
comp_turn = rand () % (pile / 2 ) + 1 ;
}//ends condition for not smart mode

//humans turn
cout << "Your turn. How many marbles do you want to take? ";
cin >> human_turn;

//make sure humans choice is legal
if ( human_turn > pile/2 )
{
cout << "That was an incorrect choice, choose again.";
cin >> human_turn;
}
pile = pile - human_turn;
cout << "That leaves " << pile << " marbles.\n";

//set condition to end loop if game is won and start over
if ( pile == 1 )
{
endgame = true;
cout << "You win! You're smarter than I thought!\n";
}

else//computer turn
{
comp_turn = rand() % ( pile/2 ) + 1;
pile = pile - comp_turn;
cout << "My turn. I choose to remove " << comp_turn
<< " marbles, leaving " << pile << " marbles.\n";
}

//set condition for computer win and start over
if ( pile == 1 && ! endgame)
{
endgame = true;
cout << "I win! Better luck next time!\n";

}

} while ( ! endgame );//do loop starts over if not end of game
}//human starts IF/ELSE loop ends
}
else//player chooses not to play
{
cout << "Maybe next time!";
endgame =false;
}
}while (play == 1 && endgame );

return 0;
}
1) You have tons of duplicate code. As a general rule, if you are copy/pasting code to multiple places in your program, you're doing it wrong. Did you learn about functions yet?

2) Learn to indent. This would me much easier to read and follow if it was properly indented.

Both of the above points would help you solve your problem. Logic is much easier to follow when it's organized and clean. Since your code is so big and messy, it's very hard to spot the logic error.


3) As for your "big problem", I'm not sure if this is it, but you seem to be using endgame incorrectly. On line 188, you probably meant to have !endgame instead of endgame.
This is what I get when I enter 0:
1
2
3
Would you like to play a game?
Please enter 1 for yes, or 0 for no. 0
65 0 0Maybe next time! Have a great day!


This is what I get when I enter 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Would you like to play a game?
Please enter 1 for yes, or 0 for no. 1
31 1 0The pile has 31 marbles. On each turn, the player must choose at least one
marble, but no more than half of the marbles left in the pile. The player that
must take the last marble loses!
Your turn. How many marbles do you want to take? 15
That leaves 16 marbles.
My turn. I choose to remove 9 marbles, leaving 7 marbles.
Your turn. How many marbles do you want to take? 3
That leaves 4 marbles.
My turn. I choose to remove 1 marbles, leaving 3 marbles.
Your turn. How many marbles do you want to take? 1
That leaves 2 marbles.
My turn. I choose to remove 1 marbles, leaving 1 marbles.
I win! Better luck next time!

Would you like to play a game?
Please enter 1 for yes, or 0 for no. 0
72 1 1Maybe next time! Have a great day!]


It seems to work, but it is hard to understand what's going on in there.

This may help with what Disch said:
Google Cern C++ Coding Standard.

I've only had an interest in C++ for about 6 months, but this pdf helped alot.

Topic archived. No new replies allowed.