Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA Programming) Background In the board game Risk, each player\'s goal is to

ID: 3588267 • Letter: #

Question

(JAVA Programming)

Background

In the board game Risk, each player's goal is to conquer the world by occupying every country. The play involves neighboring countries fighting battles. Most battles in Risk involve four armies, two from the attacking country, and two from the defending country.

The process for fighting a battle involves the attacker rolling three dice, and the defender rolling two dice. We will assume these are typical 6-sided dice with values 1 through 6, inclusive. The highest die of the attacker is compared with the highest die of the defender, and the second-highest die of the attacker is compared with the second-highest die of the defender. If both of the attacker's dice are higher than the defender's, the attacker wins the battle and the defender loses two armies. If both of the defender's dice are higher than or equal to the attacker's, the defender wins the battle and the attacker loses two armies. Otherwise, each player loses one army. Some examples follow:

Attacker

Defender

Comparison

Result

1 5 4

4 2

attacker 5 > defender 4, attacker 4 > defender 2

attacker wins, defender loses two armies

4 3 4

2 4

attacker 4 <= defender 4, attacker 4 > defender 2

tie, each player loses one army

3 4 3

5 3

attacker 4 <= defender 5, attacker 3 <= defender 3

defender wins, attacker loses two armies

Programming Test

You are to write the following program. Write the program exactly as specified. Do not "improve" it. Do not use language elements that we have not covered. Remember to use good coding practices, such as following the Java naming convention and formatting your code.

Write a class AttackUtilities in the default package containing the following public static methods.

A public static method rollDie that has no parameters and returns an int random value between 1 and 6 (inclusive).

Overloaded public static methods diceMax that take either three or two int parameters. diceMax returns an int that is the largest of its parameter values.

Overloaded public static methods diceSecond that take either three or two int parameters. diceSecond returns an int value that is the second-highest value of its parameters. Note that the second highest value of (5, 4, 5) is 5, since we consider that the highest value is one of the 5s, and the "other" 5 is the second highest. Similarly, the second highest value of (4, 4) is 4; the highest value is one of the 4s, and the second highest is the "other" 4.

A public static method scoreBattle. The parameter list for scoreBattle is four int values, and they are, in order, attackerHighestDie, attackerSecondHighestDie, defenderHighestDie, defenderSecondHighestDie. The method returns an int value of

1 if the attacker wins the battle and the defender loses two armies

-1 if the defender wins the battle and the attacker loses two armies

0 if there is a tie and the attacker and defender lose one army each

Write a class AttackAnalysis in the default package that has only a main method. The main method

uses a Scanner and a sentinel-controlled loop to query the user for the number of battles to simulate. If the user enters -1, the program exits.

Uses a nested counter-controlled loop to simulate the number of battles specified by the user.

For each battle, the program

Uses rollDie to determine the values of the attacker's three dice and the defender's two dice.

Uses diceMax to determine the highest die value for each player.

Uses dieSecond to determine the second-highest die value for each player.

Uses scoreBattle to determine the outcome of the battle.

As the simulations are run, the program totals how many battles the attacker wins, how many the defender wins, and how many are ties.

After the specified simulations have finished, the program prints the information specified in Program Output, below.

The program then queries the user for another number of battle simulations, or -1 to exit.

Program Output

After the outcome of the simulated battles has been determined, main should

print the percentages of the battles the attacker won, the defender won, and ties.

