Harvey Mudd College
Computer Science 60
Assignment 6, Due Thursday, March 2, by midnight
Spampede!
Assignment 6 takes advantage of the double-ended Queue
(Deque) class you wrote in assignment 5 in order to implement
a centipede in search of spam within a simple Maze-like world.
This will give you the chance
to use Java's graphics library and event-handling code.
The Java 1.1 event-handling mechanism will be used.
Example Applet
You can run an example Spampede applet at
www.cs.hmc.edu/~dodds/Applets/Spampede/Spampede.html .
Files and Classes to Create
You will need to create a file
Spampede.java that contains at least the following classes:
- a MazeCell class (the default class from assignment 5 is included)
- a Maze class (a stripped-down version of assignment 5's is included)
that contains a two-D array of MazeCells named grid.
- a Deque class (such as the one you wrote last week)
- a DCell class (used in Deque)
- a Queue class (used in Deque)
- a QCell class (used in Queue)
- a Spampede class that extends the built-in
Applet class in Java.
Note that only the Spampede class is entirely new for this
assignment. An example applet's source code
is available in the file /cs/cs60/as/a6/ExampleApplet.java.
Its applet can be seen at
www.cs.hmc.edu/~dodds/Applets/ExampleApplet/ExampleApplet.html .
A copy of ExampleApplet.java is named Spampede.java
in the /cs/cs60/as/a6 directory. To get it, the HTML
file you will need, and a script for compiling and copying your
Spampede applet, you can do the following:
cp /cs/cs60/as/a6/*Spam* ~/cs60/a06
OPnce you have done that, you should be able to compile Spampede
and copy it to your public_html directory with the single command
./makeSpampede
from your ~/cs60/a06 directory.
What does the code have to do?
The Spampede applet gives a user control over a spam-seeking centipede.
Key presses from the keyboard change the direction of the centipede's
movement in order to intersect snacks that appear at random places
on the screen. For each snack consumed, the centipede grows
by one segment (a segment is simply one MazeCell).
The particular applet behaviors and methods you need to implement are
- Drawing the maze and the centipede within it. Each MazeCell
(the same as centipede segments) should be 10 pixels by
10 pixels. The default maze will be a simple room with
a single vertical wall in the middle. The room will
be 30 MazeCells by 50 MazeCells, the outer boundary of which
consists of walls. This room will be provided in the
Maze class within the /cs/cs60/as/a6/ExampleApplet.java
file.
- Every 0.5 second, the centipede's position should be updated
within the maze. That is, the centipede's head advances one
MazeCell in whatever direction it is facing, and the
centipede's tail retracts by one MazeCell.
(You need not stick to this speed, but remember that the graders
will be using your applet -- try to keep them happy!)
- If the centipede's head occupies a MazeCell that contains
a wall or another part of the centipede, the centipede dies, and
an appropriate consolation message should appear somewhere on the screen,
or, if you prefer, the game should be reset to its starting conditions.
(This is what the example Spampede applet does.)
- If the centipede's head occupies a MazeCell that contains
a morsel of spam, the centipede grows by one MazeCell. That is,
when the centipede next moves, its head advances one square, but
its tail does not retract.
- When the user presses the following keys, the centipede
should change direction as indicated:
- r : reverse (switch directions)
- i : turn up (north)
- j : turn left (west)
- l : turn right (east)
- k : turn down (south)
If the centipede is already heading in the
direction that the user chooses, nothing changes.
If the centipede changes direction so that it is moving
back on itself (from South to North, say, or West to East),
you may either reverse direction, ignore the change, or
"terminate" that centipede. (The last is what the example
applet does.) Note: the reverse (r) should work
without dying in any case.
- Maintain Start and Pause buttons. Working versions
are provided in ExampleApplet.java -- you will not have
to change them, though you may "upgrade" them if you wish.
Use double-buffering, as discussed in
class and shown in the ExampleApplet applet, so that your applet does not
flicker. This is built in to the starting file provided.
I want more!
If you haven't had enough of the Spampede.java file at the end of this
assignment, there are a couple of specific items and an open-ended invitation
to improve on the applet for up to 20% (or beyond...)
optional bonus credit.
- Speed up: You might want to have the rate at which the centipede
is moving to increase as the game progresses.
- Scoring: You might want to have a system of scoring with
a runing total displayed as a label or text field or simply
drawn to the applet panel.
- Lives: Rather than reseting after a single crash, keep
a text field (or label) with the number of lives remaining
and decrement it after each crash. When there are no lives left,
stop the game (though you might want to consider a "reset" button.)
- Levels: Rather than maintaining a single, static maze,
you may want to have the centipede advance to different mazes
after comsuming enough spam.
- General improvements: Feel free to add additional features
you think would enhance the Spampede! applet.
In addition, the extra credit for last week's assignment is extended
through this week. If you would like to create an applet representation
of the SpamSeeker (as indicated in that assignment), you may
submit that in a separate file for additional credit.
Testing your applet
You can test your code in two ways:
- With the appletviewer: if you are working in the terminal
room or have an X connection to a remote machine, you can
test your applet with the command
appletviewer Spampede.html
from any directory which contains both the compiled java code
(Spampede.class,
etc.)
and the Spampede.html files, available for copying from
/cs/cs60/as/a6.
Caution! For me, turing's appletviewer takes a long time
to appear and run (> 1 minute) unless the line
setenv JAVA_FONTS "/usr/local/java/jre/lib/fonts"
is added to my .cshrc file (which is then "sourced") before running.
This line speeds the search for fonts, but error
messages still appear that some fonts are not found.
Appletviewer seems to process events very slowly in any case.
- With a web browser: (preferred!)
Documents you place in your public_html directory (which should
be in
your home directory) will be publicly available on the web.
If you copy the files /cs/cs60/as/a6/ExampleApplet.html
and the .class files (obtained by compiling) to your public_html
directory, you will
be able to view the ExampleApplet applet at URL
http://www.cs.hmc.edu/~username/ExampleApplet.html, where
username is replaced by your turing login name.
A script for compiling and copying all the necessary files is
available at /cs/cs60/as/a6/makeSpampede. You can
copy that file into your directory and then run it as
./makeSpampede
(You need the extra ./ if . (dot) is not
in your path.
Note Netscape will reload newly-compiled applet code if
you hold down the "Shift" key while pressing the reload button.
Internet Explorer will reload applet code if your hold down the "Ctrl" key
while pressing the refresh button. (I suppose they're different to avoid lawsuits ... ?).
Note #2 Netscape has a "Java Console" available from the Communicator -- Tools
menu. You should have this open as you test your applet. With the Java Console, you
can include System.out.println messages anywhere in your code and
see what gets printed. In addition, runtime errors will get reported there.
This is a good thing! Debugging without it is bad! (Spoken from first-hand
experience.)
I don't know how to use the Java Console on IE (if anyone does, please let me
know.)
Reading
This assignment takes advantage of Object-Oriented programming (covered
in Chapter 7 of the book), but the details of Java's Applet
and GUI Component classes are available from the
references page.
Also, the basics of event handling and Java's built-in classes
will be presented in class.
Submission
You should submit your Spampede.java source file in the usual
way, i.e., by running
% cs60submit Spampede.java
You will be asked to input the assignment number (6).
You should not submit your Spampede.html file.