Terriers and Squrrels
-
Learn how to read and understand code
-
Write code that fits into an existing game
-
Keep the squirrels alive as long as possible
-
Help the terriers hunt down the squirrels
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.
The purpose of the assignment is to finish three Java classes that implement the behavior of:
-
Squirrels that are trying to evade terriers
-
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 theUserInterface
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 theGameEngine
, it displays a window with the playing field and a counter. After each set of moves, theGameEngine
asks theUserInterface
object to redraw itself. The playing field array is passed to theUserInterface
object in its constructor. You should NOT change this class. -
The
Animal
class is anabstract
class that has code that is shared between theTerrier
andSquirrel
class. You must complete several methods in this class, as described below. TheGameEngine
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 theTerrier
andSquirrel
classes. TheAnimal
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:
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.
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:
-
makeMove() in Animal.java
-
computeDistance() in Animal.java
-
findClosest() in Animal.java
-
findMove() in Terrier.java
-
findMove() in Squirrel.java
-
isValid() in Terrier.java
-
isValid() in Squirrel.java
Please follow the usual rules for submitting Java programs.
-
The name of the
Java
archive must be exactlyP4.jar
and must contain onlyAnimal.java
,Squirrel.java
andTerrier.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.
-
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 |