|
|
 |
 |
 |
|
Cat and Mouse
|
|
|  |
by Mike Clancy University of California Berkeley, California
 |
|
|  |
More Nifty Assignments...
Background
The scene is an urban park; a cat watches a mouse run around the base of a statue of the first computer. Over the course of a minute, the somewhat witless mouse moves 1 meter counterclockwise around the statue's base, which is circular and 2 meters in diameter. Every 60 seconds, the cat pursues the mouse as follows:
- If the cat can see the mouse, the cat moves 1 meter toward the statue.
- If the cat can't see the mouse, the cat circles 1.25 meters counterclockwise around the statue.
The cat plans eventually to get close enough to the mouse to make a juicy lunch of it. Should the chase go on for more than 30 minutes, however, the cat will get tired and wander off.
Problem
Complete an applet to simulate this situation and determine if the cat catches the mouse. The applet instantiates a Cat object and a Mouse object, each having a move method, and sets up a button that when clicked executes the next step of the simulation (either a mouse move or a cat move). Clicking the button once the cat has caught the mouse or abandoned the chase (after 30 simulated minutes) merely causes an error message to be displayed.
Program Files
The files CatMouseChase.fw.java, Position.fw.java, Cat.fw.java, and Mouse.java are described ("fw" stands for "framework") below. The java files are available in a single zip below.
Cat_Mouse_Java.zip
- CatMouseChase.fw.java supplies the paint and init methods for you, along with a couple of auxiliary functions. The init method retrieves initial position values for the cat and the mouse from the HTML file used to start the applet and initializes a cat and a mouse appropriately. It also sets up a status string that represents the progress of the simulation. The paint method draws a simplified representation of the cat, the mouse, and the statue, and also displays the cat and mouse positions and the status string.
- Position.fw.java defines the Position class. Positions are represented in polar coordinates. Each Position object consists of a double radius and a double angle; all these are private, and the Position class should not provide direct access to them. Methods provided for you in the Position class are several constructors, toString (to allow positions to be printed), and xCoord and yCoord methods that convert "real world" coordinates to window pixel coordinates. You are to supply an update method to increment the position's radius or angle. You will also need several comparison methods to allow a user to determine if a position is at the base of the statue, if it is at the same coordinate as another position, if its angle is between two other angles, and if a position on the base of the statue is in its line of sight.
- Cat.fw.java and Mouse.java define the Cat and Mouse methods, respectively. Both have a move method and a constructor. Neither class is allowed direct access to the components of a Position. The complete Mouse class is provided for you; you must provide the move method of the Cat class. The Mouse move method doesn't return anything. The Cat move method returns true if the cat eats the mouse in the move and returns false otherwise.
Don't change the code provided for you; merely add methods as specified above.
Applet Input
You provide initial positions for the cat and mouse in the HTML file that starts the simulation, using the param construct; its format is:
‹param name=" parameter_name " value= parameter_value ›
To supply cat and mouse positions, use parameter names "mouseangle", "catradius", and "catangle" whose values are expressed in radians. For example, an HTML file that starts the cat on the x axis 3 meters from the statue and the mouse on approximately the opposite side of the statue contains the following specification:
‹applet code="CatMouseChase.class" width=600 height=500›
‹param name="mouseangle" value=3.14›
‹param name="catradius" value=4.0›
‹param name="catangle" value=0.0›
‹/applet›
Supply sufficient HTML files to test your applet thoroughly, making sure that every statement in your code has been exercised at least once.
Trigonometric Information
- The cat sees the mouse if
(cat radius) * cos (cat angle - mouse angle)
is at least 1.0.
- When the cat circles distance d around the statue, its radius does not change, and the change in its angle can be calculated from the following relationship:
d = angle * (radius of arc)
- The cat catches the mouse when it (the cat) moves past the mouse while at the base of the statue, i.e., when the cat radius is 1.0 and the mouse angle lies between the old cat angle and the new cat angle. An angle B is between angles A and C in the following circumstances:
cos (B - A) > cos (C - A) and
cos (C - B) > cos (C - A)
The difference C - A is assumed to be less than 90 degrees, or (pi)/2 radians.
- Note that the cat cannot move inside the statue's base; hence if the cat is, say, at radius 1.7 and sees the mouse, it can move in only 0.7 meters, up to the base of the statue.
- Remember that angles a , a + 2(pi), a + 4(pi) ... are all equal.
Suggested Approach
Your first task should be a design task: outline the cat's move algorithm, then figure out what methods the Position class should provide. Then start on the coding and the testing.
You should especially test the Position methods in isolation. To simplify this, set up an applet with an init method that tests the Position methods and outputs results with drawString.
Be sure to check that the output makes sense for all your tests; a student is sometimes embarrassed to have someone point out that his or her program has the cat moving into and through the statue, or moving away from the mouse instead of approaching it.
Checklist
Correctly working code:
- Completion of the Cat move method
- Completion of the Position class
- Completion of the CatMouseChase actionPerformed method
Sufficient testing:
- Applet to test the Position methods
- An init method in that applet that supplies sufficiently comprehensive tests
- HTML files that exercise all statements in the remainder of the applet
Good style:
- Appropriate use of indenting and white space
- Variable and method names that reflect their use
- Informative comments at the head of each method
- Clean case analysis
Teacher's Comments
This is a cute problem that requires nontrivial geometry and algorithms, but it can be solved in 100 lines.
Contribute
If you would like to contribute your suggestions for nifty assignments, please submit your ideas.
|
|
|
|
|
|