Part 1. Using the Die class defined in class, write a class called PairOfDice, c
ID: 3857822 • Letter: P
Question
Part 1.
Using the Die class defined in class, write a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.
Part 2
Using the PairOfDice class you wrote, 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 the points accumulated thus far in the game and losescontrol 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
Part 1: -
Die.java
public class Die {
private int faceValue;
private final int MAX = 6;
public Die() {
faceValue = 1;
}
// Math.random creates random integers from 1 - 6
public void roll() {
faceValue = (int) (Math.random() * MAX) + 1;
}
// Sets the face value
public void setFaceValue(int value) {
faceValue = value;
}
// Gets the face value using the setFaceValue method
public int getFaceValue() {
return faceValue;
}
// Converts values into a String
public String toString() {
String result = "You rolled a " + faceValue;
return result;
}
}// end class Die
PairOfDice.java
public class PairOfDice {
private Die d1, d2;
private int value1, value2, total;
// -----------------------------------------------------------------
// Creates two six-sided Die objects, both with an initial
// face value of one.
// -----------------------------------------------------------------
public PairOfDice() {
d1 = new Die();
d2 = new Die();
value1 = 1;
value2 = 1;
total = value1 + value2;
}
// -----------------------------------------------------------------
// Rolls both dice and returns the combined result.
// -----------------------------------------------------------------
public void roll() {
d1.roll();
value1 = d1.getFaceValue();
d2.roll();
value2 = d2.getFaceValue();
total = value1 + value2;
}
// -----------------------------------------------------------------
// Returns the current combined dice total.
// -----------------------------------------------------------------
public int getDiceSum() {
total = getDie1() + getDie2();
return total;
}
// -----------------------------------------------------------------
// Returns the current value of the first die.
// -----------------------------------------------------------------
public int getDie1() {
return value1;
}
// -----------------------------------------------------------------
// Returns the current value of the second die.
// -----------------------------------------------------------------
public int getDie2() {
return value2;
}
// -----------------------------------------------------------------
// Sets the FaceValue of the first die.
// -----------------------------------------------------------------
public void setDie1(int value) {
d1.setFaceValue(value);
}
// -----------------------------------------------------------------
// Sets the FaceValue of the second die.
// -----------------------------------------------------------------
public void setDie2(int value) {
d2.setFaceValue(value);
}
// -----------------------------------------------------------------
// Sets the FaceValue of the second die.
// -----------------------------------------------------------------
public String toString() {
String result = "You rolled a " + total;
return result;
}
}
RollingDice2.java
public class RollingDice2 {
// ------------------------------------------------------
// Creates two Die objects and rolls them several times
// ------------------------------------------------------
public static void main(String[] args) {
Die d1, d2;
int sum;
// Creates two Die objects
d1 = new Die();
d2 = new Die();
d1.roll();
d2.roll();
System.out.println(" Die One: " + d1 + " , Die Two: " + d2);
sum = d1.getFaceValue() + d2.getFaceValue();
System.out.println(" Sum: " + sum);
}// end main
}// end class RollingDice
Sample Run: -
Die One: You rolled a 1 , Die Two: You rolled a 2
Sum: 3
Part 2: -
DiceGame.java
import java.util.Scanner;
public class DiceGame {
public static void main(String[] args) {
// Rules of the Game
System.out.println("______________________________________");
System.out.println("/ Rules of the Game /");
System.out.println("/ ----------------- /");
System.out.println("/ 1)It's you vs computer. /");
System.out.println("/ 2)You play by rolling the dice. /");
System.out.println("/ 3)The first player to reach 100 /");
System.out.println("/ points wins. /");
System.out.println("/ 4)When a player rolls a 1 /");
System.out.println("/ the turn is over. /");
System.out.println("/ 5)The computer's turn is over /");
System.out.println("/ when turn total reach 20 points /");
System.out.println("/ in a single turn. /");
System.out.println("______________________________________");
PairOfDice d1 = new PairOfDice(); // Creating PairOfDice object
int turnTotal = 0;
int computerTotal = 0; // your total
int playerTotal = 0; // computer's total
int turnOver = 1; // when to give up die
int winner = 100; // amount to be reached before winning
Scanner in = new Scanner(System.in);
String answer; // named of what will take answer from user
// first do-while loop is for repeating the change between user and
// computer
do {
if (playerTotal <= winner && computerTotal <= winner) {
System.out.println("Your turn.");
// do-while loop for the player's turn.
do {
System.out.println("Type 'y' if ready and 'n' to end turn.");
answer = in.next();
if (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner) {
d1.roll();
System.out.println("You rolled Dice 1 with value : "+d1.getDie1());
System.out.println("You rolled Dice 2 with value : "+d1.getDie2());
System.out.println(d1);
// if and else statement to figure out whether user's
// turn is over or not.
if (d1.getDie1() == turnOver || d1.getDie2() == turnOver) {
System.out.println("You rolled a 1. Your turn is over.");
System.out.println(
"Please type 'done' when you are ready to turn the dice over to the Computer.");
answer = in.next();
} else {
turnTotal = turnTotal + d1.getDiceSum();
playerTotal = playerTotal + d1.getDiceSum();
System.out.println("Your Turn Total: " + turnTotal);
System.out.println("Your Grand Total: " + playerTotal);
}
}
}
while (answer.equalsIgnoreCase("y") && playerTotal <= winner && computerTotal <= winner);
turnTotal = 0; // turntotal assigned to 0 again.
System.out.println();
System.out.println("Your Grand Total is: " + playerTotal);
System.out.println("The Computer's Grand Total is: " + computerTotal);
System.out.println();
// Begin the Computer's turn
int endComputerTurn = 20;// when to end computer's turn
turnOver = 1; // what die equals for turn to be over
int answercomp = 1;
do {
if (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner
&& computerTotal <= winner) {
d1.roll();
System.out.println("Computer rolled Dice 1 with value : "+d1.getDie1());
System.out.println("Computer rolled Dice 2 with value : "+d1.getDie2());
System.out.println(d1);
if (d1.getDie1() == turnOver || d1.getDie2() == turnOver) {
System.out.println("The Computer rolled a 1. Their turn is over.");
answercomp = 0;
}
else {
turnTotal = turnTotal + d1.getDiceSum();
computerTotal = computerTotal + d1.getDiceSum();
System.out.println("The Computer's Turn Total is: " + turnTotal);
System.out.println("The Computer's Grand Total is: " + computerTotal);
}
}
}
while (turnTotal <= endComputerTurn && answercomp == 1 && playerTotal <= winner
&& computerTotal <= winner);
turnTotal = 0; // turntotal assigned to 0 again.
if (playerTotal <= winner || computerTotal <= winner) {
System.out.println();
System.out.println("The Computer's Grand Total is: " + computerTotal);
System.out.println("Your Grand Total is: " + playerTotal);
System.out.println();
}
else {
System.out.println();
System.out.println();
}
}
}
while (playerTotal <= winner && computerTotal <= winner);
// if-else statements to check if there is a winner
if (playerTotal >= winner)
System.out.println("You win!");
else
System.out.println("You lose ): ");
}
}
Sample Run: -
______________________________________
/ Rules of the Game /
/ ----------------- /
/ 1)It's you vs computer. /
/ 2)You play by rolling the dice. /
/ 3)The first player to reach 100 /
/ points wins. /
/ 4)When a player rolls a 1 /
/ the turn is over. /
/ 5)The computer's turn is over /
/ when turn total reach 20 points /
/ in a single turn. /
______________________________________
Your turn.
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 2
You rolled Dice 2 with value : 2
You rolled a 4
Your Turn Total: 4
Your Grand Total: 4
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 4
You rolled Dice 2 with value : 5
You rolled a 9
Your Turn Total: 13
Your Grand Total: 13
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 6
You rolled Dice 2 with value : 1
You rolled a 7
You rolled a 1. Your turn is over.
Please type 'done' when you are ready to turn the dice over to the Computer.
done
Your Grand Total is: 13
The Computer's Grand Total is: 0
Computer rolled Dice 1 with value : 1
Computer rolled Dice 2 with value : 6
You rolled a 7
The Computer rolled a 1. Their turn is over.
The Computer's Grand Total is: 0
Your Grand Total is: 13
Your turn.
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 4
You rolled Dice 2 with value : 5
You rolled a 9
Your Turn Total: 9
Your Grand Total: 22
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 2
You rolled Dice 2 with value : 1
You rolled a 3
You rolled a 1. Your turn is over.
Please type 'done' when you are ready to turn the dice over to the Computer.
done
Your Grand Total is: 22
The Computer's Grand Total is: 0
Computer rolled Dice 1 with value : 2
Computer rolled Dice 2 with value : 1
You rolled a 3
The Computer rolled a 1. Their turn is over.
The Computer's Grand Total is: 0
Your Grand Total is: 22
Your turn.
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 5
You rolled Dice 2 with value : 3
You rolled a 8
Your Turn Total: 8
Your Grand Total: 30
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 2
You rolled Dice 2 with value : 6
You rolled a 8
Your Turn Total: 16
Your Grand Total: 38
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 6
You rolled Dice 2 with value : 4
You rolled a 10
Your Turn Total: 26
Your Grand Total: 48
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 2
You rolled Dice 2 with value : 5
You rolled a 7
Your Turn Total: 33
Your Grand Total: 55
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 4
You rolled Dice 2 with value : 5
You rolled a 9
Your Turn Total: 42
Your Grand Total: 64
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 5
You rolled Dice 2 with value : 4
You rolled a 9
Your Turn Total: 51
Your Grand Total: 73
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 6
You rolled Dice 2 with value : 5
You rolled a 11
Your Turn Total: 62
Your Grand Total: 84
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 2
You rolled Dice 2 with value : 1
You rolled a 3
You rolled a 1. Your turn is over.
Please type 'done' when you are ready to turn the dice over to the Computer.
done
Your Grand Total is: 84
The Computer's Grand Total is: 0
Computer rolled Dice 1 with value : 1
Computer rolled Dice 2 with value : 6
You rolled a 7
The Computer rolled a 1. Their turn is over.
The Computer's Grand Total is: 0
Your Grand Total is: 84
Your turn.
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 4
You rolled Dice 2 with value : 3
You rolled a 7
Your Turn Total: 7
Your Grand Total: 91
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 1
You rolled Dice 2 with value : 5
You rolled a 6
You rolled a 1. Your turn is over.
Please type 'done' when you are ready to turn the dice over to the Computer.
done
Your Grand Total is: 91
The Computer's Grand Total is: 0
Computer rolled Dice 1 with value : 2
Computer rolled Dice 2 with value : 2
You rolled a 4
The Computer's Turn Total is: 4
The Computer's Grand Total is: 4
Computer rolled Dice 1 with value : 3
Computer rolled Dice 2 with value : 2
You rolled a 5
The Computer's Turn Total is: 9
The Computer's Grand Total is: 9
Computer rolled Dice 1 with value : 6
Computer rolled Dice 2 with value : 6
You rolled a 12
The Computer's Turn Total is: 21
The Computer's Grand Total is: 21
The Computer's Grand Total is: 21
Your Grand Total is: 91
Your turn.
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 4
You rolled Dice 2 with value : 3
You rolled a 7
Your Turn Total: 7
Your Grand Total: 98
Type 'y' if ready and 'n' to end turn.
y
You rolled Dice 1 with value : 6
You rolled Dice 2 with value : 6
You rolled a 12
Your Turn Total: 19
Your Grand Total: 110
Your Grand Total is: 110
The Computer's Grand Total is: 21
The Computer's Grand Total is: 21
Your Grand Total is: 110
You win!