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 mazeMazeFileMenu
- 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
- the Java archive library for the Grid Package, containing classes
such as
BoundedGrid
, ColorBlock
,
ColorBlockDisplay
, BasicGridFileMenu
,
etc.
Class documentation for all classes in the Grid
Package can be found
here.
(The grid.jar
file is not included in the
MouseInAMaze.zip
file, and must be downloaded
separately.)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.
MouseInAMaze.zip
and extract the files.zip
file 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 +libs
folder and put the jar
file 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).
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 the other maze configuration
files provided in the MouseInAMaze.zip
file 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.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.
private Maze getMaze() { return (Maze) this.grid(); }
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.Mouse
class 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.)
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.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.)
If you wish to write a class to draw
a mouse graphically, you may want to use the 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!)
You can also draw the cheese graphically if you want.