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

MUST BE IN C++. Write a program named PigDice.cppthat plays the game “Pig.” Pig

ID: 3860295 • Letter: M

Question

MUST BE IN C++.

Write a program named PigDice.cppthat plays the game “Pig.” Pig is a two-player game where the players take turns repeatedly rolling a single–sided die; a player repeatedly rolls the die until one of two events occurs. Either the person chooses to stop rolling, in which case the sum of that player’s rolls are added to his/her total points; or if the player rolls a 1 at any time, all points for that turn are lost and the turn ends immediately. The first player to reach a score of at least 50 points wins. Additional Requirements: 1. Use the C++ Style Guide to format the code you hand in for this assignment. It is on Canvas under Course Documents.

Here what I have

// Global constants
enum class Player { ONE, TWO }; // two player game.
const unsigned int GAME_LIMIT = 50; // First player to this value wins.

// function prototypes
unsigned int roll(); // simulates rolling one die.
unsigned int play( Player player ); // models one player's turn.
void reportTotal( Player player, unsigned int score ); // current total.
void printWinner( unsigned int player1_total, // Game over, man!
unsigned int player2_total );

using namespace std;

// main controls the game, allowing the players to take turns.
// main also determines when the game is over.
int main()
{
unsigned int player1_total = 0;
unsigned int player2_total = 0;

while ( player1_total < GAME_LIMIT
&& player2_total < GAME_LIMIT )
{
player1_total += play( Player::ONE );
reportTotal( Player::ONE, player1_total );

// if player_1 has won the game, player2 doesn't get another turn.
if ( player1_total < GAME_LIMIT )
{
player2_total += play( Player::TWO );
reportTotal( Player::TWO, player2_total );
}
}

// first person to GAME_LIMIT wins.
printWinner(player1_total, player2_total );
  
// needed in Visual Studio so the console doesn't disappear.
system("pause");
}

// simulates rolling one die.
unsigned int roll()
{
// We use the new random facilities of C++11
// They are static so that they are only instantiated once.
static default_random_engine engine(static_cast<unsigned int>(time(0)));
static uniform_int_distribution<unsigned int> randomInt(1, 6);

return randomInt(engine);
}

Explanation / Answer

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

const int SCORE_LIMIT = 100;

int diceroll();

int humanturn( int& humanTotalscore);

int computerTurn(int& omputerTotalscore);

int main()

{

    bool win =1;

int humantotalscore = 0, computertotalscore = 0;

cout << "it is your turn.press r to roll. ";

if ((humantotalscore < score-limit) &&( computertotalscore < score-limit))

{

         win =1;

}

else

{

      win = 0;

}

do

{

   cout << "computers score: " << computertotalscore <<end1;

   humanturn(humantotalscore);

   computerturn(computertotalscore);

}

while(win = 1);

if (win = 0) && (humantotalscore > computertotalscore))

{

     cout << "you win!";

}

else

{

     cout << " you lost!";

}

return 0;

}

int humanturn(int& humantotalscore)

{

    int currentscore = 0;

int lastroll;

char rollor hold;

whaile(rollorhold == 'r' )

{

    srand (time(NULL));

     lastroll = diceroll();

    if (lastroll == 1)

   {

        cout <<you rolled a 1, ending your turn. " << end1;

         break;

}

else

    {

        currentscore += latroll;

        cout << " you roll a " << lastroll << ". your score this turn is: " <<currentscore << end1;

        cout << " press r to roll again, or h to hold." << end1;

        cin >> rollorhold;

   }

}

while (rollorhold == 'h' )

{

     humantotalscore += currentscore;

      break;

}

return humantotalscore;

}

int computerturn (int& computertotalscore)

{

     int currentscore =0;

     int lastroll;

   cout << "computer's total score is: " << computertotalscore << "." << end1;

    whaile ((currentscore <= 20) && (currentscore != 1))

     {

           lastroll =diceroll();

          if (lastroll == 1)

           {

                cout << " the computer rolled a1, ending their turn." << end1;

                 break;

            }

             else

             {

              currentscore += lastroll;

               cout << " the computer rolls a " << lastroll << " . computer's score this turn is: "<< currentscore << end1;

             }

         }

    if(currentscore >= 20)

{

     computertotalscore += currentscore;

      cout << " after the computer's turn , they have gained an additional " << lastroll << " points." << end1;

   }

    return computertotalscore;

}

int dicroll()

{

int roll;

roll =(int)(rand()%6)+1;

return roll;

}

           

               

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