Question: JAVA 2D GRID GAME I need to create a 2D grid game whereby a cat and canary(ies) move on a 2d grid. The canary tries

JAVA 2D GRID GAME

I need to create a 2D grid game whereby a cat and canary(ies) move on a 2d grid. The canary tries to eat seeds and the cat tries to eat the canary. How do I move these animals to a random place on the grid? How do I determine if a location is occupied or not? How do I determine a winner at the end based on the amount of energy available? The classes are below with most of the code written apart from the start and move method which is needed to create this game.

JAVA 2D GRID GAME I need to create a 2D grid game

import java.util.ArrayList;

public class Grid implements GridObject { private ArrayList locations; private int size; public Grid(int size) { locations = new ArrayList(size*size); this.size=size; for(int i=0; i

public interface GridObject { }

public class Location { public final int x; public final int y; private GridObject gridObject; private boolean occupied; public Location(int x,int y){ this.x=x; this.y=y; } public void setGridObject(GridObject gridObject){ this.gridObject=gridObject; occupied=true; } public boolean getOccupied(){ return occupied; } @Override public boolean equals(Object object){ if(object==null){ return false; } if(object instanceof Location){ Location location = (Location) object; if(location.x == x && location.y == y){ return true; } } return false; } }

The Grid will try to move each moveable object to a random location on the grid. A moveable object in our game is an object that has a move method Obviously, a vegetable object like a seed, cannot move. The rules of the game are: If the location is unoccupied, the incoming object can move to the location. It loses some energy in the move if the location is already occupied, the incoming object [a Cat or a Canary) will try to eat the occupying object at the location. If the incoming object succeeds (i.e. its eat method returns true), it occupies the location (and absorbs the energy of the occupying object]. If it fails, it must go to another empty location and it must lose energy due to its move. To make this a fair fight, a Cat should lose a lot of energy it fails to eat. Despite what I said in the tutorial, I think that that an animal cannot have negative energy and should become immobile when its energy level is at zero. This continues for a number of turns and the winner is the animal object with the highest energy. Approach In the tutorial I am going to introduce some new Game classes. These should be all you need to complete the assignment Grid: a class that represents a 2D grid Gridobject: an interface for all Avatars that want to be added to the Grid Location: a simple clastha contains the xy coordinates of the Grid The Grid will try to move each moveable object to a random location on the grid. A moveable object in our game is an object that has a move method Obviously, a vegetable object like a seed, cannot move. The rules of the game are: If the location is unoccupied, the incoming object can move to the location. It loses some energy in the move if the location is already occupied, the incoming object [a Cat or a Canary) will try to eat the occupying object at the location. If the incoming object succeeds (i.e. its eat method returns true), it occupies the location (and absorbs the energy of the occupying object]. If it fails, it must go to another empty location and it must lose energy due to its move. To make this a fair fight, a Cat should lose a lot of energy it fails to eat. Despite what I said in the tutorial, I think that that an animal cannot have negative energy and should become immobile when its energy level is at zero. This continues for a number of turns and the winner is the animal object with the highest energy. Approach In the tutorial I am going to introduce some new Game classes. These should be all you need to complete the assignment Grid: a class that represents a 2D grid Gridobject: an interface for all Avatars that want to be added to the Grid Location: a simple clastha contains the xy coordinates of the Grid

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!