I finished this last night and it almost works perfectly aside from one minor thing that I can't quite figure out.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int playerscore, computerscore;
int turnscore;
int dice;
char input = 0;
bool turnover, gameover;
srand((unsigned)time(0));
playerscore = 0;
computerscore = 0;
cout << "Welcome to the game of Pig! You get to roll first\n\n";
while(!gameover){ //while compiling, it says this bool value "gameover" is unused
turnscore = 0;
turnover = false;
do //Human Turn
{
dice = (rand()%6)+1;
if(dice==1){
cout << "You rolled a 1! ";
cout << "you lose your turn! Your total is " << playerscore << " \n";
turnscore = 0;
turnover = true;
}
else{
turnscore += dice;
cout << "\n";
cout << "You rolled a " << dice << " ";
cout << "Your turn score is " << turnscore << " ";
cout << "and your total score is " << playerscore << endl;
cout << "If you hold, you will have " << playerscore + turnscore << " points.\n";
cout << "Enter 'r' to roll again, or 'h' to hold: ";
cin >> input;
if(input == 'r'){
turnover = false;
}
else if(input == 'h'){
break;
}
else {
return 0;
}
}
}
while(!turnover);
playerscore += turnscore;
cout << "Your score is " << playerscore << "\n\n";
if(playerscore >= 100){
cout << "YOU WIN!!";
break;
gameover = true; //right here it should finish, but it goes all the way down to the end from here.
}
turnscore = 0;
do //Computer turn
{
dice = (rand()%6)+1;
if(dice==1){
cout << "Computer rolled a 1! ";
cout << "Computer loses its turn! Computer total is " << computerscore << " \n";
turnscore = 0;
turnover = true;
}
else{
turnscore += dice;
cout << "\n";
cout << "Computer rolled a " << dice << " ";
cout << "Computer's turn score is " << turnscore << " ";
cout << "and its total score is " << computerscore << endl;
if(turnscore >= 20){
turnover = true;
}
else {
turnover = false;
}
}
}
while(!turnover);
computerscore += turnscore;
cout << "Computer score is " << computerscore << endl;
if(computerscore >= 100){
cout << "COMPUTER WINS!!";
gameover = true;
}
}
return 0;
}
My problem is that after the Human wins and the message, "YOU WIN!!" is displayed, instead of terminating right there, it goes through the computer turn one more time before finishing.
I'm thinking perhaps I used my bool value "gameover" improperly, but I don't know why it would be wrong, any ideas?