CS 160, Spring 2015
Programming Assignment P12
Terriers and Squirrels
Programming due Wednesday, May. 6 at 11:59pm, no late period.
This programming assignment has four objectives:
- Learn how to read and understand code
- Write code that fits into an existing game
- Keep the terriers hungry as long as possible
- Keep the squirrels alive as long as possible
Description
The purpose of the assignment is to finish a Java class that implements
the behavior of a Squirrel that is trying to evade Terriers that want to
eat it! The game is played on a board, with some number of terriers and
sssquirrels whose placement is determined by a file with the starting positions.
The board is a two-dimensional array, with 'S' for squirrels, 'T' for terriers,
and '-' for empty squares.
The game engine reads the file specified on the command line, builds a
user interface. Approximately every two seconds it asks each Squirrel and
Terrier which way they want to move. The Terrier is written by the instructor.
Terrier behavior is to find the nearest Squirrel and eat it, avoiding running
in to other Terriers or going off the board.
You must implement the behavior of the Squirrel to find the closest Terrier
and move in the opposite direction. You must also prevent the Squirrel from going
off the board. You may look at the Terrier to see how some of this might be done,
Instructions
NOTE: This assignment is complex, make sure and read the instructions carefully!
Part One
Create a project called P12, with a package called "myPackage" instead of using
the default package. Setup the project as follows:
- Copy the code from UserInterface.java into the package.
- Copy the code from GameEngine.java into the package.
- Copy the code from Inferface.java into the package.
- Copy the code from Terrier.java into the package.
- Copy the code from Squirrel.java into the package.
- Copy the image file iconTerrier.png into the project.
- Copy the image file iconSquirrel.png into the project.
- Copy the image file iconMunch.png into the project.
- Copy the data file Simple Game into the project.
- Copy the data file Two Terriers into the project.
- Copy the data file Three Squirrels into the project.
- Copy the data file Squirrel Nightmare into the project.
- Only modify Squirrel.java, do not modify other provided files.
- Don't forget to put your name and the date in the comments.
At this point you should be able to run UserInterface, but the Squirrel will always move left,
and will soon go off the board, causing an exception. If UserInterface is not running at all,
get some help in the lab.
Part Two
Complete the Squirrel object, as follows:
- Write a private method that figures out which Terrier is the closest.
To do so you must scan the board from top to bottom and left to right,
computing the distance to each Terrier that you find. If Terriers are at
an equal distance, store the data for the first Terrier you find.
- Write a private method that selects the correct move for the Squirrel,
which should be in the opposite direction from the closest Terrier. For example,
if the Terrier is left on the same row, move right. If the Terrier is below
on the same column, move up. If the Terrier is above and right, move down and left.
If the Terrier is below and right, move up and left, etc.
- Next you must avoid going off the board, by carefully implementing the
following behavior (in the order shown), after deciding above which direction to move:
- If you are planning on moving DOWN_LEFT, DOWN, or DOWN_RIGHT, and
the Squirrel is in the bottom row, move LEFT instead.
- If you are planning on moving DOWN_LEFT, LEFT, or UP_LEFT, and
the Squirrel is in the leftmost column, move UP instead.
- If you are planning on moving UP_LEFT, UP, or UP_RIGHT, and the
Squirrel is in the top row, move RIGHT instead.
- If you are planning on moving DOWN_RIGHT, RIGHT, or UP_RIGHT, and the
Squirrel is in the rightmost column, move DOWN instead.
- Do not worry about colliding with a Terrier or another Squirrel, the
game engine will prevent that, and it will update your position as required.
- To give you an idea of the scope of Squirrel.java, I wrote a method of
~20 lines to find the closest Terrier, ~20 lines to figure out how to move
in the opposite direction, and ~15 lines to avoid going off the board.
Testing
Start by testing using the Simple Game board, and make sure you are locating
the closest Terrier, moving in the opposite direction, and avoiding going off
the board.
Grading Rules
Please follow the usual rules for submitting Java programs.
- Work on your own.
- The name of the source code file must be exactly Squirrel.java
- Comments at the top with your name, date and course.
- We expect programming assignments to be implemented in Eclipse
using Java 1.5 or 1.6 or 1.7 or 1.8.
- We will be testing the code on the machines in the CS computer lab,
so make sure your code runs on those machines.
- 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)
- test0: checks that you can find the closest terrier. (20 points)
- test1: checks that you move in the correct direction. (20 points)
- test2: checks that you stay on the board (10 points)
- test3: checks the behavior of the squirrels on "Simple Board". (10 points)
- test4: checks the behavior of the squirrels on "Two Terriers". (10 points)
- test5: checks the behavior of the squirrels on "Three Squirrels". (15 points)
- Final Tests
- test6: checks the behavior of the squirrels on "Squirrel Nightmare". (15 points)
- Final grading includes the preliminary tests.
Submission
Submit your modified source file named Squirrel.java to the the Checkin tab on the course web site.
© 2015 CS160 Colorado State University. All Rights Reserved.