I am writing a game program for a dice game. You want to get 100 points. You roll until you decide to hold, and the sum of your rolls is added to your total score. If you roll a 1 though, your turn is over and you get 0 points for that turn.
My problem is that when the user (me) rolls a 1, it appears that the computer rolls a 1 right after me. For example I get 0 points for my turn, without fail after me the computer gets 0 points also. If I hold, it acts how I want it to (the computer gets points but sometimes gets 0). There are 3 functions besides main. One for the human turn, the computer turn, and to simulate a dice roll. I'm pretty stumped as to how these functions are affecting each other. If anyone has any suggestions or hints as to what the problem is please let me know, thanks.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int humanTurn(int humanTotalScore); // human turn function
int computerTurn (int computerTotalScore); // computer turn function
int diceRoll (); // dice roll function
int main ()
{
int humanTotalScore = 0;
int computerTotalScore = 0;
cout << "Are you ready to play? Good." << endl;
while ((computerTotalScore < 100) && (humanTotalScore < 100))
{
humanTotalScore = humanTurn (humanTotalScore);
computerTotalScore = computerTurn(computerTotalScore);
}
if (humanTotalScore >= 100) // human wins
{
cout << "Good job, you won." << endl;
}
else
{
cout << "You lost, better luck next time." << endl; // computer wins
}
}
int diceRoll()
{
int x;
srand (time(NULL)); // uses a seed based on the time
x=((rand()%6)+1);
return x;
}
int humanTurn (int humanTotalScore)
{
int score = 0;
int roll;
char rollAgain;
cout << "Your total score is " << humanTotalScore << "." << endl;
cout << "Your score for this turn is " << score << "." << endl;
cout << "Enter r to roll again or h to hold." << endl;
cin >> rollAgain;
while ((rollAgain == 'r') || (rollAgain == 'R'))
{
roll = diceRoll();
if (roll == 1)
{
cout << "You rolled a 1, your turn is over and you gain 0 points." << endl;
cout << endl;
return humanTotalScore; // humanTotalScore is returned without adding anything to it
}
else
{
score = score + roll;
cout << "You rolled a " << roll << " and your score for this turn is " << endl;
cout << score << ". Enter r to roll again or h to hold." << endl;
cin >> rollAgain;
}
}
while ((rollAgain == 'h') || (rollAgain == 'H'))
{
humanTotalScore = humanTotalScore + score; // humanTotalScore is returned after adding the turn score to it
return humanTotalScore;
}
}
int computerTurn (int computerTotalScore)
{
int score = 0;
int roll;
roll = diceRoll();
while (score < 20)
{
if ((roll == 1))
{
cout << "Computer gained 0 points this turn and has a total of " << endl;
cout << computerTotalScore << " points." << endl;
cout << endl;
return computerTotalScore;
}
else
{
score = score + roll;
}
}
{
computerTotalScore = computerTotalScore + score;
cout << "Computer gained " << score << " points this turn." << endl;
cout << "Computer has a total of " << computerTotalScore << " points." << endl;
cout << endl;
}
return computerTotalScore;
}