java, using the PairOfDice class from PP 4.9, design and implement a class to pl
ID: 3889543 • Letter: J
Question
java, 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 1’s 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.
Explanation / Answer
PairOfDice.java
// creating class PairOfDice
public class PairOfDice
{
// Declare two Die objects as instance data
private Die die1 = new Die();
private Die die2 = new Die();
// Set method
public void setValue(int facevalue1, int facevalue2)
{
die1.setFaceValue(facevalue1);
die2.setFaceValue(facevalue2);
}
// Get method
public int getValueOfDie1()
{
return die1.getFaceValue();
}
public int getValueOfDie2()
{
return die2.getFaceValue();
}
// Roll the dice method
public void roll()
{
die1.roll();
die2.roll();
}
// Sum of the two die values
public int sum()
{
return getValueOfDie1() + getValueOfDie2();
}
}
Die.java
// it showing faces values between 1 and 6
public class Die
{
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
// Constructor: Sets the initial face value of this die.
public Die()
{
faceValue = 1;
}
// Computes a new face value for this die and returns the result.
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
// Face value mutator
public void setFaceValue (int value)
{
if (value > 0 && value <= MAX)
faceValue = value;
}
// Face value accessor.
public int getFaceValue()
{
return faceValue;
}
// Returns a string representation of this die.
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
Pig.java
// header file
import java.util.Scanner;
// creat class name of Pig
public class Pig
{
public static void main(String[] args)
{
// declaring local variables and assign values
int myPoints = 0;
int compPoints = 0;
int compRandomPick = 0;
int myExit =0 , compExit = 0;
String next;
Scanner scan = new Scanner(System.in);
System.out.println();
System.out.println("Hi, welcome to the Pig Game! Please hit enter to continue.");
// getting input from user
next = scan.nextLine();
System.out.println("Please roll a die.");
next = scan.nextLine();
// creating two objects
PairOfDice myDie = new PairOfDice();
PairOfDice compDie = new PairOfDice();
game:
// checking the condition using while loop
while(myPoints != 100 || compPoints != 100)
{
do
{
// calling function of roll()
myDie.roll();
// checking the condition using if loop
if (myDie.getValueOfDie1() == 1 && myDie.getValueOfDie2() == 1)
{
//assigning points equal to zero
myPoints = 0;
myExit = 1;
// dislpaying desired output
System.out.println("Your Die1: " + myDie.getValueOfDie1() + " " + "Your Die2: " + myDie.getValueOfDie2());
System.out.println("Opps, you got the pig.");
System.out.println("You lose all your accumulated points.");
System.out.println("Your points is back to " + myPoints + ".");
System.out.println("The dice control is now passed to the computer.");
System.out.println();
}
else if (myDie.getValueOfDie1() == 1 || myDie.getValueOfDie2() == 1)
{
myExit = 1;
System.out.println("Your Die1: " + myDie.getValueOfDie1() + " " + "Your Die2: " + myDie.getValueOfDie2());
System.out.println("Opps, you got a 1.");
System.out.println("You lose the points accumulated in this round.");
System.out.println("Your current accumulated points are " + myPoints + ".");
System.out.println("The dice control is now passed to the computer.");
System.out.println();
}
else if(myDie.getValueOfDie1() != 1 && myDie.getValueOfDie2() !=1)
{
myPoints += myDie.sum();
System.out.println("Your Die1: " + myDie.getValueOfDie1() + " " + "Your Die2: " + myDie.getValueOfDie2());
System.out.println("Congrats, you got " + myDie.sum() + "!");
System.out.println("Your current accumulated points are " + myPoints + ".");
if(myPoints >= 100)
{
break;
}
System.out.println();
System.out.println("Hit 'Y'' if you want to roll the next round.");
System.out.println("Or hit other buttons if you want to relinquish the die control.");
next = scan.nextLine();
if(next.equalsIgnoreCase("Y") || next.equalsIgnoreCase("y"))
myExit = 0;
else
myExit = 1;
}
}
while(myExit == 0 && (myPoints < 100));
if(myPoints >= 100)
{
break game;
}
do
{
compDie.roll();
if (compDie.getValueOfDie1() == 1 && compDie.getValueOfDie2() == 1)
{
compPoints = 0;
compExit = 1;
System.out.println("Computer Die1: " + compDie.getValueOfDie1() + " " + "Computer Die2: " + compDie.getValueOfDie2());
System.out.println("Computer got the pig.");
System.out.println("Computer lose all your accumulated points.");
System.out.println("Computer's point is back to " + compPoints + ".");
System.out.println("The dice control is now passed to the player.");
System.out.println();
}
else if (compDie.getValueOfDie1() == 1 || compDie.getValueOfDie2() == 1)
{
compExit = 1;
System.out.println("Computer Die1: " + compDie.getValueOfDie1() + " " + "Computer Die2: " + compDie.getValueOfDie2());
System.out.println("Computer got a 1.");
System.out.println("Computer lose the points accumulated in this round.");
System.out.println("Computer's current accumulated points are " + compPoints + ".");
System.out.println("The dice control is now passed to the player.");
System.out.println();
}
else if(compDie.getValueOfDie1() != 1 && compDie.getValueOfDie2() !=1)
{
compPoints += compDie.sum();
System.out.println("Computer Die1: " + compDie.getValueOfDie1() + " " + "Computer Die2: " + compDie.getValueOfDie2());
System.out.println("Computer got " + compDie.sum() + "!");
System.out.println("Computer current accumulated points are " + compPoints + ".");
System.out.println();
if(compPoints >= 100)
{
break;
}
compRandomPick = (int)(Math.random() * 2);
if(compPoints >= 20 || compRandomPick == 0)
{
compExit = 1;
System.out.println("Computer relinquishes the die control.");
}
else
System.out.println("Computer is rolling the next round.");
System.out.println();
}
}
while(compExit == 0 && compPoints < 100);
if(compPoints >= 100)
{
break game;
}
}
if(myPoints >= 100)
System.out.println("Congrats! You win the game!");
else if(compPoints >= 100)
System.out.println("Opps, the computer win the game.");
System.out.println();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.