//this game is played with a six sided die, on each player's turn he may roll until he gets a 1 from the die, at which point, all points accumulated to that point are wiped, he may hold and add the accumulation to his score at any time.
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h>
using namespace std;
const int SCORE_LIMIT = 100;
int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);
int main ()
{
bool no_winner = 1;
int humanTotalScore = 0, computerTotalScore = 0;
cout << "I would like to play a game" << endl;
cout << "Take your turn now; the game is pig" << endl << endl;
if ((humanTotalScore < SCORE_LIMIT) && (computerTotalScore < SCORE_LIMIT))
{
no_winner = 1;
}
else
{
no_winner = 0;
}
do
{
cout << "Current computer score: " << computerTotalScore << endl;
computerTurn(computerTotalScore);
humanTurn(humanTotalScore);
}
while (no_winner = 1);
if (no_winner = 0, humanTotalScore > computerTotalScore)
{
cout << "You win this time human";
}
else
{
cout << "The Machines win again, human";
}
getchar();
getchar();
return 0;
}
int humanTurn(int& humanTotalScore)
{
int accumulatedScore = 0; //this is the score accumulated from rolling the dice on this turn
char choice; //if user wants to continue rolling or keep his score
int lastRoll; //value of the last dice roll
cout << "Your current score is " << humanTotalScore << "." << endl;
cout << "Would you like to roll the dice?" << endl;
cout << "R(r) to Roll, H(h) to hold your score" << endl;
cout << "Accumulation from this set of rolls is " << accumulatedScore << endl << endl;
cin >> choice;
while (choice == 'R' || choice == 'r')
{
lastRoll = diceRoll();
if (lastRoll == 1)
{
break;
}
else
{
accumulatedScore += lastRoll;
cout << "You rolled a " << lastRoll << endl;
cout << "R(r) to roll Again. H(h) to hold. Current Accumulation:" << accumulatedScore << endl;
cin >> choice;
}
}
while (choice == 'H' || choice == 'h')
{
humanTotalScore += accumulatedScore;
}
while ( (choice != 'H') || (choice != 'h') || (choice != 'R') || (choice != 'r') ) //not sure why this is not working
{
cout << "Invalid input, please enter either R(r) or H(h)" << endl;
cin >> choice;
}
return humanTotalScore;
}
int computerTurn (int& computerTotalScore)
{
int accumulatedScore = 0; //this is the score accumulated from rolling the dice on this turn
char choice; //if user wants to continue rolling or keep his score
int lastRoll; //value of the last dice roll
/* while ((lastRoll != 1) && (accumulatedScore <= 20))
{
lastRoll = diceRoll();
accumulatedScore += lastRoll;
computerTotalScore += accumulatedScore;
}
*/
do
{ lastRoll = diceRoll();
if (lastRoll == 1)
{
break;
}
else
{
accumulatedScore += lastRoll;
}
}
while (accumulatedScore <= 20);
return computerTotalScore;
}
int diceRoll( )
{
int x, count; //x is the random number to be written to the file, count is the amount of numbers to be generate
srand (time(NULL)); //gets a new seed based on the time
int const M = 1, N = 6;
x = (M+(rand()%(N-M+1)));
return x;
}
This is the code, I can't figure out why the while loop that checks if the user did not input an h, H, r, or R does not work properly. I'm sure the solution is simple, its just some kind of syntax thing maybe?