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

Game Description In this game, the user plays against the computer. The two play

ID: 3574207 • Letter: G

Question

Game Description In this game, the user plays against the computer. The two players (the user and the computer) take turns in rolling two die. Both the players race to reach a score of 50. The first player who scores 50 or more points wins. The game starts with the user’s turn. Each turn, a player repeatedly rolls two die until either a 1 is rolled on any of the two die or the player decides to "hold". The following are the rules of the game:

If the player does not roll a 1 on any of the two die, the sum of the two die is added to his turntotal and the player's turn continues.

If the player rolls a 1 on one of the two die, he score nothing and it becomes the next player's turn.

If two 1s are rolled, the player’s entire total score is lost, and the turn ends.

If a player chooses to "hold", his turn-total is added to his/her total score, and it becomes the next player's turn.

The first player to score 50 or more points wins. Program Requirements Your program should be resilient to having wrong input. The program input should not be case sensitive. Therefore, if the user enters an input character (e.g., 'H' for hold), the program should accept and interpret both small and capital characters in the same way (e.g., both 'H' and 'h' are accepted as the input to hold). You can make the computer player follow any intelligent strategy to win. However, to simplify the requirements, you can make it have the strategy of making random choices with 50% chance for rolling and 50% for holding.

For example, the following C++ statement will set the computer response to 'R' for rolling, or 'H' for holding with 50% probability for each: response = rand()%2? 'R' : 'H';

Pseudo Code The following shows a sample pseudo code for this game program:

Initialize turnTotal, userScore and computerScore to zero Initialize turn to user

Loop until one of the scores reaches 50 or higher

If user turn Call function to handle the user turn

Else Call function to handle the computer turn Set turnTotal = 0

End loop

If userScore 50 User wins

Else Computer wins

Endif

Handling the user turn:

Display the current scores User inputs ‘R’ to start rolling Loop while the user input is ‘R’ and it is still his turn Roll the two die

If both rolls are 1 Reset the userScore to zero Change the turn to computer

Else if one of the rolls is 1 Change the turn to computer

Else Add the two die rolls to turnTotal and display turnTotal Ask the user if he wants to “Hold” or “Roll” again

Endif

If the users entered “Hold” Add the turnTotal to userScore Change the turn to Computer

Endif

End loop

Handling the computer turn:

This is very similar to handling the user turn except that instead of reading the user’s input, the computer generates his choices automatically.

Explanation / Answer

*******************program.cpp**************

#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;

const int scoreLimit = 10;

int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);
int dr,dr1;
int main()
{
   bool continuePlay = 1;
   int humanTotalScore = 0, computerTotalScore = 0;
   cout << "This is a game of Pig. It is your turn. Press r to roll." << endl << endl;
  
   if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit))
   {
       continuePlay = 1;
   }
   else
   {
       continuePlay = 0;
   }
   do
   {
       cout << "Computers Score: " << computerTotalScore << endl;
      
       humanTurn(humanTotalScore);
       computerTurn(computerTotalScore);
      
   }
   while(continuePlay = 1);

   if(humanTotalScore>=scoreLimit && humanTotalScore > computerTotalScore)
   {
       cout << "Good job! You won.";
   }
   else
   {
       cout << "Sorry, you lost. Try again!";
   }
   getchar();
   getchar();
  
  
   return 0;
}


int humanTurn(int& humanTotalScore)
{
   int currentScore = 0;
   int lastRoll;
   int lastRoll1;
   char rollOrHold;
   cout << "Your total score is: " << humanTotalScore << "." << endl;
   cout << "Press r to roll again, or h to hold." << endl;
   cin >> rollOrHold;
   while (rollOrHold == 'r')
   {
       diceRoll();
       lastRoll=dr;
       lastRoll1=dr1;
       if (lastRoll == 1 || lastRoll1 == 1)
       {
           cout << "You rolled a 1, ending your turn." << endl;
           break;
       }
       else if(lastRoll == 1 && lastRoll1 == 1){
           currentScore=0;
           cout << "The die1 shows a " << lastRoll<< " The die2 shows a " << lastRoll1<< "Your score this turn is: " << currentScore << endl;
       }
       else
       {
           currentScore = currentScore+lastRoll+lastRoll1;
           cout << "The die1 shows a " << lastRoll<< " The die2 shows a " << lastRoll1<< " Your score this turn is: " << currentScore << endl;
           cout << "Press r to roll again, or h to hold." << endl;
           cin >> rollOrHold;
          
       }
   }
   while (rollOrHold == 'h')
   {
       humanTotalScore = humanTotalScore+currentScore;
       break;
      
   }
  
   return humanTotalScore;
}

int computerTurn(int& computerTotalScore)
{
   int currentScore = 0;
   int lastRoll,lastRoll1;
   cout << "Computers total score is: " << computerTotalScore << "." << endl;
   while ((currentScore <= 20) && (currentScore != 1))
   {
  
       diceRoll();
       lastRoll=dr;
       lastRoll1=dr1;
       if (lastRoll == 1 || lastRoll1 == 1)
       {
           cout << "Computer rolled a 1, ending computer's turn." << endl;
           break;
       }
       else if(lastRoll == 1 && lastRoll1 == 1){
           currentScore=0;
           cout << "The die1 shows a " << lastRoll<< " The die2 shows a " << lastRoll1<< "Computer score this turn is: " << currentScore << endl;
       }
       else
       {
           currentScore = currentScore+lastRoll+lastRoll1;
           cout << "The die1 shows a " << lastRoll<< " The die2 shows a " << lastRoll1<< " Computer score this turn is: " << currentScore << endl;
          
       }
  
   }
       computerTotalScore += currentScore;

  
   return computerTotalScore;
}


int diceRoll()
{
   int x,y;
   srand (time(0));
   int const j = 1, k =6;
   x = (j+(rand()%(k-j+1)));
   y = (j+(rand()%(k-j+1)));
//   cout<<" x:"<<x<<" y:"<<y;
   dr=x;
   dr1=y;
   srand(1);
   return x;
}

**********************output**************************

This is a game of Pig. It is your turn. Press r to roll.

Computers Score: 0
Your total score is: 0.
Press r to roll again, or h to hold.
r
You rolled a 1, ending your turn.
Computers total score is: 0.
Computer rolled a 1, ending computer's turn.
Computers Score: 0
Your total score is: 0.
Press r to roll again, or h to hold.
r
The die1 shows a 2
The die2 shows a 6
Your score this turn is: 8
Press r to roll again, or h to hold.
h
Computers total score is: 0.
The die1 shows a 3
The die2 shows a 3
Computer score this turn is: 6
The die1 shows a 3
The die2 shows a 3
Computer score this turn is: 12
The die1 shows a 3
The die2 shows a 3
Computer score this turn is: 18
The die1 shows a 3
The die2 shows a 3
Computer score this turn is: 24
Computers Score: 24
Your total score is: 8.
Press r to roll again, or h to hold.