A. In Chapter 4, you created a Die application that randomly \"throws\" five dic
ID: 3668985 • Letter: A
Question
A. In Chapter 4, you created a Die application that randomly "throws" five dice for the computer and five dice for the player. The application displays the values. Modify the application to decide the winner based on the following hierarchy of Die values. Any higher combination beats a lower onen - for example, five of a kind beats four of a kind.
Five of a kind
Four of a kind
Three of a kind
A pair
For this game, the dice values do not count; for example, if both players have three of a kind, it's a tie, no matter what the values of the three dice are. Additionally, the game does not recognize a ful lhouse (three of a kind plus two of a kind). Save the application as FiveDice2.java
B. Improve the FiveDice2 game so that when both players have the same combination of dice, the higher value wins. For example, two 6s beats two 5s. Save the applicatiion as FiveDice3.java.
Explanation / Answer
package com.he.capillary;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
public class FiveDice2 {
public static void main(String[] args) {
System.out.println("Starting game : ");
Map<Integer, Integer> player = new HashMap<Integer, Integer>();
Map<Integer, Integer> computer = new HashMap<Integer, Integer>();
for (int i = 0; i < 6; i++) {
int dice = new Random().nextInt((6 - 1) + 1) + 1;
if (player.get(dice) != null) {
player.put(dice, player.get(dice) + 1);
} else {
player.put(dice, 1);
}
}
for (int i = 0; i < 6; i++) {
int dice = new Random().nextInt((6 - 1) + 1) + 1;
if (computer.get(dice) != null) {
computer.put(dice, computer.get(dice) + 1);
} else {
computer.put(dice, 1);
}
}
int playerHeighestCount = -1;
int playerHeighestValue = -1;
for (Entry<Integer, Integer> entry : player.entrySet()) {
if (playerHeighestCount < entry.getValue()) {
playerHeighestCount = entry.getValue();
playerHeighestValue = entry.getKey();
}
}
int computerHeighestCount = -1;
int computerHeighestValue = -1;
for (Entry<Integer, Integer> entry : computer.entrySet()) {
if (computerHeighestCount < entry.getValue()) {
computerHeighestCount = entry.getValue();
computerHeighestValue = entry.getKey();
}
}
System.out.println(player + " " + computer);
if (computerHeighestCount > playerHeighestCount) {
System.out.println("Computer Won");
} else if (computerHeighestCount < playerHeighestCount) {
System.out.println("Player Won");
} else {
System.out.println("Tie");
}
}
}
package com.he.capillary;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;
public class FiveDice3 {
public static void main(String[] args) {
System.out.println("Starting game : ");
Map<Integer, Integer> player = new HashMap<Integer, Integer>();
Map<Integer, Integer> computer = new HashMap<Integer, Integer>();
for (int i = 0; i < 6; i++) {
int dice = new Random().nextInt((6 - 1) + 1) + 1;
if (player.get(dice) != null) {
player.put(dice, player.get(dice) + 1);
} else {
player.put(dice, 1);
}
}
for (int i = 0; i < 6; i++) {
int dice = new Random().nextInt((6 - 1) + 1) + 1;
if (computer.get(dice) != null) {
computer.put(dice, computer.get(dice) + 1);
} else {
computer.put(dice, 1);
}
}
int playerHeighestCount = -1;
int playerHeighestValue = -1;
for (Entry<Integer, Integer> entry : player.entrySet()) {
if (playerHeighestCount < entry.getValue()) {
playerHeighestCount = entry.getValue();
playerHeighestValue = entry.getKey();
}else if(playerHeighestCount == entry.getValue()){
if(playerHeighestValue < entry.getKey()){
playerHeighestCount = entry.getValue();
playerHeighestValue = entry.getKey();
}
}
}
int computerHeighestCount = -1;
int computerHeighestValue = -1;
for (Entry<Integer, Integer> entry : computer.entrySet()) {
if (computerHeighestCount < entry.getValue()) {
computerHeighestCount = entry.getValue();
computerHeighestValue = entry.getKey();
}else if(computerHeighestCount == entry.getValue()){
if(computerHeighestValue < entry.getKey()){
computerHeighestCount = entry.getValue();
computerHeighestValue = entry.getKey();
}
}
}
System.out.println(player + " " + computer);
if (computerHeighestCount > playerHeighestCount) {
System.out.println("Computer Won");
} else if (computerHeighestCount < playerHeighestCount) {
System.out.println("Player Won");
} else {
if (playerHeighestValue > computerHeighestValue) {
System.out.println("Player Won");
} else if (playerHeighestValue < computerHeighestValue) {
System.out.println("Computer Won");
} else {
System.out.println("Tie");
}
}
}
}
Let me know if you don't find answers reasonable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.