Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ question: The game of Pig is a simple two player dice game in which the firs

ID: 3799141 • Letter: C

Question

C++ question:

The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die:

• If the player rolls a 2–6 then he or she can either

— ROLL AGAIN or

— HOLD. At this point the sum of all rolls made this turn is added to the player’s total score and it becomes the other player’s turn.

• If the player rolls a 1 then the player loses his or her turn. The player gets no new points and it becomes the opponent’s turn.

If a player reaches 100 or more points after holding then the player wins. Write a program that plays the game of Pig, where one player is a human and the other is the computer. Allow the human to input “r” to roll again or “h” to hold.

The computer program should play according to the following rule: Keep rolling on the computer’s turn until it has accumulated 20 or more points, then hold. Of course, if the computer wins or rolls a 1 then the turn ends immediately. Allow the human to roll first.

Write your program using at least two functions:

int humanTurn( int humanTotalScore);

int computerTurn( int computerTotalScore);

These functions should perform the necessary logic to handle a single turn for either the computer or the human. The input parameter is the total score for the human or computer. The functions should return the turn total to be added to the total score upon completion of the turn. For example, if the human rolls a 3 and 6 and then holds, then humanTurn should return 9. However, if the human rolls a 3 and 6 and then a 1, then the function should return 0.

Explanation / Answer

Hope this works !

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;

int dieRoll();
int humanTurn(int);
int computerTurn(int);

int main()
{
   int humanTotalScore = 0, computerTotalScore = 0;
      
   //loop to keep playing until someone scores 100+
   do
   {
       humanTotalScore = humanTotalScore + humanTurn(humanTotalScore); //add the score from a new turn to the running total
       cout << "Your total score so far is " << humanTotalScore << "." << endl;
       if(humanTotalScore >= 100)
       {
           cout << "You win!";
           break;
       }
       computerTotalScore = computerTotalScore + computerTurn(computerTotalScore); //add the score from a new turn to the running total
       cout << "Computer total score so far is " << computerTotalScore << "." << endl;
       if(computerTotalScore >= 100)
       {
           cout << "Computer wins!";
           break;
       }
   }
   while(humanTotalScore < 100 && computerTotalScore < 100);
      
   return 0;
}


int dieRoll()
{
       return (rand() % 6) + 1; //call to rand() returns 0-5, + 1 to give range 1-6
}

int humanTurn(int humanTotalScore)
{
   int isTurnScore = 0, score = 0;
   char isRollHold;
  
   //loop to keep going as long the player chooses Roll Again or a 1 is thrown
   do
   {
           score = dieRoll(); //roll the die
      
       if(score == 1)
       {
           cout << "You rolled a 1. End of turn." << endl;
           break;
       }
      
       isTurnScore = isTurnScore + score; //running total for this turn only
              
       cout << "You rolled a " << score << ". Score so far this turn is " << isTurnScore << "." << endl;
       cout << "Roll again (r) or Hold (h)? ";
       cin >> isRollHold;
   }
   while(isRollHold == 'r' || isRollHold == 'R');
  
   if(isRollHold == 'h' || isRollHold == 'H') return isTurnScore; //finsh turn and return total score if player chooses to Hold
      
   return 0; //will only get this far if player rolled a 1
}

int computerTurn(int computerTotalScore)
{
   int isTurnScore = 0, score = 0;
  
   //loop to keep going as long the CPU score for this turn is less than 20
   do
   {
       score = dieRoll(); //roll the dice
      
       if(score == 1)
       {
           cout << "CPU rolled a 1. End of turn." << endl;
           break;
       }
      
       isTurnScore = isTurnScore + score; //running total for this turn only
              
       cout << "CPU rolled a " << score << ". Score so far this turn is " << isTurnScore << "." << endl;
   }
   while(isTurnScore < 20);

   //finsh turn and return total score if the CPU scored 20+
   if(isTurnScore >= 20)
   {
       cout << "CPU holds." << endl;
       return isTurnScore;
   }
      
   return 0; //will only get this far if CPU rolled a 1
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote