CS 51 Test Program #1
Due: Friday, October 14, at 4 PM

A test program is a laboratory that you complete on your own, without the help of others. It is a form of take-home exam. You may consult your text, your notes, your lab work, or our on-line examples and web pages, but use of any other source for code is forbidden. You may not discuss these problems with anyone aside from the course instructor. You may only ask the TA's for help with hardware problems or difficulties in retrieving your program from a disk or network.

Complete each of the following problems, documenting your code well. You are encouraged to reuse the code from your labs or our class examples. Submit your code in the usual way by dragging it into the Dropoff folder. Please do not submit three separate folders. Instead, place the folders for all three of your complete programs into one folder, make sure that your name appears in the title of the main folder and each of the subfolders, and then place the main folder in our dropoff folder.

Problem 1: A new broom sweeps clean

For the first problem, you are to write a program that emulates a broom that can sweep a rectangle around the screen. The broom will be represented by a small ball with diameter 10 that appears when the mouse is pressed and disappears when the mouse is released. While the mouse is depressed, the ball is dragged along by the mouse (the upperleft corner of the ball is always at the mouse location). There is also a filled square (30 pixels to a side) showing on the screen. If the broom bumps into the square during a drag, then the square will move the same distance (and in the same direction) as the broom, as indeed it would if you had a real broom.




Notice that we are asking you to have the broom push the square, not just drag it. Thus if the mouse is depressed when it is not over the square, but then is dragged over the square, the square should be pushed ahead of the mouse (that is, after the drag, the mouse will still not be on top of the square). Of course if the mouse is depressed on top of the square, then the broom will drag the square, but this program should also be able to push (and the code to do the pushing will automatically do that dragging). Please use the method overlap to determine if the broom has been moved over the square.

One warning: If you are pushing the square diagonally, the square will have a tendency to slide off of the broom. This is because movements parallel to the side of the square will not affect the square (and the computer is so fast that some of your diagonal moves will end up parallel to the square). This is a "feature" not a "bug" (this is what happens with real brooms), so don't worry about it as long as the square generally is moved along with the broom.

Problem 2: Track the Zombies

The Dean noticed a sudden infestion of zombies on campus last week. After careful consideration of the rights of both zombies and students, the Deans have rejected the option of targetted assassinations and have instead elected to begin studying the phenomenon by having a campus security guard follow groups of zombies and phone in changes to their locations to the Dean's crisis room, where locations of zombies will be plotted on a large map of the campus.

While advanced CS students have been charged with computing search strategies for computer-controlled predator drones, your assignment is to help the Deans keep track of the zombies by plotting the zombies and their movements on a computerized campus map. Because this is a prototype, you are allowed to simplify the problem considerably.

In your program, there will only be a single trio of zombies, who all follow a specified leader (staying in the same order at all times). Your program will begin by displaying a map of the canvas filling the screen. (The map can be found in the file "CampusMap.jpg". The window should be set to be 792 by 792 so that the map will fill the canvas.) At the program start the leader of the three zombies will be located on College Avenue in front of Seaver North, heading south, with the other two zombies following behind (i.e., they are both to the north of the leader). Hint: In the starter, we provide you with the location of the lead zombie.

In response to reports from campus security, the Deans will click to one side of the lead zombie to tell the program which way the zombies should move. When the lead zombie moves, the second zombie will move to the location where the lead zombie was, while the last zombie moves into the position formerly occupied by the second zombie.

The leader only moves left, right, up, or down, never diagonally. The followers should not move if the mouse is clicked in any diagonal direction. When moving left and right, a zombie moves a complete body width (44 pixels). When moving up and down, a zombie moves a complete body height (66 pixels). The leader is considerate, so if you click the mouse in a direction that would cause the leader to step on the second zombie, the leader does not move.

Zombies don't like being pushed around. If you press the mouse on any of the zombies, the leader screams maniacally. We haven't covered playing sounds, so we'll be satisfied by having you displaying a red "Scream!" message to the right of the leader. When you release the mouse, the message should go away.

