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

The Game of Pig has been around for quite a while, requiring just one or two dic

ID: 3751017 • Letter: T

Question

The Game of Pig has been around for quite a while, requiring just one or two dice to play. Write a program that will allow you (the "player") to play against the computer. The rules of the game are: (java)

- The first player to accumulate a score of 100 or more wins.

- The human goes first.

- After one roll, a player has the choice to "hold" or to roll again. They can roll as many times as they like unless they get one or two sixes.

- You roll two dice. Certain conditions apply:

- If both dice are sixes the player's total score is set to zero and his turn is over. Ouch!

- If one dice is six, then your turn is over and your turn score is set to zero.

- If both dice match ("doubles"), other than sixes, then you gain twice the sum of the dice, and you must roll again. For example if you rolled two fours, you would gain 16 and then have to roll again.

- For any other dice combination, you just add the dice total to your turn score and you have the choice of rolling again.

- When your turn is over, either because you won, you chose not to roll again or you rolled a six, then your turn sum is added to your accumulated score.

More Info

- Put all methods in a single class along with a main method, where they all need to be declared static. At this point it does not matter if they are declared public or private. There is no required number of methods for this program. Methods should be compact, should minimize code duplication and have a single purpose. If a method is over about 25 lines in length, it is probably too long.

- You can only declare constant class attributes, declared as final static, if you need them. Other, non-constant data items must be moved between methods as arguments or return values. The only exception to this rule is the random number generator object as described below.

In Java

Explanation / Answer

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

public class Pig
{
private static int plyer_1 = 1;
private static int plyr_2 = 2;
private static int WinPoints = 21;
  
private int PLAYER_turn;
  
private int p1_totalpoints;
private int p2_totpoints;
  
private String OverallGameState() {
return "It is Player " + PLAYER_turn + "'s turn. Player 1 has " +
p1_totalpoints + " points and Player 2 has " +
p2_totpoints + " points.";
}
  
private void roll_dice(int roundPoints){
System.out.println("Enter 0 to roll_dice or 1 to hold.");
Scanner reader = new Scanner(System.in);
int dec = reader.nextInt();
if (dec == 0) {
Random randm = new Random();
int dice_result = randm.nextInt(6) + 1;
if (dice_result == 1) {
System.out.println("Player " + PLAYER_turn +
"rolled 1 and busted.");
} else {
System.out.println("Player " + PLAYER_turn +
" rolled " + dice_result);
int newRoundPoints = roundPoints + dice_result;
roll_dice(newRoundPoints);
}
} else {
if (PLAYER_turn == 1) {
p1_totalpoints = p1_totalpoints + roundPoints;
} else {
p2_totpoints = p2_totpoints + roundPoints;
}
  
System.out.println("Player " + PLAYER_turn + "earned " +
roundPoints + " points.");
}
}
  
public static void TestRandom(){
Random randm = new Random();
int dice_result = randm.nextInt(6) + 1;
System.out.println(dice_result);
}
  
private void Round() {
System.out.println(OverallGameState());
roll_dice(0);
PLAYER_turn = 2;
System.out.println("Player 2's turn!");
roll_dice(0);
PLAYER_turn = 1;
System.out.println("Player 1's turn!");
}
  
private boolean IsGameOver(){
boolean player1Won = p1_totalpoints >= WinPoints;
boolean player2Won = p2_totpoints >= WinPoints;
return player1Won || player2Won;
}


public Pig()
{
PLAYER_turn = 1;
p1_totalpoints = 0;
p2_totpoints = 0;
  
while (!IsGameOver()) {
Round();
}
  
}
}

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