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
MouseFileMenu- 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- a Java archive library containing classes such as
BasicGridFileMenu, etc. The class documentation for all classes in this library is available online.
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.
mousemaze.zipand extract the files.
mousemaze.zipinto the project (Project->Import, or File->Import in older versions of BlueJ) Bring up the the Tools->Preferences dialog box (or File->Properties in older versions of BlueJ) and, in the Libraries tab, add the
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.dat and see if you can figure out what each line of the file might mean. Test your hypotheses by creating a new initial 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.
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.)
Mouseclass that will move randomly to any adjacent location in the maze (the "clueless mouse"). You may wish to use the
nextLocationmethod from the
Fishclass in the Marine Biology Simulation as a template.
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.
MouseDisplayclass that draws a mouse graphically, or you could use an image, such as the
mouse.gifimage. To associate an image with a class, construct a new
ScaledImageDisplayas the display object, passing it the name of the image file as a
Stringparameter (e.g., "mouse.gif"). If you want the image to be take on the appropriate color for a mouse, add a
ScaledImageTintDecoratorto the image display. This will tint the image with the color specified by the mouse's
colormethod. You can find the class documentation for
Mouseclass to extend
ColorBlock. If you do this, and if your method of displaying a mouse makes use of its color, you will need to add an instance variable and a
colormethod to the
Mouseclass to keep track of and report its color.
FishDisplayclass 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
MouseDisplayclass until after everything else in your program is working!)
Cheeseclass, rather than just use an object of the