Pig is a dice game with the following rules: Two players race to reach 100 point
ID: 3760836 • Letter: P
Question
Pig is a dice game with the following rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn, the player is faced with two decisions: . roll - If the player rolls a 1: the player scores nothing and it becomes the opponent's turn. 2 - 6: the number is added to the player's turn total and the player's turn continues. . hold - The turn total is added to the player's score and it becomes the opponent's turn. The first player to score 100 or more points wins. For example, the first player, Ann, begins a turn with a roll of 5. Ann could hold and score 5 points, but chooses to roll again. Ann rolls a 2, and could hold with a turn total of 7 points, but chooses to roll again. Ann rolls a 1, and must end her turn without scoring. The next player, Bob, rolls the sequence 4-5-3-5-5, after which he chooses to hold, and adds his turn total of 22 points to his score. Write a C++ program in which the computer will play the user in a game of Pig. A key decision to build strategy for the computer player is how large a turn total should be risked to possibly get an even larger total. Use ''hold at 20'' strategy for your computer player (unless holding will win the game). (This will maximize the expected number of points per turn, though it does not maximize the expected probability of winning.) Include a welcome message/directions when the program first runs. After the welcome message, include a prompt that asks them to hit any key when they are ready to go on. Clear the screen using the command: system (''cis'') ; Include appropriate user feedback, helpful prompts, output each dice roll, the final score gain, and the score at the end of each turn for the players, label output, be creative! MAKE THE GAME FUN! Include error checking on the input (only check for valid values, not invalid data type). Print an error message and allow the user to re-enter the input again. Make your code self-documenting, using appropriate indentation, style, and comments. After the game is over, ask the player if they want to play again.Explanation / Answer
import java.util.Scanner; public class PigGame { public static boolean gameover = false, playersturn = true, playgame = true; public int computerScore = 0, humanScore = 0; public static Scanner kbd = new Scanner(System.in); public PigGame(String name) { System.out.println("Welcome " + name + " to the game Pig."); System.out.println("Start game?(Y/N)"); if(kbd.nextLine().equalsIgnoreCase("y")) { while(playgame) { while(humanScore >= 30 && humanScore > computerScore) { System.out.println("The Computer Score is " + computerScore + " and your score is " + humanScore + "."); System.out.println("Congratulations " + name + ", You WON!"); System.out.println("Would you like to play agian?(Y/N)"); if(kbd.nextLine().equalsIgnoreCase("y")) { computerScore = 0; humanScore = 0; new PigGame(name); } else { System.exit(0); } } while(computerScore >= 30 && computerScore > humanScore) { System.out.println("The Computer's Score is " + computerScore + " and your score is " + humanScore + "."); System.out.println("Sorry you didn't win..."); System.out.println("Would you like to play agian?(Y/N)"); if(kbd.nextLine().equalsIgnoreCase("y")) { computerScore = 0; humanScore = 0; new PigGame(name); } else { System.exit(0); } } while(playersturn) { humanScore += new Logic().roll(playersturn); } while(playersturn != true) { computerScore += new Logic().roll(playersturn); } } } else { System.exit(0); } } public static void main(String args[]) { System.out.print("Please enter your name: "); new PigGame(kbd.nextLine()); } } class Logic { boolean keepgoing = true; int diceRoll = 0, turnPoints = 0, maxScore = 30; Scanner kbd = new Scanner(System.in); public int roll(boolean playersturn) { if(playersturn) { while(keepgoing) { diceRoll = (int)(Math.random() * 6) + 1; if(diceRoll == 1 ) { System.out.println("Sorry you rolled: " + diceRoll); System.out.println("It is the computer's turn:"); turnPoints= 0; PigGame.playersturn = false; keepgoing = false; } else { System.out.println("You rolled: " + diceRoll); turnPoints+=diceRoll; System.out.println("Do you wish to roll(r) or hold(h)?"); if (kbd.nextLine().equalsIgnoreCase("h")) { System.out.println("It is the computer's turn:"); PigGame.playersturn = false; playersturn = false; keepgoing = false; } } } } else if (playersturn != true) { while(keepgoing) { diceRoll = (int)(Math.random() * 6) + 1; if(diceRoll == 1 ) { System.out.println("The computer rolled: " + diceRoll); turnPoints = 0; PigGame.playersturn = true; keepgoing = false; } else { System.out.println("The computer rolled: " + diceRoll); turnPoints+=diceRoll; if (turnPoints >= 20) { PigGame.playersturn = true; keepgoing = false; } } } } return turnPoints; } }