There is a Squirrel in a maze looking for nuts. You will guide the squirrel to find and collect the nuts. There are two types of nuts available in the maze, Almonds and Peanuts. As the squirrel collects the nuts, it gains nutritional points. Once the squirrel finds all the nuts in the maze, the game is over. To implement this game, you will define 7 classes: Maze class, Entity class, Squirrel class, Nuts class, Almond class, Peanut class, and HungrySquirrelGame class. Each class is described in details in the following sections.
The following class diagram shows the relationship between the classes: See image.
In this document, you will find each class defined in details. There is enough information to help you build and implement this game. You are not required to implement the class attributes and methods exactly as specified here; feel free to add/remove attributes or methods as you see fit. However, you must define each class and do not make modifications to the class names.
You must define the Maze class. The maze is a 20 x 50 rectangular area represented by a 2-dimensional character array. The walls in the maze are represented by * and available positions by blank space. The maze structure is read from a text file Maze.txt (The Maze.txt will be provided to you). A squirrel is able to freely move in blank spaces in the maze.
**************************************************
******************** **********************
******************** **********************
******************** **********************
*********** ***********
*********** ***********
* **
* **
* **
* ********************** **
* ********************** **
* ********************** **
* ********************** **
* **
* **
* **
***** ************** ********
************ *************
***** *********
**************************************************
The Maze class defines the following attributes:
The Maze class implements the following methods:
public static void create(String filename)
This method reads the file passed to the method (i.e. Maze.txt) and initializes the 2-dimentional array with the maze content provided in the file.
public static void display()
This method will display the maze structure and the containing entities.
public static boolean available(int row, int col)
This method takes a row and a column and determines if the location at the row and the column is blank space. If it is, it returns true; otherwise, it returns false.
You must define the "Movable" interface. This interface is implemented by entities that are capable to move in the maze (e.g. Squirrel) This interface has a single method:
void move(char direction)
You must define an abstract Entity class. Two types of entities exist in the Maze: Nuts and Squirrel. Each entity has three attributes:
The abstract Entity class contains an abstract method:
public void create()
The Entity class contains the following concrete method:
public char put(int newRow, int newCol)
This method puts an entity at location (newRow, newCol) in the maze. You have to pay attention because the entity cannot move to a location already filled with a wall (i.e. *). This method returns a character that was replaced in the new location. This can be useful when moving the squirrel and figuring out what it stepped over.
You must define the Squirrel class. The squirrel is represented by the @ symbol in the maze. It is able to move up, down, left and right. The initial location of the squirrel is determined by the user. The program must prompt the user to enter the starting row and column of the squirrel. The squirrel cannot move to where there is a wall (i.e. *). Once the squirrel moves over a nut, it collects points. Each type of nut carries a different point.
You must define the Squirrel class which is inherited from the Entity abstract class and implements the Movable interface. The Squirrel class contains two attributes:
The Squirrel class defines the following methods:
public void create()
This method overrides the abstract method in the Entity superclass. This method asks the user to provide the initial location of the squirrel in the maze. You have to make sure the location provided by the user is valid and available. If the user provides an invalid or unavailable location, you will have to ask the user to input a new set of data. You must continue until the user provides a valid position. Keep in mind that a squirrel cannot be placed where there is a wall *.
public void move(char direction)
This method moves the squirrel one position to the direction specified.
Nuts are entities that are placed in the maze for the squirrel to find. There are two types of nuts available: Almond and Peanut. When a squirrel moves over a nut, itll gain points. Once a nut is collected, it should disappear from the maze. There are total of 5 nuts created. The nuts locations are created randomly in the maze and have to be placed in a valid location in the maze, meaning that a nut cannot be put in a position occupied by a wall (*), a squirrel (@) or a previously created nut. The number of peanuts / almonds are based on a random 50% chance.
The Nuts class is inherited from the Entity abstract class. The Nuts class must contain the following attribute:
The Nuts class implements (or override when appropriate) the following methods:
void create()
This method overrides the abstract method in the Entity superclass. This method randomly creates the location of the nuts. You have to make sure the locations are valid and available. Keep in mind that a nut cannot be placed over a wall (*), a squirrel (@) or a previously created nut. In other words, it can only be placed where there is a blank space ( ).
Almond is a type of a Nut . An almond is represented by the character symbol A in the maze. An almond carries 5 nutritional points; therefore, when the squirrel eats an almond, it gains 5 points.
The Almond Class is inherited from the Nuts class. The Almond class must define the following attribute:
Peanut is a type of a Nut. A peanut is represented by the character symbol P in the maze. A peanut carries 10 nutritional points; therefore, when the squirrel eats a peanut, it gains 10 points.
The Peanut Class is inherited from the Nuts class. The Peanut class must define the following attribute:
The HungrySquirrelGame class defines the main method. The flow of the main method can be something like this:
“Squirrel successfully collected all the nuts. Total points 30.”
“Thank you for playing this game”
**************************************************
******************** **********************
******************** **********************
******************** **********************
*********** ************
*********** ************
* **
* **
* **
* ********************** **
* ********************** **
* ********************** **
* ********************** **
* **
* **
* **
***** ************** ********
************ *************
***** *********
**************************************************
Enter the Squirrel position (row , column): 1,1
Position not available. Try again!
Enter the Squirrel position (row , column): 7,23
User input accepted.
Enter commands u,d,l,r to move Up, Down, Left, and Right:
**************************************************
******************** **********************
******************** **********************
******************** **********************
*********** ************
*********** ************
* @ **
* **
* A P **
* ********************** **
* ********************** **
* P ********************** **
* ********************** **
* A **
* **
* **
***** ************** ********
************ *************
***** P *********
**************************************************
Enter command: d
**************************************************
******************** **********************
******************** **********************
******************** **********************
*********** ************
*********** ************
* **
* @ **
* A P **
* ********************** **
* ********************** **
* P ********************** **
* ********************** **
* A **
* **
* **
***** ************** ********
************ *************
***** P *********
**************************************************
**************************************************
******************** **********************
******************** **********************
******************** **********************
*********** ************
*********** ************
* **
* **
* **
* ********************** **
* ********************** **
* ********************** **
* ********************** **
* **
* **
* **
***** ************** ********
************ *************
***** @ *********
**************************************************
Squirrel successfully collected all the nuts. Total points 40
Thank you for playing this game