Rabbit Hunt

Supporting files: ZIP file containing all

Controller.java
Direction.java
Entity.java
Fox.java
Game.java
Grader.java
MessageView.java
Model.java
Player.java
Rabbit.java
RabbitHunt.java
View.java

Administrative note: The original designer of this assignment, David Matuszek of the University of Pennsylvania, assigned grades as the percentage of times that their Rabbit program succeeds. I have not tried the assignment in the classroom, so I can't vouch for it myself. Additionally, I have rewritten his code; I don't think the changes make success substantially more difficult, but it's possible.

Objectives:

A fox is hunting a rabbit in a field. The field contains a number of bushes which obstruct both the fox's view and the rabbit's view, so each may or may not be able to see the other. If the fox catches the rabbit, the fox eats it and wins. If the rabbit can keep away from the fox for 100 turns, the rabbit wins.

You are the rabbit.

When you compile the distributed files and run the RabbitHunt class, you will see a diagram of the field.

The big red dot represents the fox, the small brown dot is the rabbit, and the green blobs are bushes.

Run the program a few times and see what happens. Notice that you can step through the hunt, or you can just let it run. You can adjust the speed at which the animation proceeds. (This does not affect what happens, only how quickly it happens.) The rabbit almost always meets an untimely end. However, occasionally the rabbit will evade the fox for 100 turns, and the rabbit wins - this doesn't happen very often.

Now look at the Rabbit class. It has a constructor method, and it has a decide method for determining how the rabbit moves each turn. The rabbit I've given you is a really stupid rabbit - it just moves randomly.

Your job is to improve the Rabbit class so that the rabbit escapes more often. You can edit this file in any way you wish; however, you cannot edit the other files, although you are welcome - and encouraged - to look at them.

Your program should not assume that the grid is always 22x22, even though the RabbitHunt program happens to use a 22x22 grid. Your class can access the grid's size via the Game class described below. You can easily test other grid sizes yourself by editing the first line of RabbitHunt's main method.

It is probably impossible to find a strategy for the rabbit that ensures a 100% survival rate.

Useful classes

There are several classes in the program; besides the Rabbit class, you will be able to make use of four others. This section briefly describes these classes and what you might find useful in each. You can get additional information by reading the classes themselves.

Although your code will not be able to use it directly, the Fox class is a useful example of a sophisticated animal using all of these classes together to compute its own decision in each turn.

Direction class

Direction objects represent one of the eight objects that an animal can move each turn. Because the constructor is private, you cannot create Direction objects; you can, however, access them through the constants provided in the class, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, etc.

static int NUM_DIRECTIONS The number of directions (8).
int getDeltaX() Returns the change in columns with each step in this direction.
int getDeltaY() Returns the change in rows with each step in this direction. (Note that northward movement increases the row index.)
Direction rotate(int steps) Returns the direction clockwise from this direction by steps 45-degree steps.

Entity class

Represents something in the field - a bush, a rabbit, or a fox. Your class will see Entity objects in two ways: It receives the rabbit entity via its constructor, and Player's look method will return other entities.

boolean isBush() Returns true if this entity is a bush.
boolean isRabbit() Returns true if this entity is a rabbit.
boolean isFox() Returns true if this entity is a fox.
int getRow() Returns the row that this entity currently occupies.
int getColumn() Returns the column that this entity currently occupies.
int distanceTo(Entity other) Returns the minimum number of steps to get from this entity to another.

Player class

Your Rabbit class extends the Player class. Notice that there is both a Rabbit player and a rabbit entity; you can think of the player as the rabbit's mind, and the entity as the rabbit's body. The Rabbit class inherits some useful methods from the Player class.

Entity look(Direction where) Returns the first entity visible in the given direction.
boolean canMove(Direction where) Returns true if the move is legal - that is, if there isn't a bush blocking the way.
int getStepCount() Returns the number of steps completed so far.

Game class

The Game class provides data related to the overall game. Your class will receive the Game object via the constructor method.

static int MAX_RABBIT_STEPS The number of steps the rabbit needs to survive.
int getRowCount() Returns the number of rows in the grid.
int getColumnCount() Returns the number of columns in the grid.
int randomInt(int min, int max) Returns a random integer from the range given, inclusive.
Direction randomDirection() Returns a randomly chosen direction.
Whenever your class wants to do something random, it should use randomInt or randomDirection. If you don't do this, than choosing replay in the program won't work accurately.