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:
  1. Learn how to read and understand code
  2. Write code that fits into an existing game
  3. Keep the terriers hungry as long as possible
  4. 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:
  1. Copy the code from UserInterface.java into the package.
  2. Copy the code from GameEngine.java into the package.
  3. Copy the code from Inferface.java into the package.
  4. Copy the code from Terrier.java into the package.
  5. Copy the code from Squirrel.java into the package.
  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 data file Simple Game into the project.
  10. Copy the data file Two Terriers into the project.
  11. Copy the data file Three Squirrels into the project.
  12. Copy the data file Squirrel Nightmare into the project.
  13. Only modify Squirrel.java, do not modify other provided files.
  14. 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:
  1. 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.
  2. 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.
  3. 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:
  4. 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.
  5. 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.

Grading Criteria

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.