Step 1) Using the Die class defined in Chapter 4, write a class called PairOfDic
ID: 3592147 • Letter: S
Question
Step 1)
Using the Die class defined in Chapter 4, write a class called PairOfDice, composed of two Die objects. Include methods to set and get each of the individual die values, a method to roll the two die, and a method that returns the current sum of the two die values. The constructor should initialize each die to 1.
Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.
Note 1: Die class does not change.
Note 2: Make sure PairOfDice is working before you proceed with the remainder of assignment.
Here is the UML:
PairOfDice
- die1: Die
- die2: Die
+ PairOfDice(): PairOfDice
+ getDie1(): Die
+ getDie2(): Die
+ setDie1(int): void
+ setDie2(int): void
+sumDice(): int
+rollDice():int
+toString():String
Step 2)
Using the PairOfDice class, design and implement a program to play a game called Hog. In this game, a computer user (player 1) competes again a human user (player 2). On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 50 points before your opponent does.
If, on any turn, the player rolls a 1 on either die, all points accumulated for that round (turn) are forfeited and control of the dice moves to the other player. The human player may voluntarily turn over the dice after each roll. Therefore, the human player must decide to either roll again 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/turn.
I'll get you started by providing an algorithm (this is VERY high-level, you still have PLENTY of designing to do) and some of the variables/data you will need (feel free to add more or use less):
Data
char answer
PairOfDice dice
Die die1
Die die2
int computerTotalScore
humanTotalScore
computerRoundTotal //points for one turn
humanRoundTotal //points for one turn
Feel free to use more variables
____________________________________________________________________________________________
import java.util.*;
class Die
{
private int faceValue;
private final int MAX = 6;
public Die()
{
faceValue = 0;
}
public void roll()
{
Random rand = new Random();
int n = rand.nextInt(MAX) + 1;
setFaceValue(n);
}
public void setFaceValue(int n)
{
faceValue = n;
}
public int getValue()
{
return faceValue;
}
public String toString()
{
return getValue() + "";
}
}
--------------------------------------------------------------------------------------------------------
what is the code for Hog.java, Die.java and PairOfDice.java
Note 1: Die class does not change.
Note 2: Make sure PairOfDice is working before you proceed with the remainder of assignment.
Explanation / Answer
Die.java
import java.util.Random;
class Die {
private int faceValue;
private final int MAX = 6;
public Die() {
faceValue = 0;
}
public void roll() {
Random rand = new Random();
int n = rand.nextInt(MAX) + 1;
setFaceValue(n);
}
public void setFaceValue(int n) {
faceValue = n;
}
public int getValue() {
return faceValue;
}
public String toString() {
return getValue() + "";
}
}
__________________
PairOfDice.java
public class PairOfDice {
private Die die1;
private Die die2;
public int value1, value2, total;
public PairOfDice() {
die1 = new Die();
die2 = new Die();
}
public Die getDie1() {
return die1;
}
public Die getDie2() {
return die2;
}
public int sumDice() {
return (value1 + value2);
}
public int rollDice() {
die1.roll();
die2.roll();
value1 = die1.getValue();
value2 = die2.getValue();
return value1 + value2;
}
@Override
public String toString() {
return "Die 1=" + die1 + ", Die 2=" + die2;
}
}
______________________
Hog.java
import java.util.Random;
import java.util.Scanner;
public class Hog {
static Random r;
static Scanner sc = new Scanner(System.in);
static PairOfDice pod = new PairOfDice();
public static void main(String[] args) {
int user_tot_score = 0, comp_to_score = 0;
r = new Random();
System.out.println("Welcome to the game of Pig!");
while (true) {
System.out.println("****************************");
System.out.println("Current Status:");
System.out.println("Computer: " + comp_to_score);
System.out.println("You: " + user_tot_score);
user_tot_score = usersTurn(user_tot_score);
System.out.println("****************************");
System.out.println("Current Status:");
System.out.println("Computer: " + comp_to_score);
System.out.println("You: " + user_tot_score);
comp_to_score = computersTurn(comp_to_score);
}
}
private static int usersTurn(int user_tot_score) {
char ch;
int crand1 = 0, crand2 = 0, user_temp_tot = 0, user_turn_score = 0;
while (true) {
pod.rollDice();
crand1 = pod.getDie1().getValue();
crand2 = pod.getDie2().getValue();
if (crand1 == 1 || crand2 == 1) {
System.out.println(" You rolled: " + pod.toString());
user_temp_tot = 0;
user_turn_score = 0;
System.out.println("Busted");
break;
}
System.out.println(pod.toString());
user_turn_score += pod.sumDice();
user_temp_tot = user_tot_score + user_turn_score;
System.out.println("Current Round: " + user_turn_score + " Potential Total: " + user_temp_tot);
System.out.println("Take another turn (y/n)?");
ch = sc.next(".").charAt(0);
if (ch == 'y' || ch == 'Y')
continue;
else {
if (user_temp_tot >= 50) {
System.out.println("User Wins !");
user_tot_score = user_temp_tot;
System.out.println("The User's score is " + user_tot_score);
System.exit(0);
}
user_tot_score = user_temp_tot;
user_turn_score = 0;
user_temp_tot = 0;
break;
}
}
return user_tot_score;
}
private static int computersTurn(int comp_to_score) {
int urand1, urand2, com_turn_score = 0, com_temp_tot = 0;
while (true) {
pod.rollDice();
urand1 = pod.getDie1().getValue();
urand2 = pod.getDie2().getValue();
System.out.println(pod.toString());
if (urand1 == 1 || urand2 == 1) {
com_turn_score = 0;
com_temp_tot = 0;
System.out.println("Busted");
break;
}
System.out.println("Current Round:" + com_turn_score);
System.out.println("Potential Total:" + com_temp_tot);
com_turn_score += pod.sumDice();
com_temp_tot = comp_to_score + com_turn_score;
if (com_temp_tot >= 50) {
System.out.println("Computer Wins !");
comp_to_score = com_temp_tot;
System.exit(0);
}
if (com_turn_score >= 20) {
comp_to_score += com_turn_score;
com_turn_score = 0;
break;
} else {
continue;
}
}
return comp_to_score;
}
}
____________________
Output:
Welcome to the game of Pig!
****************************
Current Status:
Computer: 0
You: 0
You rolled: Die 1=3, Die 2=1
Busted
****************************
Current Status:
Computer: 0
You: 0
Die 1=6, Die 2=2
Current Round:0
Potential Total:0
Die 1=6, Die 2=1
Busted
****************************
Current Status:
Computer: 0
You: 0
Die 1=3, Die 2=3
Current Round: 6
Potential Total: 6
Take another turn (y/n)?
y
Die 1=2, Die 2=3
Current Round: 11
Potential Total: 11
Take another turn (y/n)?
y
Die 1=2, Die 2=5
Current Round: 18
Potential Total: 18
Take another turn (y/n)?
y
You rolled: Die 1=1, Die 2=3
Busted
****************************
Current Status:
Computer: 0
You: 0
Die 1=4, Die 2=5
Current Round:0
Potential Total:0
Die 1=2, Die 2=4
Current Round:9
Potential Total:9
Die 1=2, Die 2=4
Current Round:15
Potential Total:15
****************************
Current Status:
Computer: 21
You: 0
You rolled: Die 1=1, Die 2=3
Busted
****************************
Current Status:
Computer: 21
You: 0
Die 1=3, Die 2=5
Current Round:0
Potential Total:0
Die 1=4, Die 2=3
Current Round:8
Potential Total:29
Die 1=2, Die 2=3
Current Round:15
Potential Total:36
****************************
Current Status:
Computer: 41
You: 0
Die 1=3, Die 2=5
Current Round: 8
Potential Total: 8
Take another turn (y/n)?
y
Die 1=4, Die 2=2
Current Round: 14
Potential Total: 14
Take another turn (y/n)?
y
Die 1=6, Die 2=2
Current Round: 22
Potential Total: 22
Take another turn (y/n)?
y
Die 1=6, Die 2=6
Current Round: 34
Potential Total: 34
Take another turn (y/n)?
y
Die 1=3, Die 2=5
Current Round: 42
Potential Total: 42
Take another turn (y/n)?
y
Die 1=5, Die 2=5
Current Round: 52
Potential Total: 52
Take another turn (y/n)?
n
User Wins !
The User's score is 52
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.