Harvey Mudd College
Computer Science 60
Assignment 6, Due Friday, March 8, 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 (actually, modify) a
Spampede.java file that contains your Spampede applet class.
In addition, you will need to use the following classes, which you
may spread among several files (such as those in /cs/cs60/as/a6).
- 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.
To get you started, there are the following files in the
/cs/cs60/as/a6 directory:
- Spampede.java (a skeleton of the Spampede applet)
- Maze.java (with the Maze class)
- MazeCell.java (with the MazeCell class)
- DequeInterface.java (with the Deque Interface)
- Deque.java (with the Deque class)
- DCell.java (with the DCell class)
- Queue.java (with the Queue class)
- QCell.java (with the QCell class)
You should copy all of these files to your ~/cs60/a06 directory
with the command
cp /cs/cs60/as/a6/* ~/cs60/a06
(This will also copy a couple of other important files you'll need.)
You can see what the skeleton Spampede.java applet does
at
http://www.cs.hmc.edu/~situdent/Spampede.html.
Note that this is just a starting point, and not the whole Spampede applet.
Getting the applet running
So, the first thing to do is to make sure you can get that
example applet running on your own webpage. To do this, follow
these steps:
- You should make sure you have all the files from /cs/cs60/as/a6
in the directory you'd like to work in. I would strongly suggest
creating an a6 directory in your ~/courses/cs60
directory and then putting all the files there. To move all the files
at once, go to the directory you'll be working in and then type
cp /cs/cs60/as/a6/* .
- Copy the Spampede.html file to your "public_html" directory.
That directory is inside your top-level home directory. Thus, from
your assignment 6 working directory you can type
cp Spampede.html ~/public_html
You will not have to do this step again.
- Make sure that the permissions are set so that people can view your
applet. I believe this is already true by default.
If you run into trouble, however, it won't hur to
do the following:
chmod 755 ~/public_html
Then, your public_html directory will be readable by anyone.
- Next, from your working directory, ~/courses/cs60/a6 or
whatever you've called it,
compile all of the files:
javac *.java
- This compilation will produce a large number or .class files.
These all need to be copied to your public_html directory with
the command
cp *.class ~/public_html
- Finally, you need to make sure that all of the files in your
public_html directory are readable by everyone.
To do this, type
chmod o+r ~/public_html/*
- Now your Spampede applet (in it's current state) should appear from any
web browser at the address
http://www.cs.hmc.edu/~<your turing login name>/Spampede.html
Writing the applet
Once the above steps work for you, you're ready to write your own applet.
Basically, the starting point is the Spampede.java file. You will
want to change the Spampede class to include a data member
of type Maze and you
will want to write a method that draws a Maze.
As you write your code, please compile and run the applet often
to make sure you're on the right path. Bascially, that means repeating steps
4-7 above.
How do I know what going wrong in an applet?
IE and Netscape have a wonderful applet-debugging feature called the
Java Console. To open the Java Console, go to the Communicator
Menu, the Tools submenu, and choose "Java Console."
A window will open up. In Internet Explorer, choose "Java Console"
from the "View" Menu. If any errors occur during the running
of your applet, they will appear in that window.
Also, you can use System.out.println to debug your
applet -- all of those print statements will print to the
Java Console. This is critical for debugging!
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 (spam) 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 (in the y direction) by 50 MazeCells (in the
x direction), the outer boundary of which
consists of walls. This room will be provided in the
Maze class within the /cs/cs60/as/a6/Maze.java
directory.
- Every so often, 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 any particular 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
either an appropriate consolation message should
be printed or, if you prefer, the game should be reset to its starting conditions.
(The latter 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 actions
indicated should occur:
- p : "pause" (as if the pause button had been pushed)
- s : "start" (as if the start button had been pushed)
These actions do not involve much coding -- simply calling
pause() and go() at the appropriate spots.
- When the user presses the following keys, the centipede
should change direction as indicated:
- r : "reverse" (switch head and tail)
- i : turn north
- j : turn west
- l : turn east
- k : turn south
If the centipede is already heading in the
direction that the user chooses, nothing changes.
If the user changes the centipede's direction so that it is moving
back on itself (from South to North, say, or West to East),
you may reverse direction, ignore the command, or
"terminate" the centipede. (The last option is what the example
applet does, but any choice is OK.)
- Note on "reverse" The reverse (r)
does not necessarily send the centipede in the
compass direction opposite to which it was just heading.
Instead, it switches the current tail to become the head and the current
head to become the tail. It then should determine
which direction the centipede's head segment and second segment
are heading and set the current heading appropriately.
- Maintain Start and Pause buttons. Working versions
are provided in Spampede.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 example Spampede applet,
so that your applet does not
flicker. This is built in to the starting file provided, so
there's really not a whole lot to do, I suppose... .
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.
Grading/Testing?
The best way to test your code is with a Java-enabled browser.
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.)
Internet Explorer also has a Java Console -- available from the
"View" menu.
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
There are a lot of files that make up this assignment --
you could type cs60submit for each of them, but you can
submit all of your .java files at once with the command
% cs60submit *.java
Here is a list of files that should be part of this
all-in-one submission. You may add your own files, as well,
if you wish. Note that you can also have
several class in one file, e.g., all of the Queue
and Deque classes or Maze with MazeCell.
Spampede.java
Maze.java
MazeCell.java
DequeInterface.java
Deque.java
DCell.java
Queue.java
QCell.java
There is no need to submit the .html file; we will
provide one.
No tests will be run; instead the graders will play your Spampede
applet to see if it does all of the things it should!