handout #17

CSE142—Computer Programming I

Programming Assignment #8

due: Wednesday, 3/11/09, 11 pm

This assignment will give you practice with defining classes.  You are to write a set of classes that define the behavior of certain animals.  You will be given a program that runs a simulation of a world with many animals wandering around in it.  Different kinds of animals will behave in different ways and you are defining those differences.  In this world, animals propagate their species by infecting other animals with their DNA, which transforms the other animal into the infecting animalÕs species.  This idea of animals transforming into a different species appeared in many Star Trek episodes, particularly the Next Generation episode called ÒIdentity Crisis.Ó

For this assignment you will be given a lot of supporting code that runs the simulation.  You are just defining the individual ÒcrittersÓ that wander around this world trying to infect each other.  While it is running the simulation will look something like this:

On each round of the simulation, each critter is asked what action it wants to perform.  There are four possible responses each with a constant associated with it.

Constant

Description

Action.HOP

Move forward one square in its current direction

Action.LEFT

Turn left (rotate 90 degrees counter-clockwise)

Action.RIGHT

Turn right (rotate 90 degrees clockwise)

Action.INFECT

Infect the critter in front of you

Each of your critter classes should extend a class known as Critter.  So each of your Critter classes will look like this:

public class SomeCritter extends Critter {

    ...

}

The ÒextendsÓ clause in the header of this class establishes an inheritance relationship.  This is discussed in chapter 9 of the textbook, although you donÕt need a deep understanding of it for this assignment.  The main point to understand is that the Critter class has several methods and constants defined for you.  So by saying that you extend the class, you automatically get access to these methods and constants.  You then give new definitions to certain methods to define the behavior of your critters.

There are three key methods in the Critter class that you will redefine in your own classes.  When you redefine these methods, you must use exactly the same method header as what you see below.  The three methods to redefine for each Critter class are:

public Action getMove(CritterInfo info) {

    ...

}

 

public Color getColor() {

    ...

}

 

public String toString() {

    ...

}

For the getMove method you should return one of the four Action constants described earlier in the writeup.  For the getColor method, you should return whatever color you want the simulator to use when drawing your critter.  And for the toString method, you should return whatever text you want the simulator to use when displaying your critter (normally a single character).

For example, below is a definition for a simple Critter that always tries to infect and that always displays itself as a green letter F:

import java.awt.*;

 

public class Food extends Critter {

    public Action getMove(CritterInfo info) {

        return Action.INFECT;

    }

 

    public Color getColor() {

        return Color.GREEN;

    }

 

    public String toString() {

        return "F";

    }

}

Notice that it begins with an import declaration to be able to access the Color class.  All of your Critter classes will have the basic form shown above.

The getMove method is passed an object of type CritterInfo.  This is an object that provides you information about the current status of the critter.  It includes four methods for asking about surrounding neighbors plus a method to find out the current direction the critter is facing plus a method to find out how many other critters this critter has successfully infected.  Below are the methods of the CritterInfo class:

Method

Description

public Neighbor getFront();

returns neighbor in front of you

public Neighbor getBack();

returns neighbor in back of you

public Neighbor getLeft();

returns neighbor to your left

public Neighbor getRight();

returns neighbor to your right

public Direction getDirection();

returns direction you are facing

public int getInfectCount();

returns # of critters you have infected

The return type for the first four methods is Neighbor.  There are four different constants for the different kind of neighbors you might encounter:

Constant

Description

Neighbor.WALL

The neighbor in that direction is a wall

Neighbor.EMPTY

The neighbor in that direction an empty square

Neighbor.SAME

The neighbor in that direction is a critter of your species

Neighbor.OTHER

The neighbor in that direction is a critter of another species

Notice that you are only told whether critters are of your species or some other species.  You canÕt find out exactly what species they are.

The getDirection method of the CritterInfo class tells you what direction you are facing.  There are four direction constants:

Constant

Description

Direction.NORTH

facing north

Direction.SOUTH

facing south

Direction.EAST

facing east

Direction.WEST

facing west

This program will probably be confusing at first because this is the first time where you are not writing the main method.  Your code will not be in control.  Instead, you are defining a series of objects that become part of a larger system.  For example, you might find that you want to have one of your critters make several moves all at once.  You wonÕt be able to do that.  The only way a critter can move is to wait for the simulator to ask it for a move.  The simulator is in control, not your critters.  Although this experience can be frustrating, it is a good introduction to the kind of programming we do with objects.  A typical Java program involves many different interacting objects that are each a small part of a much larger system.

Critters move around in a world of finite size that is enclosed on all four sides by walls.  You can include a constructor for your classes if you want, although it should generally be a zero-argument constructor (one that takes no arguments).  The one exception is that you are to define a class called Bear that takes a boolean parameter specifying whether it is a black bear or a polar bear, which will change how its color is displayed.

You are to implement four classes. 

Bear

Constructor

public Bear(boolean polar)

Color

Color.WHITE for a polar bear (when polar is true), Color.BLACK otherwise (when polar is false)

toString

"B"