This question tests your ability to define classes. Thus you should include a class Zombies that represents the group of three zombies. It will include instance variables for the VisibleImages representing the three zombies, a constructor, and the following methods. A method moveToward to move the followers (similar to the hopToward method of the Frog class), a contains method to determine if the user has clicked on any of the zombies, and a pair of methods screechStart and screechStop that trigger the display and hiding of the "Scream" message. You should decide what parameters each of these methods and the constructor need.

To make your life easier, we will supply a private method

    int moveDirection(Location point)

that will return an integer code indicating which direction the location point is from the lead zombie. The constants returned are named NORTH, SOUTH, EAST, WEST, and NONE (the latter is returned if the click is on the lead zombie or in a diagonal direction).

We provide two images of zombies: ZombieGirl.jpg and REZombie.jpg. You should scale them both to the same size (ZOMBIE_WIDTH by ZOMBIE_HEIGHT) when you construct them. You can decide which image to use as the leader, but we recommend that you pick one to be the leader and use the other image for the other two zombies. This will make it easy for you to remember which one is the leader so you can be sure the zombies are moving correctly.

Your applet should be 792 pixels wide by 792 pixels tall.

A running version of the program should be visible below if your browser handles applets correctly:



You may notice that when the leader screeches, the message is always to the right of the leader. This means that if the leader is at the right edge of the canvas, the message will not be seen. Also, if the second zombie is to the right of the leader, the message will be on top of that zombie. For extra credit, make sure that the scream is always visible and not on top of the second zombie.

Problem 3: Catching Fireflies

For this problem, you will write a program that simulates the fun of catching fireflies. When the game starts, a firefly will start flying across the screen at a constant speed. (You need not worry about computing elapsed time for this project, just always move the firefly by the same amount each step.)

The user will control a "net" by moving a mouse around the screen. If the firefly is inside the net, and the user presses the mouse button, then the firefly will be captured inside the net (and the net will "light up" to the color of the firefly to show this). If the mouse is released inside the jar when the firefly is caught in the net, then the count of the number of fireflies captured should increase. If the mouse is released anywhere else, then the firefly "escapes", and the count is not updated. Either way, when the firefly is released, a new firefly begins going across the window. The game will end if a firefly makes it all the way across the screen without ever being caught in the net.

You will need a FireflyGame class that extends WindowController, and a Firefly class that extends ActiveObject to represent the firefly. The jar and the net can be represented as simple drawing objects, and do not need separate classes. You may set the speed of the firefly as you wish, but remember that fireflies are easy to catch!

The FireflyGame class will need methods onMousePress, onMouseMove, onMouseDrag, and onMouseRelease, as well as the begin method. Note that to catch the firefly when the mouse is pressed, we only need for the center of the firefly to be inside the net.

The Firefly class will need a constructor and run method, as well as methods getBugCenter() and setCaught. The method getBugCenter should return a location corresponding to the center of the firefly, while setCaught should set a variable so that the firefly will stop moving. After the bug stops moving, its representation should be removed from the canvas. Note that the bug will be represented by a small red circle.

Your applet should be 400 pixels wide by 400 pixels tall.

A running version of the program should be visible below if your browser handles applets correctly. If it has already terminated (because the firefly made it across the screen), reload the page to restart the program.



Grading Point Allocations

Value

Feature
Code Quality & Readability (16 pts for each of 3 programs)
2 pts. use of boolean conditions
2 pts. ifs/whiles
2 pts. appropriate vble (instance/local, public/private)
2 pts. Descriptive comments
2 pts. Good names
2 pts. Good use of constants
2 pts. Appropriate formatting
2 pts. Parameters used appropriately
Correctness (16 pts for each of 3 programs)
Broom
4 pts. Drawing the screen initially
4 pts. Dragging the broom
4 pts. Pusing the "dirt"
4 pts. Showing/hiding the broom
Follow the leader
4 pts. Drawing the screen initially
4 pts. Lead zombie moving in the correct direction
4 pts. Other zombies following
4 pts. Screeching when pressing mouse on zombies
Firefly
2 pts. Net moves with the mouse
3 pts. Firefly moves smoothly across the screen
3 pts. Firefly captured when mouse pressed over firefly
3 pts. Releasing firefly in jar adds to score
3 pts. Releasing firefly anywhere results in new firefly
2 pts. Game ends when firefly gets across screen
Miscellaneous (4 pts total)
Extra Credit (4 pts maximum)