CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Assignment 7: Turtle hunt


A turtle watches out for bombs.

Due: 2:00pm, Monday, October 5. Value: 30 pts. Submit to Sauron.

You should first download the TurtleHunt program as your starting point. (Right-click TurtleHunt.java, select Save Link As… or Save Target As… from the pop-up menu, and save the file on your computer. Then select Open… from Jigsaw's File menu and open the file you just saved.) This program displays several turtles randomly placed in the window. It also displays a transparent-red target, controlled by the mouse, representing the area that would be affected should a bomb be dropped.

This assignment is split into two parts. You should complete and test the first part, then go on to complete the second part. However, what you submit should be a single program incorporating solutions to both parts.

Part A: Dropping bombs

Your assignment is to alter this program so that when the user presses the mouse button, all turtles that intersect with the target will dive underwater. (That is, they disappear from view. But don't think they're dead: They're just hiding underwater. This is a non-violent assignment!) This yields a puzzle for the user: How few bombs will it take to send all the turtles to swim with the fishes?

We haven't seen yet how to interact with the mouse, but it's rather simple: The run method must invoke addMouseListeners (as has already been done in the starter code); then with each mouse click, the computer will invoke the mousePressed method at the bottom of the file. Your work for Part A will be focused entirely on the last bit of the file, which is the mousePressed method. This method already uses getX and getY to retrieve the mouse cursor's location in the window when the button was pressed.

To identify which turtles lie in the target, you'll want to use the GraphicsProgram class's getElement and getElementCount methods to iterate through all objects in the window, and you can use instanceof to identify which of them are GTurtles. To compute whether the target overlaps with a turtle, assume that each turtle is a radius-10 circle. Of course, this isn't precisely correct, but it simplifies things tremendously and works well enough in practice. You can compute the distance between the turtle and the bomb using the distance formula.

the square root of the sum of the square of the
	difference in x-coordinates and the square of the difference
	in y-coordinates

Since the target has a radius of 50, and a turtle a radius of 10, a turtle will intersect the target if its center is less than 60 pixels from the target's center. In the code I've given you, the turtles and targets are arranged so that their getX and getY methods return the coordinates of the object's center.

When you find a turtle that intersects with the target, you can make it disappear from the window using its setVisible method. This method takes a boolean as a parameter; if the parameter is false, the turtle will disappear from view.

Note: You should not use instance variables in either part of this assignment. We haven't learned about these, so you probably won't be tempted to do it anyway. If Jigsaw wants you to use the word private, then you're using instance variables, and you shouldn't be. To find the target, you can use the getElement method, as seen in mouseMoved.

Part B: Counting bombs

Modify the program so that it displays a GLabel somewhere in the window, displaying how many bombs have been dropped so far, in a format such as Bombs dropped: 43. The code you write for Part B will appear in two places: You'll want to modify the run method at the top of the file in order to place the label in the window initially; and you'll also want to modify the mousePressed method to update the label with each mouse press.

The GLabel class will allow you to have a string appear in the window. The constructor and methods you will most likely use for this assignment are the following.

GLabel(String text, double x, double y)

(Constructor) Constructs a label containing text. The baseline of the text's left edge will be at (xy).

String getLabel()

Returns the string displayed by this label.

void setLabel(String value)

Changes the string displayed by this label to value.

void setFont(String fontName)

Changes the font used to display this label. The parameter fontName should be in a format such as serif-plain-28, which indicates to use a 28-point plain serif font. (Alternatives for serif include sansserif, monospaced, or a proper font name; alternatives for plain include bold and italic.)

In updating the label, you'll want some way to determine how many bombs have already been dropped. To do this, you'll want to locate the existing GLabel, retrieve its text using getLabel, use String's substring method to extract the portion of the string containing the digits, and use the Integer class's static method parseInt to get the corresponding integer value.