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 ColorBlock
class (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 mazeMouseFileMenu - 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 interfaceMouseMazeController - a class that controls the mouse
movement during program executionMouseMazeGUI - a class that provides a graphical
user interface for the Mouse-in-a-Maze projectmaze1.dat - a sample maze initial configuration filegrid.jar
- a Java archive library containing classes such as BoundedGrid,
ColorBlock, ColorBlockDisplay, 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 BoundedGrid classMouse - an object that moves around the maze looking for cheeseMouseInAMazeApp - a main class that specifies how to display
mice and color blocks, specifies the type of mice in the program, and
constructs a MazeGUI object; you will need to edit this class
when you add new Mouse types to the project and want them to
appear in the drop-down menu.Mouse class and several subclassesYou 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.zip
and extract the files.mousemaze.zip into 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 grid.jar file.
Maze class that extends BoundedGrid. The
MazeDataFileHandler object will read in a maze configuration
file and construct a Maze object that corresponds to the configuration
file. In
order for MazeDataFileHandler to
work correctly, the new class must be called Maze and 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 setStartLoc, setFinishLoc, getStartLoc,
and getFinishLoc in order for the MouseMazeGUI and
MouseMazeController to work correctly.
The
two setter methods must each take a single Location as
a parameter and have a void return type. The getter
methods do not need any parameters, but should return the appropriate
Location object. You should implement the constructor
and the four methods to interact with your two instance variables correctly. Mouse class. The MouseMazeGUI object
will construct a mouse and add it to the maze when the user clicks on
an Add Mouse button. In order for MouseMazeGUI to
work correctly, the new class must be called Mouse and
it should have a zero-parameter constructor. As a starting point
you may wish to have your Mouse class extend the ColorBlock class in
the same way that Cheese does so that you have something you can easily
display. You may wish to make your mouse LIGHT_GRAY rather than
YELLOW.maze1.dat initial
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.move method in the Mouse class that does the following:
nextLocation for example,
that calculates this location. For now, the next location could
be the current location.Maze class, as described below.)Mouse class inherits a method from GridObject that
returns its grid, you may wish to create another method that returns
the grid as a Maze object so that you don't have to cast
the grid to a Maze over and over in your program.toString method in the Mouse class
to indicate the mouse's class and location. You can get the name
of an object's class with the following: object.getClass().getName().Mouse class that will "teleport"
directly to the location of the cheese (the "beam me up" mouse).Mouse subclass
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.Mouse subclass 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 JOptionPane class (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.)Mouse class that will move
randomly to any adjacent location in the maze (the "clueless mouse"). You
may wish to use the nextLocation method from the Fish class
in the Marine Biology Simulation as a template.Mouse subclass
to the list of mouse types in your main class. Run your program
several times until you have confidence that your new Mouse subclass
is working correctly.MouseDisplay class that draws a mouse graphically,
or you could
use
an image, such as the mouse.gif image. To associate an
image with a class, construct a new ScaledImageDisplay as the
display object, passing it the name of the image file as a String parameter
(e.g.,
"mouse.gif").
If you want the image to be take on the appropriate color for a mouse, add
a ScaledImageTintDecorator to the image display. This will tint
the image with the color specified by the mouse's color method. You
can find the class documentation for ScaledImageDisplay and ScaledImageTintDecorator online.Mouse class to extend GridObject rather
than 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 color method
to the Mouse class
to keep track of and report its color. FishDisplay class
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 MouseDisplay
class until after everything else in your program is working!)Cheese class, rather than just use an object
of the ColorBlock class.)