getMove

Always hop forward if possible; otherwise infect if an enemy is in front; otherwise randomly choose between turning left or right.

Lion

Constructor

public Lion()

Color

Color.RED

toString

"L"

getMove

Always hop forward if possible; otherwise infect if an enemy is in front; and for those moves in which it is not possible to hop or infect, first make two left turns, then make two right turns, then make two left turns, etc.  Notice that the two left turns and the two right turns might not happen as two actions in a row, because of the other constraints.

Tiger

Constructor

public Tiger()

Color

Color.YELLOW

toString

"T"

getMove

Always infect if an enemy is in front; otherwise always turn right if a wall is in front; and for those moves in which it is not possible to infect and there is no wall, first hop forward 5 times, then turn left once, then hop forward 5 times, then turn left once, and so on.  As with the Lion, the series of hop and left turns might not happen in a row because of the other constraints.

Husky

Constructor

public Husky()

Color

You decide

toString

You decide

getMove

You decide

As noted above, you will determine the behavior of your Husky class.  On the last day of class, we will host a contest where students will be allowed to compete to see which has the best survival rate.

The Lion and Tiger classes deserve a little extra explanation.  The turning behavior of the Lion indicates that it tries to perform a series of moves that would be of the form (LEFT, LEFT, RIGHT, RIGHT, LEFT, LEFT, RIGHT, RIGHT, É).  But the Lion has extra constraints about hopping and infecting when possible.  So this pattern of left and right turns will be interrupted by various hop and infect moves.  As a result, the sequence of moves might end up looking more like (HOP, HOP, LEFT, HOP, LEFT, RIGHT, RIGHT, INFECT, LEFT, INFECT, LEFT, HOP, RIGHT, RIGHT, É).  Similarly, the Tiger tries to perform a pattern of (HOP, HOP, HOP, HOP, HOP, LEFT, HOP, HOP, HOP, HOP, HOP, LEFT, HOP, É).  But these hop and left-turning moves can be interspersed with infect and right-turning moves if it encounters critters to infect or walls.

Some of the style points for this assignment will be awarded on the basis of how much energy and creativity you put into defining an interesting Husky class.  These points allow us to reward the students who spend time writing an interesting critter definition.  Your Husky's behavior should not be trivial or closely match that of an existing animal shown in class.

Style points will also be awarded for expressing each critter's behavior elegantly.  Encapsulate the data inside your objects.  Follow past style guidelines about indentation, spacing, identifiers, and localizing variables.  Place comments at the beginning of each class documenting that critter's behavior, and on any complex code

For the random moves, each possible choice must be equally likely.  You may use either a Random object or the Math.random() method to obtain pseudorandom values.

Your classes should be stored in files called Bear.java, Lion.java, Tiger.java, and Husky.java.  The files that you need for this assignment will be included in a zip file called ass8.zip available from the class web page.  All of these files must be included in the same folder as your Critter files.  You should download and unzip ass8.zip, then add your four classes to the folder, compile CritterMain and run CritterMain.

The simulator has several supporting classes that are included in ass8.zip (CritterModel, CritterFrame, etc).  You can in general ignore these classes.  When you compile CritterMain, these other classes will be compiled.  The only classes you will have to modify and recompile are CritterMain (if you change what critters to include in the simulation) and your own individual Critter classes.

You will be given two sample critter classes.  The first is the Food class that appears earlier in the writeup.  The second is a particularly powerful strategy called FlyTrap that was discussed in lecture.  Both of these class definitions appear in ass8.zip and should serve as examples of how to write your own critter classes.

You will notice that CritterMain has lines of code like the following:

// frame.add(30, Lion.class);

You should uncomment these lines of code as you complete the various classes you have been asked to write.  Then critters of that type will be included in the simulation.

For those who want to produce critters that ÒwinÓ in the sense of taking over the world, you will want to understand some subtleties about the order in which actions are performed.  In general, the simulator allows each critter to make a move and it scrambles the order in which it does that.  This becomes important, for example, when two critters are facing each other and each wants to infect the other.  The simulator will randomly decide which critter goes first and the one that goes first wins.  But there is one exception to this rule.  The act of infecting another critter weakens the critter slightly.  As a result, the simulator first allows all critters with an infect count of 0 (critters that have never infected another critter) to move.  Then it allows critters with an infect count of 1 to move.  Then it allows critters with an infect count of 2 to move.  This continues until it allows critters with an infect count of 9 to move.  After that, all other critters are asked to move.  Within each group, the simulator selects a random order.  Taking advantage of this ordering can allow you to write more competitive critters.

When a critter is infected, it is replaced by a brand new critter of the other species, but that new critter retains the properties of the old critter.  So it will be at the same location, facing in the same direction, and with the same infect count as the critter it is replacing.

The simulator provides great visual feedback about where critters are, so you can watch them move around the world.  But it doesnÕt give great feedback about what direction critters are facing.  The simulator has a ÒdebugÓ button that makes this easier to see.  When you request debug mode, your critters will be displayed as arrow characters that indicate the direction they are facing.