The problem is that the program would not terminate if either the human or the computer reach 100 or above points. it is not displaying if the human wins that you won or if the computer wins then sorry you lost, try again.
#include <iostream>
#include <ctime>
using namespace std;
const int scoreLimit = 100;
int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);
int main()
{
bool continuePlay = true;
int humanTotalScore = 0, computerTotalScore = 0;
srand(time(NULL));
cout << "This is a game of Pig. It is your turn. Press r to roll." << endl << endl;
if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit))
{
continuePlay = true;
}
else
{
continuePlay = false;
}
do
{
cout << "Computers Score: " << computerTotalScore << endl;
humanTurn(humanTotalScore);
computerTurn(computerTotalScore);
}
while(continuePlay = true);
while(continuePlay = false)
{
if(humanTotalScore >= scoreLimit)
{
cout << "Good job! You won.";
break;
}
else if (computerTotalScore >= scoreLimit)
{
cout << "Sorry, you lost. Try again!";
break;
}
}
return 0;
}
int humanTurn(int& humanTotalScore)
{
int currentScore = 0;
int lastRoll;
char rollOrHold;
cout << "Your total score is: " << humanTotalScore << "." << endl;
cout << "Press r to roll again, or h to hold." << endl;
cin >> rollOrHold;
while (rollOrHold == 'r')
{
lastRoll = diceRoll();
if (lastRoll == 1)
{
cout << "You rolled a 1, ending your turn." << endl;
break;
}
else
{
currentScore += lastRoll;
cout << "The die shows a " << lastRoll << ". Your score this turn is: " << currentScore << endl;
cout << "Press r to roll again, or h to hold." << endl;
cin >> rollOrHold;
}
}
while (rollOrHold == 'h')
{
humanTotalScore += currentScore;
break;
}
return humanTotalScore;
}
int computerTurn(int& computerTotalScore)
{
int currentScore = 0;
int lastRoll;
cout << "Computers total score is: " << computerTotalScore << "." << endl;
while ((currentScore <= 20) && (currentScore != 1))
{
lastRoll = diceRoll();
if (lastRoll == 1)
{
cout << "The computer rolled a 1, ending their turn." << endl;
break;
}
else
{
currentScore += lastRoll;
cout << "The computers die shows a " << lastRoll << ". Their score this turn is: " << currentScore << endl;
}
}
if(currentScore >= 20)
{
computerTotalScore += currentScore;
cout << "After the computers turn, they have gained an additional " << lastRoll << "points." << endl;
}
return computerTotalScore;
}
int diceRoll()
{
int x;
int const j = 1, k =6;
x = (j+(rand()%(k-j+1)));
return x;
}