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

The game of Pig is a simple two-player dice game in which the first player to re

ID: 3759057 • Letter: T

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 fair six-sided die: • If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. • If the player rolls 2 through 6, then he or she can either: – ROLL AGAIN or – HOLD. At this point, the sum of all rolls is added to the player’s score and it becomes the other player’s turn. In a class called H4P2, write a program that plays the game of Pig, where one player is a human and the other is the computer. When it is the human’s turn, the program should show the score of both players and the previous roll. 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 when it is the computer’s turn until it has accumulated 20 or more points for that turn, then hold (of course, if the computer rolls a 1, then it is the humans’ turn again). If the computer wins or rolls a 1, then the turn ends immediately. Allow the human to roll first.

Write a program for java.

I can only use the simple do-while state, if-else statement, and while statement. No list and arraylist.

Explanation / Answer

import java.util.Scanner;
import java.util.Random;

public class Game_Pig
{
   public static void main(final String[] args)
   {
      int humanScore = 0;
      int humanTotal = 0;
      int computerScore = 0;
      int subtotal = 0;
      int humanRoll, computerRoll;
      final Scanner keyboard = new Scanner(System.in);
      final Random rand = new Random();
      char choice = 'h';
      String input;
      System.out.println("Welcome to the game of Pig! ");


      while (computerScore < 100 && humanScore < 100)
      {
         do
         {
            humanRoll = rand.nextInt(6) + 1;
            System.out.println("You rolled: " + humanRoll);
            if (humanRoll == 1)
            {
               humanScore = 0;
               System.out.print("You lose your turn! ");
               System.out.print("Your total is " + humanTotal);
               break;
            }
            else
            {
               humanScore += humanRoll;
               System.out.print("Your turn score is " +
                                humanScore);
               System.out.println(" If you hold you will have " +
                                  humanScore + " points.");
               System.out.println("Enter 'r' to roll " +
                                  "again, 'h' to hold.");
               input = keyboard.nextLine();
               choice = input.charAt(0);
              
               if (choice == 'h')
               {
                 break;
               }
            }
         }while (humanRoll != 1 && choice != 'h');
           
            humanTotal += humanScore;
            System.out.println("Your score is " +
                               humanTotal);
            humanScore = 0;
        
         System.out.println();
         System.out.println("It is the computer's turn.");
         do
         {
            computerRoll = rand.nextInt(6) + 1;
            System.out.println("The computer rolled: " +
                               computerRoll);
            if (computerRoll == 1)
            {
               computerScore = 0;
               System.out.print("The computer lost its turn!");
               System.out.println(" Computer total is " +
                                  subtotal);
               break;
            }
            else
            {
               computerScore += computerRoll;
               if (computerScore >= 20 || subtotal >= 100)
               {
                  System.out.println("The computer holds");
                  break;
               }
            }
        
          }while (computerRoll != 1 && subtotal <=20);
             subtotal += computerScore;
             System.out.println("The computer's score is " +
                                subtotal + " ");
             computerScore = 0;
      }
      while (humanTotal >= 100)
      {
         System.out.println("YOU WIN!");
      }
      while (subtotal >= 100)
      {
         System.out.println("THE COMPUTER WINS!");
      }
   }
}

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