Why wont my loops repeat?
Can anyone help me figure this out?
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 <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand ( time ( NULL ) );
bool endgame = false; //this will be my sentinel for ending loops
bool stop = false;
do//1
{ //prompt user to play or replay the game
int play;
cout << "\nWould you like to play a game?\nPlease enter 1 for yes, or 0"
<< " for no. ";
cin >> play; //declare variables:pile size, starter, 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;
if ( play == 1 )//user wants to play
{
//output initial marbles in the pile and rules
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 mode
{
if ( pile == 63 or pile == 31 or pile == 15 or
pile == 7 or pile == 3 )
{
comp_turn = rand () % ( pile/2 ) + 1;
}
else 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;
}
}//end if loop for mode = 0
else
{
comp_turn = rand () % (pile/2) + 1;
}
//computer takes turn
pile = pile - comp_turn;
cout << "My turn.\nI choose to remove " << comp_turn
<< " marbles, leaving " << pile << " marbles.\n";
if ( pile == 1 )//siginifies endgame
{
endgame = true;
cout << "I win! Better luck next time!\n";
}//ends if 'pile=1' signifies endgame loop
else //game not over and human gets turn
{
cout << "Your turn. How many marbles do you want to"
<< " take? ";
cin >> human_turn;
//ensure legal choice by human
if ( human_turn > pile/2 or human_turn < 1 )
{
cout << "That choice wasn't legal, choose again. ";
cin >> human_turn;
}//ends ensure legal choice if loop
pile = pile - human_turn;
cout << "That leaves " << pile << " marbles.\n";
if ( pile == 1 )//signifies game is over
{
endgame = true;
cout << "You win! You're smarter than I thought!";
}//end of if game is over loop
}//end else game not over and human gets turn loop
//end if loop for mode = 0
} while ( pile > 1 && !endgame );//ends do2 loop where computer starts
}//end if loop for computer start
else //start=1 and human starts
{
do
{
cout << "Your turn. How many marbles do you want to take? ";
cin >> human_turn;//ensure legal choice by human
if ( human_turn > pile/2 or human_turn < 1 )
{
cout << "That choice wasn't legal, choose again. ";
cin >> human_turn;
}//ends ensure legal choice if loop
pile = pile - human_turn;
cout << "That leaves " << pile << " marbles.\n";
if ( pile == 1 )//indicates endgame
{
endgame = true;
cout << "You win! You're smarter than I thought!\n";
}//ends if pile=1 loop
else //computer gets a turn
{
if ( mode == 0 )//smart computer mode
{
if ( pile == 63 or pile == 31 or pile == 15 or
pile == 7 or pile == 3 )
{
comp_turn = rand () % ( pile/2 ) + 1;
}
else 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;
}
}//end of if computer smart mode=0
else
{
comp_turn = rand () % ( pile / 2) + 1;
}
pile = pile - comp_turn;
cout << "My turn. I choose to remove " << comp_turn
<< " marbles, leaving " << pile << " marbles.\n";
if ( pile == 1 )//see if game is won
{
endgame = true;
cout << "I win! Better luck next time!\n";
}//ends if loop for comp win
}//end of else loop for computers turn
}while ( pile > 1 && !endgame);
}//end of else start=1 and human starts loop
}//ends 'if (start = 0)' computer starts loop
//ends if user wantsto play
else//if user does not want to play
{
cout << "Maybe next time! Have a great day!";
stop = true;
}
} while ( !stop );//1
return 0;
}
|
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
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
srand ( time ( NULL ) );
bool endgame = false; //this will be my sentinel for ending loops
bool stop = false;
do//1
{
//prompt user to play or replay the game
int play;
cout << "\nWould you like to play a game?\nPlease enter 1 for yes, or 0"
<< " for no. ";
cin >> play; //declare variables:pile size, starter, 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;
if ( play == 1 )//user wants to play
{
//output initial marbles in the pile and rules
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 mode
{
if ( pile == 63 or pile == 31 or pile == 15 or
pile == 7 or pile == 3 )
{
comp_turn = rand () % ( pile/2 ) + 1;
}
else 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;
}
}//end if loop for mode = 0
else
{
comp_turn = rand () % (pile/2) + 1;
}
//computer takes turn
pile = pile - comp_turn;
cout << "My turn.\nI choose to remove " << comp_turn
<< " marbles, leaving " << pile << " marbles.\n";
if ( pile == 1 )//siginifies endgame
{
endgame = true;
cout << "I win! Better luck next time!\n";
}//ends if 'pile=1' signifies endgame loop
else //game not over and human gets turn
{
cout << "Your turn. How many marbles do you want to"
<< " take? ";
cin >> human_turn;
//ensure legal choice by human
if ( human_turn > pile/2 or human_turn < 1 )
{
cout << "That choice wasn't legal, choose again. ";
cin >> human_turn;
}//ends ensure legal choice if loop
pile = pile - human_turn;
cout << "That leaves " << pile << " marbles.\n";
if ( pile == 1 )//signifies game is over
{
endgame = true;
cout << "You win! You're smarter than I thought!";
}//end of if game is over loop
}//end else game not over and human gets turn loop
//end if loop for mode = 0
}
while ( pile > 1 && !endgame ); //ends do2 loop where computer starts
}//end if loop for computer start
else //start=1 and human starts
{
do
{
cout << "Your turn. How many marbles do you want to take? ";
cin >> human_turn;//ensure legal choice by human
if ( human_turn > pile/2 or human_turn < 1 )
{
cout << "That choice wasn't legal, choose again. ";
cin >> human_turn;
}//ends ensure legal choice if loop
pile = pile - human_turn;
cout << "That leaves " << pile << " marbles.\n";
if ( pile == 1 )//indicates endgame
{
endgame = true;
cout << "You win! You're smarter than I thought!\n";
}//ends if pile=1 loop
else //computer gets a turn
{
if ( mode == 0 )//smart computer mode
{
if ( pile == 63 or pile == 31 or pile == 15 or
pile == 7 or pile == 3 )
{
comp_turn = rand () % ( pile/2 ) + 1;
}
else 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;
}
}//end of if computer smart mode=0
else
{
comp_turn = rand () % ( pile / 2) + 1;
}
pile = pile - comp_turn;
cout << "My turn. I choose to remove " << comp_turn
<< " marbles, leaving " << pile << " marbles.\n";
if ( pile == 1 )//see if game is won
{
endgame = true;
cout << "I win! Better luck next time!\n";
}//ends if loop for comp win
}//end of else loop for computers turn
}
while ( pile > 1 && !endgame);
}//end of else start=1 and human starts loop
}//ends 'if (start = 0)' computer starts loop
//ends if user wantsto play
else//if user does not want to play
{
cout << "Maybe next time! Have a great day!";
stop = true;
}
}
while ( !stop ); //1
return 0;
}
|
I don't see the problem, when I do exactly what it told me to, it beat me.
It still has some sort of glitch that I can't find! When pile gets to one less than a power of 2, it doesn't loop again.
Topic archived. No new replies allowed.