CSC 8310: Magic Forest Assignment--Notes and Suggestions
Section 001: Tuesday & Thursday 4:30 to 5:45pm in Mendel G30
Section 002: Thursday 6:15 to 8:45 in Mendel G30
Dr. David Matuszek, Mendel 162C, (610) 519-5654
Villanova University, Spring 2000

Since this assignment turned out to be more difficult than I had hoped, here are some notes and suggestions that might help you. The suggestions are just suggestions, not additional requirements; if you write your program some other way, that's fine, too.


Notes:

You can use your favorite text editor or word processor to write programs, in Java or in any other language. Just be sure to save the file as text, not in some word-processor format. Java, of course, requires the .java extension.

If you run your program from MS-DOS, you get a tiny little window without a scrollbar, which is just about useless. (UNIX and Mac user's won't have this problem.) As an alternative, you can redirect your output to a file, say, "myfile," as follows:

java MagicForest > myfile

then you can look at myfile with your favorite text editor or word processor.

Just about every class you write should override public String toString (), because sooner or later you will want to print out objects in that class (probably for debugging reasons).

When you try to debug your program, it will help immensely if you print out the direction each creature is trying to travel in. Here's what my printouts look like:

Magic forest after 2 turns:
 .  Y> .  .  G> .  .  .  .  . 
 .  G> .  .  .  .  .  .  .  R>
 G< .  .  .  Y> G< .  .  .  Y>
 .  .  .  Y> R< .  .  .  .  . 
 .  .  .  G^ .  .  Gv .  .  . 
 .  .  .  .  .  R< .  Gv .  . 
 B^ .  Y> .  B^ .  .  .  .  . 
 B^ .  .  Y> .  .  .  .  .  . 
 R^ .  .  .  G< .  .  G> G^ . 
 .  .  .  R< .  .  B^ .  .  . 

Movement note. The order that creatures move in will affect what happens. For example, in the leftmost column above, will the Red creature bump into a Blue creature? In the assignment, I didn't specify in what order the creatures moved (in my version I just looped through the array).

As a general rule, if I don't specify something in the assignment, it's up to you; so I don't care in what order your creatures move. However, you need to be aware of this ordering as you try to debug your program.


How I did it

This describes the set of classes and methods that I used. You are welcome to write your program exactly the same way, somewhat the same, or totally differently. A lot of the work in writing a program like this is in figuring out what classes to have and what methods to put in each (I changed my mind several times while writing the program), so you might save yourself a lot of work if you use this as an outline. But don't feel you have to follow this exactly; do whatever makes sense to you.

MagicForest is the main class. It contains:

static Creature[][] forest is the main array. MagicForest also contains a number of static final variables, that is, named constants (for example, the number of rows and columns in the array).

main() creates and populates the forest, and runs it for some number of turns.

populate() creates the initial set of creatures and places them randomly.

takeTurn() runs through the array, giving each creature a chance to do something, then prints the array.

print() prints forest[][].

I found it useful to write Location and Direction as classes.

Location represents a location in the forest; it contains a row and a column, along with static references to the forest itself and integers representing the number of rows and columns in the forest. Methods are:

Location (row, column) is a constructor.

boolean valid () tells whether the location is within the array (has legal row and column numbers).

void setContents (creature) puts a creature in this location.

Creature getContents () returns the creature in this location (or null).

boolean occupied () tells whether this location contains a creature.

Location next (direction) returns the next location in that direction (may be invalid, that is, outside the array bounds).

void wrap() changes an invalid location into a valid one.

static Location random () returns a random unoccupied location.

public String toString () returns a string of the form (row, column).

Direction represents one of the four compass directions. It contains variables rowDelta (how much to add to the row) and columnDelta (how much to add to the column). I used these to define the static directions: up, down, left, and right. It includes the methods:

Direction (rowDelta, columnDelta) is a constructor.

static Direction random () to choose a random direction.

Direction other () to choose a random direction different from this one.

public String toString () returns one of "^", ">", "v", "<".

Creature keeps track of its own age, direction, location, and a few other things. It includes the following methods:

Creature (location) is a constructor.

void takeTurn () controls movement and keeps track of "age."

Direction chooseDirection () decides which way to try to move.

boolean canMove (direction) tests if the desired move is possible.

void move (direction) actually makes the move.

Red, Yellow, Green, Blue each extend Creature. Each has a constructor (with a Location argument), and each defines toString. In addition,