Terriers and Squrrels

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

Getting Started

Create a new Java project called P4 and import P4-starter.jar.

Your directory should look like this:

P4/
├── resources
│   ├── images
│   │   ├── iconFence.png
│   │   ├── iconGrass.png
│   │   ├── iconMunch.png
│   │   ├── iconSquirrel.png
│   │   ├── iconTerrier.png
│   │   └── iconTree.png
│   └── mazes
│       ├── SimpleGame.txt
│       ├── SquirrelEscape.txt
│       ├── SquirrelFence.txt
│       ├── SquirrelNightmare.txt
│       ├── TwoSquirrels.txt
│       └── TwoTerriers.txt
└── src
    ├── Animal.java
    ├── GameEngine.java
    ├── Squirrel.java
    ├── Terrier.java
    └── UserInterface.java

Run the GameEngine class with resources/mazes/SimpleGame.txt. The playing field will appear but not be updated correctly. If GameEngine is not running or has compile errors, get some help in the lab.

Description

The purpose of the assignment is to finish three Java classes that implement the behavior of:

  1. Squirrels that are trying to evade terriers

  2. Terriers that feel compelled to chase squirrels.

You will code the behavior of both animals. The game is played on a field, with some number of terriers and squirrels whose placement is determined by a file. The field is a two-dimensional array, with 'S' for Squirrel, 'D' for Terrier, 'T' for Tree, 'F' for Fence, and '-' for empty squares with Grass. Here is a description of the classes involved in this assignment:

  • The GameEngine class has the main method. It reads the file specified on the command line, and instantiates the UserInterface object to put up the playing field. Approximately every couple of seconds it asks each squirrel and terrier which way they want to move, then updates the playing field accordingly. You should NOT change this class.

  • The UserInterface class provides the graphics for the game. When instantiated by the GameEngine, it displays a window with the playing field and a counter. After each set of moves, the GameEngine asks the UserInterface object to redraw itself. The playing field array is passed to the UserInterface object in its constructor. You should NOT change this class.

  • The Animal class is an abstract class that has code that is shared between the Terrier and Squirrel class. You must complete several methods in this class, as described below. The GameEngine continually asks the terriers and squirrels to move, and this class helps with that, but defers parts of the task that are specific to a particular animal to the Terrier and Squirrel classes. The Animal class has code to figure out the closest terrier to a squirrel, and the closest squirrel to a terrier.

  • 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 field.

  • 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 field, and they cannot climb trees or pass through fences.

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 GameEngine reports all movements and significant events. A UML diagram of the classes in the Terriers and Squirrels is shown below:

UML
Testing

You must setup a run configuration for the project with the name of the text file containing the game. Start by testing the individual methods you have written, using "SimpleGame.txt", field, and make sure the Squirrel locates the closest Terrier, and moves in the opposite direction, and avoids going off the field. Then make sure the Terrier locates the closet Squirrel and chases it, and avoids going off the field. The simplest game has only one of each animal. Then proceed to the more difficult fields.

Instructions

Use the javadocs to read implementation details about each method

Here are the complete list of methods you must implement to complete the game, in the optimal order:

  1. makeMove() in Animal.java

  2. computeDistance() in Animal.java

  3. findClosest() in Animal.java

  4. findMove() in Terrier.java

  5. findMove() in Squirrel.java

  6. isValid() in Terrier.java

  7. isValid() in Squirrel.java

Submissions

Please follow the usual rules for submitting Java programs.

  • The name of the Java archive must be exactly P4.jar and must contain only Animal.java, Squirrel.java and Terrier.java.

  • Comments at the top with your name, date and course.

  • We expect programming assignments to be implemented using Java 1.8.

  • We will be checking programs for plagiarism, so please don’t copy from anyone else.

Grading Criteria
  • 100 points for perfect submission.

  • 0 points for no submission, will not compile, submitted class file, etc.

  • Preliminary Tests

    • compileTest: checks that program compiles. (0 points)

    • test1: checks the behavior of both animals on SimpleGame.txt. (15 points)

    • test2: checks the behavior of both animals on TwoTerriers.txt. (15 points)

    • test3: checks the behavior of both animals on TwoSquirrels.txt. (15 points)

    • test4: checks the behavior of both animals on SquirrelEscape.txt. (20 points)

    • test5: checks the behavior of both animals on SquirrelNightmare.txt. (20 points)

  • Final Tests

    • Final grading includes the preliminary tests.

    • test6: checks the behavior of both animals on SquirrelFence.txt. (15 points)


Important
Submit P4.jar to Checkin