In this program you will implement a simulation of a mouse looking for a piece of cheese in a maze.
Design a program to simulate mice moving through a maze until they find cheese. A mouse always starts at the starting position, and moves one unit at a time. Your program should be able to support mice with different movement strategies.
Your program should read in a maze configuration, including the starting position and the location of the cheese (the start and end locations for the maze). It should allow the user to run the experiment several times, with different maze configuration files. Each time, a piece of cheese is put at the end of the maze, a mouse is put at the starting location, and the mouse is allowed to move around the maze looking for the cheese. The user should be able to choose the type of mouse or mouse movement strategy for each run of the experiment. Your program should graphically display the state of the mouse and the maze at the end of each time unit. When a mouse finds the cheese, your program should report how many time units it took for the mouse to find the cheese.
Your program should have a graphical user interface with a File menu, a mouse choice drop-down menu, a button to add a mouse, a panel in which the maze is graphically displayed, and a slider bar for changing the speed of the mouse movement. (Actually, it controls the speed of the animation -- how long the program pauses to let you view the display between time steps -- rather than the speed of the mice.)
You may use the following classes or files, which have been fully implemented and are available in MouseInAMaze.zip and grid.jar.
Cheese- an extension of the
ColorBlockclass (you could implement it in some other way, though, if you chose)
MazeDataFileHandler- a class that reads the maze information from a file and constructs the maze
MazeFileMenu- a class that provides a File menu with an Open menu item; you may ignore this class as it is only used by the graphical user interface
MouseMazeController- a class that controls the mouse movement during program execution
MouseMazeGUI- a class that provides a graphical user interface for the Mouse-in-a-Maze project
maze1.dat- a sample maze initial configuration file
grid.jar- the Java archive library for the Grid Package, containing classes such as
BasicGridFileMenu, etc. Class documentation for all classes in the Grid Package can be found here. (The
grid.jarfile is not included in the
MouseInAMaze.zipfile, and must be downloaded separately.)
You will need to implement or modify the following classes:
Maze- an extension of the
Mouse- an object that moves around the maze looking for cheese
MouseInAMazeApp- a main class that specifies how to display mice and color blocks, specifies the type of mice in the program, and constructs a
MazeGUIobject; you will need to edit this class when you add new
Mousetypes to the project and want them to appear in the drop-down menu.
Mouseclass and several subclasses
You will probably find it useful to implement this program in stages (iterative development). For example, you might:
You do not have to develop the program in this way, but if you want to follow this structured approach you will find more details below.
MouseInAMaze.zipand extract the files.
zipfile does not contain project information, so you will have to create a project and import the extracted files. Or, if you are using BlueJ, you can simply open the folder as a Non BlueJ project in the Project Menu, which will also create project information. You will also have to make sure your project knows about the grid.jar library. In BlueJ, you can create a
+libsfolder and put the
jarfile there, or you can specify its location in the Libraries tab of the Preferences or Properties dialog box (under BlueJ->Preferences, Tools->Preferences or File->Properties, depending on the version of BlueJ you are using).
Mazeclass that extends
MazeDataFileHandlerobject will read in a maze configuration file and construct a
Mazeobject that corresponds to the configuration file. In order for
MazeDataFileHandlerto work correctly, the new class must be called
Mazeand it must have a two-parameter constructor just like
BoundedGrid. It should also have two instance variables to keep track of the starting location (for the mouse) and the finish location (the location of the cheese). It should have "getter" and "setter" methods for those instance variables, which must be called
getFinishLocin order for the
MouseMazeControllerto work correctly. The two setter methods must each take a single
Locationas a parameter and have a
voidreturn type. The getter methods do not need any parameters, but should return the appropriate
Locationobject. You should implement the constructor and the four methods to interact with your two instance variables correctly.
MouseMazeGUIobject will construct a mouse and add it to the maze when the user clicks on an Add Mouse button. In order for
MouseMazeGUIto work correctly, the new class must be called
Mouseand it should have a zero-parameter constructor. As a starting point you may wish to have your
Mouseclass extend the
ColorBlockclass in the same way that
Cheesedoes so that you have something you can easily display. You may wish to make your mouse
maze1.datinitial configuration file. This should create and display a maze with a piece of cheese in it, but no mouse. Test the various components of the graphical user interface to see which ones work at this point. Look at the contents of
maze1.datand the other maze configuration files provided in the
MouseInAMaze.zipfile and see if you can figure out what each line in these files might mean. Test your hypotheses by creating a new maze configuration file with different values.
movemethod in the
Mouseclass that does the following:
nextLocationfor example, that calculates this location. For now, the next location could be the current location.
Mazeclass, as described below.)
Mouseclass inherits a method from
GridObjectthat returns its grid, you may wish to create another method that returns the grid as a
Mazeobject so that you don't have to cast the grid to a
Mazeover and over in your program.
toStringmethod in the
Mouseclass to indicate the mouse's class and location. You can get the name of an object's class with the following:
Mouseclass that will "teleport" directly to the location of the cheese (the "beam me up" mouse).
Mousesubclass to the list of mouse types passed to the graphical user interface. If you do this correctly, your subclass should show up in the drop-down menu when you run the program. If you have the subclass selected in the drop-down menu before you click on the Add Mouse button, the new mouse should be a "beam me up" mouse.
Mousesubclass is working correctly. If your program throws an exception right away, make sure that your mouse is "eating" the cheese before attempting to move into that location, since a grid does not allow two objects in the same cell at the same time.
Mouseclass that will move randomly to any adjacent location in the maze (the "clueless mouse"). One way of doing this is to place any locations to which the mouse might move (empty locations or locations containing the cheese) into a list, then randomly pick the next location randomly from the list. (The number of items in the list would be the number of locations to which the mouse could logically move, which, depending on where the mouse is, might be less than four.)
Mousesubclass to the list of mouse types in your main class. Run your program several times until you have confidence that your new
Mousesubclass is working correctly.
System.out.println, or you may investigate using the
JOptionPaneclass (optional). Run your program several times until you have confidence that your calculations of the number of moves is working correctly. (You may wish to print the total after every step as you are testing, until you are confident that it is being calculated correctly.)
If you wish to write a class to draw
a mouse graphically, you may want to use the
as a model. Note that a display class assumes
that it is drawing in a 1 x 1 area with the origin at the center of the area.
In other words, the pixels range from -0.5 to 0.5 for both the x and y coordinates.
You can leave out (or comment out) the code for the gradient if you prefer.
(I suggest you not spend time creating a clever
class until after everything else in your program is working!)
You can also draw the cheese graphically if you want.