1. Write a function named Pig that plays the game of Pig. The rules are simple:
ID: 3765278 • Letter: 1
Question
1. Write a function named Pig that plays the game of Pig. The rules are simple:
In a game of Pig, two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions:
roll - if the player rolls a
1: the player scores nothing and it becomes the opponent's turn.
2-6: the number is added to the player's turn total and the player's turn continues.
hold - The turn total is added to the player's score and it comes the opponent's turn
quit - Allow the user to exit the game
You should break your function into multiple functions. I suggest having the following:
turn(player) that handles a player's turn. It should return the score from a single player's turn (a turn being until they hold or roll a 1).
outputScores(player1Score, player2Score) that outputs both player's scores in a nice way. You should make sure to output the scores after each turn. It would be great if you used innerHTML to output the scores to the page.
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int dieRoll();
int humanTurn(int);
int computerTurn(int);
int main()
{
int humanTotalScore = 0, computerTotalScore = 0;
srand(time(NULL));
do
{
humanTotalScore = humanTotalScore + humanTurn(humanTotalScore);
cout << "Your total score so far is " << humanTotalScore << "." << endl;
if(humanTotalScore >= 100)
{
cout << "You win!";
return 0;
}
computerTotalScore = computerTotalScore + computerTurn(computerTotalScore);
cout << "CPU total score so far is " << computerTotalScore << "." << endl;
if(computerTotalScore >= 100)
{
cout << "Computer wins!";
return 0;
}
}
while(humanTotalScore < 100 && computerTotalScore < 100);
}
int dieRoll()
{
return (rand() % 6) + 1;
}
int humanTurn(int humanTotalScore)
{
int thisTurnScore = 0, score = 0;
char rollOrHold;
do
{
score = dieRoll();
if(score == 1)
{
cout << "You rolled a 1. End of turn." << endl;
return 0;
}
thisTurnScore = thisTurnScore + score;
cout << "You rolled a " << score << ". Score so far this turn is " << thisTurnScore << "." << endl;
do
{
cout << "Roll again (r) or Hold (h)? ";
cin >> rollOrHold;
}
while(rollOrHold != 'r' && rollOrHold != 'R' && rollOrHold != 'h' && rollOrHold != 'H');
}
while(rollOrHold == 'r' || rollOrHold == 'R');
if(rollOrHold == 'h' || rollOrHold == 'H') return thisTurnScore;
int computerTurn(int computerTotalScore)
{
int thisTurnScore = 0, score = 0;
do
{
score = dieRoll();
if(score == 1)
{
cout << "CPU rolled a 1. End of turn." << endl;
return 0;
}
thisTurnScore = thisTurnScore + score;
cout << "CPU rolled a " << score << ". Score so far this turn is " << thisTurnScore << "." << endl;
}
while(thisTurnScore < 20);
if(thisTurnScore >= 20)
{
cout << "CPU holds." << endl;
return thisTurnScore;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.