The problem
For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Each possible item is worth a certain number of fishing points. The points will remain hidden until the user is finished fishing, and then a message is displayed congratulating the user, depending on the number of fishing points gained.
Here are some suggestions for the game's design:
- Each round of the game is performed as an iteration of a loop that repeats as long as the player wants to fish for more items.
- At the beginning of each round, the program will ask the user whether or not he or she wants to continue fishing.
- The program simulates the rolling of a six-sided die
- Each item that can be caught is represented by a number generated from the die; for example, 1 for "huge fish", 2 for "an old shoe", 3 for a "little fish", and so on.
- Each item the user catches is worth different amount of points
- The loop keeps a running total of the user's fishing points.
- After the loop has finished the total number of fishing points is displayed, a long with a message that varies depending on the points earned.
Breaking it down
Die Class
public class Die {
private final int SIDES = 6;
private int value;
/**
* Default constructor will call the roll
*/
Die() {
roll();
}
/**
* The roll method sets the value of the die to a random number.
*/
public void roll() {
// Create a random object.
Random randomValue = new Random();
// Set the value to a random number.
value = randomValue.nextInt(SIDES) + 1;
}
public int getValue() {
return value;
}
}
Roll Outcome Class
public class RollOutcome {
String message;
Integer points;
public RollOutcome(String message, Integer points) {
super();
this.message = message;
this.points = points;
}
public String getMessage() {
return message;
}
public Integer getPoints() {
return points;
}
}
Program demonstration
public static void main(String args[]) {
// instance of class
FishingGameSimulator feSim = new FishingGameSimulator();
// set up outcome map
Map<Integer, RollOutcome> outComeMap = new HashMap<>();
outComeMap.put(1, feSim.new RollOutcome("huge fish", 10));
outComeMap.put(2, feSim.new RollOutcome("an old shoe", 20));
outComeMap.put(3, feSim.new RollOutcome("little fish", 30));
outComeMap.put(4, feSim.new RollOutcome("30 inch walleye", 40));
outComeMap.put(5, feSim.new RollOutcome("Salt water redfish", 50));
outComeMap.put(6, feSim.new RollOutcome("52 inch muskellunge", 60));
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
String continuePlay = "";
Integer totalFishingPoints = 0;
System.out.println("Lets go fishing!");
do {
Die die = feSim.new Die();
RollOutcome outcome = outComeMap.get(die.getValue());
System.out.println("You rolled: \t\t" + die.getValue());
System.out.println("You caught: \t\t" + outcome.getMessage());
System.out.println("Points: \t\t" + outcome.getPoints());
// track points
totalFishingPoints += outcome.getPoints();
// ask user to play again
System.out.println("");
System.out.println("Lets go fishing! Enter Y to play: ");
continuePlay = keyboard.next();
} while (continuePlay.equalsIgnoreCase("Y"));
System.out.println("Thanks for playing, total points: " + totalFishingPoints);
//Game over message
if (totalFishingPoints < 100) {
System.out.println("Better Luck next time");
} else {
System.out.println("BOOM, lets get the grill started");
}
// close keyboard
keyboard.close();
}
Output
Lets go fishing!
You rolled: 4
You caught: 30 inch walleye
Points: 40
Lets go fishing! Enter Y to play:
y
You rolled: 4
You caught: 30 inch walleye
Points: 40
Lets go fishing! Enter Y to play:
y
You rolled: 3
You caught: little fish
Points: 30
Lets go fishing! Enter Y to play:
y
You rolled: 1
You caught: huge fish
Points: 10
Lets go fishing! Enter Y to play:
n
Thanks for playing, total points: 120
BOOM, lets get the grill started
Level Up
- Refactor outComeMap and game over message to be populated to a method. Create two new unit tests for behavior created.
- Clean up output so it is presented in a matrix excel format with columns of Roll, Catch and Points.