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

Problem 2. The game of Pig is a simple two-player dice game in which the first p

ID: 3758857 • Letter: P

Question

Problem 2. 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.

Explanation / Answer

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Runner {
   static char turn;
   /*
   * score index : 0-> Human 1-> Computer
   */
   static int[] score = { 0, 0 };

   Random randomGenerator = new Random();

   int getDiceRoll() {
       return randomGenerator.nextInt(6);
   }

   static void updateTurn(char t) {
       if (t == 'H') {
           System.out
                   .println("Your Turn .. Press (R) to Roll | (H) to Hold : ");
       }
       turn = t;
   }

   static boolean isGameOver() {
       if (score[0] >= 100 || score[1] >= 100) {
           return true;
       }
       return false;
   }

   static void updateScore(int index, int rollValue) {
       score[index] = score[index] + rollValue;
   }

   static void printComputerTurnRollDetails(List<Integer> rollValues) {
       printScores();
       // if (!rollValues.isEmpty()) {
       // System.out.println(" Computer Turn Roll Values are : ");
       // for (Integer rollValue : rollValues) {
       // System.out.print(rollValue + " ");
       // }
       // }
   }

   static void printScores() {
       System.out.println(" Human : " + score[0]);
       System.out.println("Computer : " + score[1]);
   }

   public static void main(String[] args) throws Exception {
       Runner run = new Runner();
       Scanner in = new Scanner(System.in);
       System.out.println("Starting the Game............");

       // Giving the first turn to Human
       System.out.println("--------------Your Turn Starting--------------");
       updateTurn('H');

       do {
           int rollValue = 0;
          
           if (turn == 'H') {
               while (in.next().equals("R")) {
                   rollValue = run.getDiceRoll();
                   System.out.println("Roll Value: " + rollValue);
                   if (rollValue == 1) {
                       System.out.println("--------------Your Turn Ended--------------");
                       break;
                   } else {
                       updateScore(0, rollValue);
                       System.out
                               .println("Your Turn again ... Press (R) to Roll | (H) to Hold : ");
                   }
               }
               updateTurn('C');
               printScores();
           } else {
               // Computer turn
               System.out.println("--------------Computer Turn Starting--------------");
               int turnTotalRollValue = 0;
               List<Integer> computerRollValues = new ArrayList<Integer>();
               do {
                   rollValue = run.getDiceRoll();
                   System.out.println("Computer's Roll Value: " + rollValue);
                   if (rollValue == 1) {
                       System.out.println("--------------Computer Turn Ended--------------");
                       printComputerTurnRollDetails(computerRollValues);
                       updateTurn('H');
                       break;
                   } else {
                       turnTotalRollValue = turnTotalRollValue + rollValue;
                       updateScore(1, rollValue);
                       computerRollValues.add(rollValue);
                   }
               } while (turnTotalRollValue <= 20);
           }
       } while (!isGameOver());

       System.out.println("Game Ended..");
       printScores();

       in.close();
   }
}

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