Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

(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 " . 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

5. Give examples of variations in contextual rules.

Answered: 1 week ago