Write a class named Die with the attribute face that can have a value between 1
ID: 3766146 • Letter: W
Question
Write a class named Die with the attribute face that can have a value between 1 and 6(inclusive). Include a constructor that randomly assigns a value to a die object. You may use the Math.random( ) method to generate the random numbers. Add a method in the class to return a die’s value. Now, write an application that “randomly” throws five dice for the player and five dice for the computer. The application first displays the values; then it decides the winner based on the following hierarchy of die values. Any higher combination beats the lower one. For example five of a kind beats four of a kind; four of a kind beats three; three of a kind beats two. When both the player and the computer have the same combination, the higher value wins.
Explanation / Answer
// Complete Program:
// File: Die.java
public class Die
{
private int face;
public Die()
{
face = 1 + (int) (Math.random() * 100) % 6;
}
public int getFace()
{
return face;
}
}
// File: FiveDiceGame.java
import java.util.*;
public class FiveDiceGame
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int compNums = 0;
int playerNums;
String choice;
Die[] computer = new Die[5];
Die[] player = new Die[5];
System.out.println("The highest combination wins the game.");
System.out.println("5 of a kind beats 4 of a kind");
System.out.println("4 of a kind beats 3 of a kind");
System.out.println("3 of a kind beats 2 of a kind");
System.out.println("2 of a kind beats 1 of a kind ");
do
{
// computer
System.out.print("Computer rolled: ");
for(int i = 0; i < computer.length; ++i)
{
computer[i] = new Die();
compNums = computer[i].getFace();
System.out.print(compNums + " ");
}
// player
System.out.print(" Player rolled: ");
for(int i = 0; i < player.length; ++i)
{
player[i] = new Die();
playerNums = player[i].getFace();
System.out.print(playerNums + " ");
}
int compHighComb = findHighestCombinations(computer);
int playerHighComb = findHighestCombinations(player);
System.out.println(" Computer's highest combination: " + compHighComb);
System.out.println("Player's highest combination: " + playerHighComb);
if(compHighComb > playerHighComb)
System.out.println("Computer wins the game!");
else if(compHighComb < playerHighComb)
System.out.println("Player wins the game!");
else
System.out.println("Tie - No one wins the game!");
System.out.print(" Do you want to play again? (Y/YES): ");
choice = input.next();
System.out.println();
}while(choice.equalsIgnoreCase("Y") || choice.equalsIgnoreCase("YES"));
}
public static int findHighestCombinations(Die[] dies)
{
int highest = 0;
int count;
for(int i = 0; i < dies.length; i++)
{
count = 0;
for(int j = 0; j < dies.length; j++)
{
if(dies[i].getFace() == dies[j].getFace())
count++;
}
if(count > highest)
highest = count;
}
return highest;
}
}
Sample Ouptut:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.