Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA QUESTION: In a single-player survival game, the player's goal is to find food to keep their energy up. A helicopter drops food at various

JAVA QUESTION:

In a single-player survival game, the player's goal is to find food to keep their energy up. A helicopter drops food at various locations throughout the player's world (a coordinate-based plane). At any point, the player needs to know which food item will result in the greatest net energy change. The net energy change of getting a food item is calculated as follows: To travel the distance to the food, it costs the player 1 unit of energy per unit traveled (unit traveled rounded to the nearest integer), and they gain the energy from eating the food, according to the food's energy value. So if a food item (cookies) with an energy value of 5 is 10.8 units away from the player, the net energy value of the food item is -6. (It costs them 10.8 energy units to travel to the food, but then they gain 5 units from eating the food.) As another example, if a food item (grapes) with an energy value of 10 is 3 units away from the player, the net energy value of the food item is 7. (It costs they 3 energy units to travel to the food, but then they gain 10 units from eating the food). When given the choice between cookies and grapes, the player would choose to get the grapes, since the net energy value is greater.

A common application of priority queues is keeping track of the nearest/most desirable/valuable item/location (e.g., where the nearest gas station is in a navigation app). You will implement the survival game described using a priority queue.

A player has a priority queue of food items, ordered by their net energy. The greater the net energy of the item, the more urgent the priority. Whenever a food item is dropped into the world, it is added to the priority queue.

When the player goes to get food, they will always get the item with the most urgent priority. The player moves to the food's location, causing their location to update accordingly, which means the net energy values (priorities) of the existing food items change as well. Thus, when this happens, the priority queue needs to be refreshed (all the items removed and re-added). Use these templates: SurvivalGame.java Download SurvivalGame.java Food.java Download Food.java Player.java

Example:

SurvivalGame game = new SurvivalGame(); game.dropFood(new Point(3, 3), "granola bar", 2); // net energy: -2 game.dropFood(new Point(-1, 1), "steak", 8); // net energy: 7 game.dropFood(new Point(16, 6), "candy bar", 1); // net energy: -16 game.dropFood(new Point(1, 0), "sandwich", 10); // net energy: 9 game.getPlayer().getFood(); // returns the sandwich // now player location is 1, 0 // new net energies: // steak: 6 // granola bar: -2 // candy bar: -15 game.getPlayer().getFood(); // returns the steak // now player location is -1, 1 // new net energies: // granola bar: -2 // candy bar: -17 game.dropFood(new Point(15, 5), "pizza", 20); // net energy: 4 game.getPlayer().getFood(); // returns the pizza // now player location is 15, 5 // new net energies: // candy bar: 0 // granola bar: -10 game.getPlayer().getFood(); // returns the candy bar game.getPlayer().getFood(); // returns the granola bar

Templates:

SurvivalGame.java

package labs.lab7; import java.awt.Point; public class SurvivalGame { // ADD YOUR INSTANCE VARIABLES HERE /** * Constructs a new survival game with player */ public SurvivalGame() { // FILL IN } public Player getPlayer() { return null; // FIX ME } /** * Drops food into the game * * @param location food drop location * @param description description of food item * @param energyValue energy value of food item */ public void dropFood(Point location, String description, int energyValue) { // FILL IN } }

Food.java

package labs.lab7; import java.awt.Point; /** * Class representing a food item in the survival game */ public class Food implements Comparable { // ADD YOUR INSTANCE VARIABLES HERE /** * Constructs a new food item * * @param location location of the food item * @param player player in the game * @param description description of the food item * @param energyValue energy value of the food item */ public Food(Point location, Player player, String description, int energyValue) { // FILL IN } public Point getLocation() { return null; // FIX ME } public String getDescription() { return ""; // FIX ME } /** * Compares based on the net energy value of this food item to the player; A * higher net energy means a more urgent priority. */ @Override public int compareTo(Object otherObject) { return -1; // FIX ME } /** * Returns a string representation of the food in the format: * "[description] at location [x], [y], energy value [energy value] */ @Override public String toString() { return ""; // FIX ME } /** * Calculates the net energy gain/loss to the player of getting this food item. * Net energy = (food energy value) - (distance from food (rounded to the nearest int)) * * @return the net energy */ public long getNetEnergy() { return -1; // FIX ME } }

Player.java

package labs.lab7; import java.awt.Point; import java.util.PriorityQueue; /** * Represents a player in the survival game * */ public class Player { private Point location; private int energy; private PriorityQueue foods; // foods in the world /** * Constructs a new player with location 0, 0 and energy 100 */ public Player() { // FILL IN } public Point getLocation() { return null; // FIX ME } public int getEnergy() { return -1; // FIX ME } public void setEnergy(int newEnergy) { // FILL IN } /** * Adds the given food item to this player's priority queue * * @param f the food item */ public void addFoodToQueue(Food f) { // FILL IN } /** * Moves player to location of food with highest priority and eats it (removes * it from the queue). Updates player's energy accordingly from the net energy * value of the food. * * Causes the food queue to be updated (all items removed and re-added), since * their priorities have changed based on the player's changed location * * @return the food with highest priority */ public Food getFood() { return null; // FIX ME } }

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

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions

Question

Define Administration?

Answered: 1 week ago