Question: Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from
Hello,
Could you please help me code this program in Java? It is important that all rules are carefully followed.
Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a 1, all points accumulated for that round are forfeited and control of the dice moves to the other player. If the player rolls two 1s in one turn, the player loses all points accumulated thus far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. Therefore the player must decide to either roll again (be a pig) and risk losing points or relinquish control of the dice, possibly allowing the other player to win. Implement the computer player such that it always relinquishes the dice after accumulating 20 or more points in any given round.
This is the Die class:
public class Die { private final int MAX = 6; //Maximum facevalue. private int faceValue; //Current value showing on the die. //Constructor: Sets the initial face value. public Die() { faceValue = 1; } //Rolls the die and returns the result. public int roll() { faceValue = (int)(Math.random() * MAX ) + 1; return faceValue; } //Face value mutator. public void setFaceValue(int value) { faceValue = value; } //Face value accessor. public int getFaceValue() { return faceValue; } public String toString() { String result = Integer.toString(faceValue); return result; } }
This is the PairOfDice class:
public class PairOfDice { Die die1, die2; //Creates two objects of the die class. public PairOfDice() { die1 = new Die(); die2 = new Die(); } //Initializes two objects of the die class. public int getDie1Value() { return die1.getFaceValue(); } //A method to get the FaceValue of the first die object. public int getDie2Value() { return die2.getFaceValue(); } //A method to get the FaceValue of the second die object. public void setDie1Value(int v1) { die1.setFaceValue(v1); } //A method to set the FaceValue of the first die object. public void setDie2Value(int v2) { die2.setFaceValue(v2); } //A method to set the FaceValue of the second die object. public void rollTheDice() { die1.roll(); die2.roll(); //A method to roll both of the dice. } public int sumOfTheDice() { return getDie1Value() + getDie2Value(); } //A method to print out the sum of the two dice's values. }
Thank you
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
