This is a game of pig program.
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h>
#include<ctime>
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;
}

New Topic/Question
Reply



MultiQuote




|