CS 160, Summer 2016
Programming Assignment P8
Terriers and Squirrels

Programming due Monday, Aug. 1 at 11:59pm, no late submissions.

This programming assignment has four objectives:
  1. Learn how to read and understand code.
  2. Write code that fits into an existing game.
  3. Keep the squirrels alive as long as possible.
  4. Help the terriers hunt down the squirrels.


The purpose of the assignment is to finish two Java classes that implement the behavior of 1) a squirrel that is trying to evade terriers, and 2) a terrier that feels compelled to chase squirrels. The game is played on a board, with some number of terriers and squirrels whose placement is determined by a file. The board is a two-dimensional array, with 'S' for Squirrel, 'D' for Terrier, 'T' for Tree, 'F' for Fence, and '-' for empty squares with Grass.

The game engine reads the file specified on the command line, builds a user interface. Approximately every couple of seconds it asks each squirrel and terrier which way they want to move. You are going to code the behavior of both animals.

Terrier behavior is defined in Terrier.java. Terriers always try to chase after the nearest squirrel and eat it. Terriers must avoid running into other terriers or going off the board, and they cannot climb trees or pass through Fences.

Squirrel behavior is defined in Squirrel.java. Squirrels look for the closest terrier and always move in the opposite direction. Squirrel must avoid running into other squirrels or terriers, but they can pass through fences or climb trees. If they make it to a Tree, they are safe and disappear from the board.

The game continues until all squirrels are safe or eaten, or for 30 iterations, whichever comes first. None of the supplied fields require that many iterations. The game engine reports all movements and significant events.


NOTE: This assignment is complex, make sure and read the instructions carefully!

Part One

Create a project called P8, and download the following files:
  1. Copy the code from UserInterface.java into P8/src.
  2. Copy the code from GameEngine.java into P8/src.
  3. Copy the code from AnimalInterface.java into P8/src.
  4. Copy the code from Terrier.java into P8/src.
  5. Copy the code from Squirrel.java into P8/src.
  6. Copy the image file iconTerrier.png into the project.
  7. Copy the image file iconSquirrel.png into the project.
  8. Copy the image file iconMunch.png into the project.
  9. Copy the image file iconFence.png into the project.
  10. Copy the image file iconTree.png into the project.
  11. Copy the image file iconGrass.png into the project.
  12. Copy the image file squirrelPrints.png into the project.
  13. Copy the image file dogPrints.png into the project.
  14. Copy the data file SimpleGame.txt into the project.
  15. Copy the data file TwoTerriers.txt into the project.
  16. Copy the data file TwoSquirrels.txt into the project.
  17. Copy the data file SquirrelEscape.txt into the project.
  18. Copy the data file SquirrelFence.txt into the project.
  19. Copy the data file SquirrelNightmare.txt into the project.
  20. Only modify Squirrel.java and Terrier.java, do not modify other provided files.
  21. Submit a P8.jar file with (only) Squirrel.java and Terrier.java.
  22. Don't forget to put your name and the date in the comments.
After downloading and building the files, you should be able to run the GameEngine, but the Squirrel will always move right, and the Terrier will always move left. Both will soon go off the board, causing an exception. If GameEngine is not running at all, get some help in the lab.

Part Two

Next, modify the code in Terrier.java and Squirrel.java, which both implement the following interface:
public interface AnimalInterface{
    public enum eMove {
    // Find the closest squirrel or terrier
    public void findClosest();

    // Report the closest position
    public int getClosestCol();
    public int getClosestRow();
    // Move the animal, according to the rules 
    public void moveAnimal();
    // Report the current position
    public int getCurrentRow();
    public int getCurrentCol();

    // Report the previous position
    public int getPreviousRow();
    public int getPreviousCol();
Complete the Squirrel object, as follows:
  1. Complete the non-static method findClosest to find which Terrier is the closest:
  2. Write the moveAnimal method in AnimalInterface. This move has several steps, which can all be included in the method, or you can add private methods for each step:
  3. To give you an idea of the scope of Squirrel.java, I wrote a method of ~15 actual lines to find the closest Terrier, ~20 lines to select the move, ~30 lines to adjust the move, ~10 lines to make the move, and ~10 lines for a private method to see if a particular move is valid. There is virtually all of the code required.
Complete the Terrier object, as follows:
  1. Write the findClosest method to which is similar to the Squirrel class, but instead of finding the closest Terrier you'll be finding the closest Squirrel.
  2. Write the move method in AnimalInterface that selects the desired move for the Terrier, which should be in the direction of the closest Squirrel.
  3. Terrier.java is similar in size to Squirrel.java, and most of the code can be cloned, with minor changes.


Start by testing using the Simple Game (SimpleGame.txt) field, and make sure the Squirrel locates the closest Terrier, and moves in the opposite direction, and avoids going off the board. Then make sure the Terrier locates the closet Squirrel and chases it, and avoids going off the board. The Simple Game has only one of each animal. Then proceed to the more difficult boards.

Grading Rules

Please follow the usual rules for submitting Java programs.

Grading Criteria


To create the P8.jar file containing Terriers.java and Squirrels.java, you must export the project from Eclipse in the JAR format. The default in Eclipse is to make a JAR with Terriers.class and Squirrels.class. This will not pass automated grading. To avoid this and correctly submit the source files, follow these directions: The default on the second to last item is "Export Java class files and resources", which will not work. Here is an image that shows which box to select:

Export Java source files with Eclipse.

© 2016 CS160 Colorado State University. All Rights Reserved.