Computer Science 60
Principles of Computer Science
Fall 2003
Assignment 4: A taste of rex and a mouthful of Spam!
Due Monday, September 29 by 11:59 PM.
This assignment has two parts. The first part is a short programming
exercise to familiarize you with rex.
The second part of the assignment is to write a Spamsweeper Applet.
You can do these parts of the assignment in any order - they are
entirely
independent of one another.
See the the course web page for a reminder of how the
late homework policy works.
rex!
First the rex assignment.
Type rex at the prompt to enter the rex interpreter.
You can exit rex at any time by pressing CTRL-D (control key and D).
(By doing this, you will lose all of the functions that you defined while
you were in the interpreter.)
Play around with rex and get a feel of how it works. For documentation,
see the rex reference guides available from the "Reference Guides" link
on the course homepage.
Write a function called twiddle which takes a list as input and
returns a list which is identical to the input list except with the first
and second items of the list exchanged.
For example twiddle([1, 2, 3, 4])
should return the list [2, 1, 3, 4]. If the user provides
a list of length less than 2, that's not your problem!
Once you've got the twiddle function
working, exit rex and type that function definition in a file called
hw4.rex. Now you can start rex back up and, at the rex prompt,
type *i hw4.rex. This will load in the file
(the "*i" stands for "include") and you can now use the functions that were
defined in that file. Alternatively, you can type rex hw4.rex at the shell and it will start rex up with the functions defined in the file file
hw4.rex. As an example, I have provided a file called
length.rex in which I have defined the length function. You'll
find this file in the directory /cs/cs60/assignments/assignment4/rexstuff.
Next, add the following functions to your file hw4.rex:
- A function called myreverse which takes a list as input
and returns the reverse of that list. For example
myreverse([1, 2, 3, 4]) should return [4, 3, 2, 1].
You may use the append
function (which is built-in to rex) but don't use any other rex functions
(for example, using rex's built-in reverse function would make
this just a bit too easy!)
- A function called power(X, Y) which takes two integers
as input and outputs X raised to the Y power. You may assume that Y
is non-negative.
For example, power(10, 3) would return 1000.
Rex handles arithmetic
in the normal way as we saw in the definition of the
length function.
For another example of arithmetic in rex, take a look at the file
factorial.rex in /cs/cs60/assignments/assignment4/rexstuff.
This function computes n! (pronounced "n factorial")
which, you will recall from Lecture 1, is n*(n-1)*(n-2)*...*1.
- A function called find_index(X, L) which
returns the index of element X in
list L. The first item in the list is assumed to have index 0.
If X is not an element of L then this function should
return a number larger than the index of any element in the list.
For example here is some sample input and output:
rex > find_index(1, [1, 2, 3]);
0
rex > find_index(3, [1, 2, 3]);
2
rex > find_index(42, [1, 2, 3]);
4
Note that since 42 is not in the list [1, 2, 3], the
function returned a number larger than the length of the list, which is 3.
My solution printed 4, but any number larger than 3 would be OK in
this case.
To summarize,
your final submission will contain functions twiddle,
myreverse, power, and find_index
all in one file called
hw4.rex. Now you can use cs60submit to submit this file!
Spamsweeper!
Now for Spamsweeper!
Note that the applet that you will be writing will run under most
(but not all) web browsers
due to incompatibilities between versions of Java. This applet DOES
run under mozilla on the terminals in the CS terminal room. It also
runs under many other browswers.
First, click here
to see what the Spamsweeper applet will look like
(more or less) when you're done. You're not required to implement the
"New Game" button (that's optional bonus stuff - without this button
you will simply need to restart the applet each time). Your game should otherwise
be similar to this example. Specifically, your program should have the
following features:
- The game is played on a 12 x 12 board with 20 hidden cans of Spam.
- If the user clicks on a cell that does not contain Spam, the
program indicates how many of the neighboring cells (a cell has up to
8 neighbors) contain Spam. If this number is zero, the program uses
recursion to automatically expose all of the neighbors as described
in class.
- If the user clicks on a cell that contains Spam (without holding
the SHIFT key down) then the game ends. You can indicate this by
printing the message "Game Over" anywhere on the screen.
- If the user clicks on a cell that contains Spam while holding down
the SHIFT key, the letter "S" is exposed at that location.
- If the user clicks on a cell that does not contain Spam while
holding down the SHIFT key, the game ends with another "Game Over"
message.
- If all 20 cans of Spam are successfully defused, the player
is congratulated with some sort of effusive compliment printed
someplace on the screen.
I've left the basic components of Spamsweeper, as described in class,
in the directory
/cs/cs60/assignments/assignment4/Spamsweeper/.
Copy all of these files into your own directory.
In particular, you will need to write the constructor for Gameboard.java and all of the interesting methods in Spamsweeper.java.
You should compile Spamsweeper.java in the normal way.
The compiler will warn you that Spamsweeper is using "deprecated
methods". Ignore the warning. (It's just saying that there are other newer
methods available in Java to do some of the graphics black magic that we've
provided.)
To run the applet, you can simply type appletviewer
Spamsweeper.html. The appletviewer will only work if you are on
a terminal in the CS terminal room. The other alternative is to run the
applet from your web site. You can do this as follows:
Copy the Spamsweeper.html file and all the compiled files
of your Spamsweeper implementation (Spamsweeper.class,
Gameboard.class, Boardcell.class and any
other file that ends in .class that you used in your
implemenation)
to the directory public_html which you'll see in your home
directory on turing. Please do not put any file that ends in
.java in your public_html directory. This makes
your code available to the public and is thus a violation of the code
sharing policy. Finally, go into your public_html directory
and type "chmod ugo+rx *". This makes the applet accessible on
the web. (This is OK, since only the compiled version of the code can
be seen by the web, not your original source code.)
Now you can run the applet by going to the website:
www.cs.hmc.edu/~username/Spamsweeper.html where
username
is your login name on turing. Don't forget the tilde symbol before
your username.
Please sumbit all of the .java files (Boardcell.java, Gameboard.java, Spamsweeper.java, Spamsweeper.html, and whatever else
you add on your own).
For Totally Optional Bonus Credit create an implementation called
Bonussweeper.java applet
(and any other auxilliary files)
which augments Spamsweeper with one or more of the following features
(extra credit for each feature):
- A "New Game" button which allows the player to start over again.
Consult a Java book or the online java documentation from the CS 60
web site to see how buttons and other similar features can be added.
- A feature which ensures that the player's first probe
never detonates a can of Spam (that Spam can is just moved).
- A clock which times the duration of the game. (Consult a Java
book for details on how to do this.)
- Any additional features that you think would be cool.
Make sure that at the top of the file Bonussweeper.java you clearly
document which of these features you have added.
Last modified September 2003 by Ran Libeskind-Hadas