Question: Create a new Java project called P4 and import P4-starter.jar. Download here: http://www.cs.colostate.edu/~cs165/.Spring18/assignments/P4/archive/P4-starter.jar Your directory should look like this: P4/ resources images iconFence.png iconGrass.png iconMunch.png

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

Download here: http://www.cs.colostate.edu/~cs165/.Spring18/assignments/P4/archive/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:

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 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:

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

Javadocs Here: http://www.cs.colostate.edu/~cs165/.Spring18/assignments/P4/doc/javadoc

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

// Animal.java

public abstract class Animal {

// Enumeration for animal moves

public enum eMove {

NO_MOVE,

LEFT,

UP_LEFT,

UP,

UP_RIGHT,

RIGHT,

DOWN_RIGHT,

DOWN,

DOWN_LEFT

}

// Instance data for animal classes

public int currentRow;

public int currentCol;

public int previousRow;

public int previousCol;

public int closestRow;

public int closestCol;

public char[][] field;

// Constructor for animal classes

public Animal(int initialRow, int initialCol, char[][] field){

this.currentRow = initialRow;

this.currentCol = initialCol;

this.previousRow = -1;

this.previousCol = -1;

this.closestRow = -1;

this.closestCol = -1;

this.field = field;

}

// Getters for animal classes

public int getCurrentRow(){ return currentRow; }

public int getCurrentCol(){ return currentCol; }

public int getPreviousRow(){ return previousRow; }

public int getPreviousCol(){ return previousCol; }

public int getClosestRow(){ return closestRow; }

public int getClosestCol(){ return closestCol; }

// ABSTRACT METHODS, must be implemented by terrier and squirrel

// Figure out the next move for an animal

public abstract eMove findMove();

// Is the move valid for the animal?

public abstract boolean isValid(int row, int col);

// IMPLEMENTED METHODS, shared behavior between terrier and squirrel

// Find the closest other animal to current position

public void findClosest(){

double minimum = Double.MAX_VALUE;

closestRow = -1;

closestCol = -1;

// Terriers look for closest squirrels, and vice versa

char lookFor = ' ';

if (this instanceof Terrier) lookFor = 'S';

if (this instanceof Squirrel) lookFor = 'D';

// STUDENT CODE HERE

}

// Find, adjust, make the move for an animal

public void moveAnimal() {

makeMove(adjustMove(findMove()));

}

// Adjust move to avoid obstacles

private eMove adjustMove(eMove move) {

if (move == eMove.DOWN_LEFT)

if (!isValid(currentRow+1,currentCol-1))

move = eMove.LEFT;

if (move == eMove.LEFT)

if (!isValid(currentRow,currentCol-1))

move = eMove.UP_LEFT;

if (move == eMove.UP_LEFT)

if (!isValid(currentRow-1,currentCol-1))

move = eMove.UP;

if (move == eMove.UP)

if (!isValid(currentRow-1,currentCol))

move = eMove.UP_RIGHT;

if (move == eMove.UP_RIGHT)

if (!isValid(currentRow-1,currentCol+1))

move = eMove.RIGHT;

if (move == eMove.RIGHT)

if (!isValid(currentRow,currentCol+1))

move = eMove.DOWN_RIGHT;

if (move == eMove.DOWN_RIGHT)

if (!isValid(currentRow+1,currentCol+1))

move = eMove.DOWN;

if (move == eMove.DOWN)

if (!isValid(currentRow+1,currentCol))

move = eMove.DOWN_LEFT;

if (move == eMove.DOWN_LEFT)

if (!isValid(currentRow+1,currentCol-1))

move = eMove.NO_MOVE;

return move;

}

// Actually move the animal

private void makeMove(eMove move) {

// STUDENT CODE HERE

}

// UTILITY METHODS, just code to do stuff

// Compute distance between two animals

private double computeDistance(int row0, int col0, int row1, int col1) {

// STUDENT CODE HERE

return 0.0;

}

// Figure out if move will stay on board

public boolean stayOnBoard(int row, int col){

// Stay on board?

if (row < 0 || row >= field.length)

return false;

if (col < 0 || col >= field[0].length)

return false;

return true;

}

}

// Squirrel.java

public class Squirrel extends Animal {

// Constructor

public Squirrel(int initialRow, int initialCol, char[][] field) {

super(initialRow, initialCol, field);

}

// Make squirrel flee from closest terrier

public eMove findMove() {

// STUDENT CODE HERE

return eMove.RIGHT;

}

// Figure out if move is valid for squirrel

public boolean isValid(int row, int col){

// STUDENT CODE HERE

return true;

}

}

public class Terrier extends Animal {

// Constructor

public Terrier(int initialRow, int initialCol, char[][] field) {

super(initialRow, initialCol, field);

}

// Make terrier chase closest squirrel

public eMove findMove() {

// STUDENT CODE HERE

return eMove.LEFT;

}

// Figure out if move is valid for terrier

public boolean isValid(int row, int col){

// STUDENT CODE HERE

return true;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!