print the total number of armies lost by the attacker and defender. (Don't forget that the ties also factor into the armies lost.)

Sample Output

In the following sample, the user runs a simulation of five battles, then 1,000,000 battles, and then enters -1 to exit.

Submission

When your program is complete, place your two Java source files into a single zip file containing no directories and submit it as your response in Blackboard.

Hints

Write and test one method, then move on to the next and write and test it. For example, start with the main method. Create a Scanner and sentinel-controlled loop and ask the user how many battles to fight or to enter -1 to exit. Test this loop. Then write your counter-controlled loop and in it, just print "fighting battle " <whatever>. This will tell you that your loops work. Then write rollDie in AttackUtilities. Call it and print its result in your loop. When you are comfortable that it works, create the attacker's dice. Then write the three-parameter diceMax and test it. Then assign the defender's dice values and write and test the two-parameter diceMax. This way, you're making sure that each piece of the program is building on working code.

Grading Elements

Five points each:

all classes and methods named correctly

all methods are in the proper class

all methods have correct headers, return types, signatures

all methods return the correct value

main method uses a sentinel-controlled loop to ask the user for the number of battles to simulate or to exit. It uses a counter-controlled loop to simulate the battles.

main method uses the specified utility methods to perform each simulated battle

main method prints the requested output

the program follows procedures and conventions established by your instructor and is submitted correctly.

Attacker

Defender

Comparison

Result

1 5 4

4 2

attacker 5 > defender 4, attacker 4 > defender 2

attacker wins, defender loses two armies

4 3 4

2 4

attacker 4 <= defender 4, attacker 4 > defender 2

tie, each player loses one army

3 4 3

5 3

attacker 4 <= defender 5, attacker 3 <= defender 3

defender wins, attacker loses two armies

Explanation / Answer

Given below is the code for the question. For each battle, it shows the values of dice and score so that it is easy to verify the program's implementation is correct. You can comment out the lines to display the dice and score when running large no. of simulations.

Hope the answer helped. If it did, please do rate it. Thank you.

Note: to reindent code in eclipse, in the file press Ctrl + A and then Ctrl+ i

AttackUtilities.java


import java.util.Random;
public class AttackUtilities {
private static Random random = new Random(System.currentTimeMillis());
//returns a random number in range 1-6
public static int rollDie()
{
return 1 + random.nextInt(6);
}
//returns the greatest of d1 and d2
public static int diceMax(int d1, int d2)
{
if(d1 >= d2)
return d1;
else
return d2;
}
//returns the greatest of d1, d2 and d3
public static int diceMax(int d1, int d2, int d3)
{
if(d1 >= d2)
{
if(d1 >= d3)
return d1;
else
return d3;
}
else
{
if(d2 >= d3)
return d2;
else
return d3;
}
}
//returns the 2nd greatest of d1 and d2
public static int diceSecond(int d1, int d2)
{
if(d1 >= d2)
return d2;
else
return d1;
}
//returns the 2nd greatest of d1, d2 and d3
public static int diceSecond(int d1, int d2, int d3)
{
if(d1 >= d2)
{
if(d2 >= d3) //d1 >= d2 >= d3
return d2;
else //compare d1 and d3
{
if(d1 >= d3) //d1 > d3 > d2
return d3;
else //d3 > d1 > d2
return d1;
}
}
else //d2 > d1
{
if(d1 >= d3) //d2 > d1 > d3
return d1;
else //d3 > d1, compare d2 and d3
{
if(d2 >= d3) // d2 > d3 > d1
return d3;
else //d3 > d2 > d1
return d2;
}
}
}
public static int scoreBattle(int attackerHighestDie, int attackerSecondHighestDie, int defenderHighestDie, int defenderSecondHighestDie)
{
if(attackerHighestDie >= defenderHighestDie)
{
if(attackerSecondHighestDie > defenderSecondHighestDie) //now both of attackers dice are greater
return 1;
else //1 of attacker and other of defender is greater, its a tie
return 0;
}
else
{
if(defenderSecondHighestDie > attackerSecondHighestDie) // both of defender values are greater than attacker
return -1;
else //its a tie
return 0;
}
}
}

AttackAnalysis.java


import java.util.Scanner;
public class AttackAnalysis {
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
int simulations = 0;
int attackerWins = 0;
int defenderWins = 0;
int ties = 0;
boolean stop = false;
while(!stop)
{
System.out.print("How many simulations (enter -1 to stop) ? ");
simulations = keybd.nextInt();
if(simulations == -1)
stop = true;
else
{
for(int i = 1; i <= simulations; i++)
{
int atttackerd1 = AttackUtilities.rollDie();
int atttackerd2 = AttackUtilities.rollDie();
int atttackerd3 = AttackUtilities.rollDie();
int defenderd1 = AttackUtilities.rollDie();
int defenderd2 = AttackUtilities.rollDie();
int attackerMax = AttackUtilities.diceMax(atttackerd1, atttackerd2, atttackerd3);
int defenderMax = AttackUtilities.diceMax(defenderd1, defenderd2);
int attacker2ndMax = AttackUtilities.diceSecond(atttackerd1, atttackerd2, atttackerd3);
int defender2ndMax = AttackUtilities.diceSecond(defenderd1, defenderd2);
int score = AttackUtilities.scoreBattle(attackerMax, attacker2ndMax, defenderMax, defender2ndMax);
if(score == 1)
attackerWins++;
else if(score == -1)
defenderWins ++;
else
ties++;
System.out.print("Attacker: " + atttackerd1 + " " + atttackerd2 + " " + atttackerd3 +", ");
System.out.print("Defender: " + defenderd1 + " " + defenderd2 + ", ");
System.out.print(" (" + attackerMax +", " + attacker2ndMax + ") " + " vs " );
System.out.print("(" + defenderMax +", " + defender2ndMax + ") " );
System.out.println(" Score = " + score);
}
System.out.println(" Attacker Wins: " + attackerWins);
System.out.println("Defender Wins: " + defenderWins);
System.out.println("Ties: " + ties);
System.out.println();
}
}
}
}

output


How many simulations (enter -1 to stop) ? 5
Attacker: 6 1 1, Defender: 4 6, (6, 1) vs (6, 4) Score = 0
Attacker: 4 1 4, Defender: 2 2, (4, 4) vs (2, 2) Score = 1
Attacker: 3 4 1, Defender: 2 2, (4, 3) vs (2, 2) Score = 1
Attacker: 1 2 2, Defender: 5 3, (2, 2) vs (5, 3) Score = -1
Attacker: 6 2 1, Defender: 3 5, (6, 2) vs (5, 3) Score = 0
Attacker Wins: 2
Defender Wins: 1
Ties: 2
How many simulations (enter -1 to stop) ? 10
Attacker: 6 4 3, Defender: 2 1, (6, 4) vs (2, 1) Score = 1
Attacker: 2 4 1, Defender: 5 1, (4, 2) vs (5, 1) Score = 0
Attacker: 2 3 5, Defender: 6 6, (5, 3) vs (6, 6) Score = -1
Attacker: 1 1 1, Defender: 3 4, (1, 1) vs (4, 3) Score = -1
Attacker: 2 1 1, Defender: 3 3, (2, 1) vs (3, 3) Score = -1
Attacker: 1 2 5, Defender: 1 1, (5, 2) vs (1, 1) Score = 1
Attacker: 5 1 5, Defender: 4 2, (5, 5) vs (4, 2) Score = 1
Attacker: 2 6 2, Defender: 3 1, (6, 2) vs (3, 1) Score = 1
Attacker: 2 3 2, Defender: 6 1, (3, 2) vs (6, 1) Score = 0
Attacker: 4 6 3, Defender: 6 4, (6, 4) vs (6, 4) Score = 0
Attacker Wins: 6
Defender Wins: 4
Ties: 5
How many simulations (enter -1 to stop) ? -1