CSC 8310: Magic Forest Assignment
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
Ok, this is a pretty bizarre assignment, but I wanted one that really made use of objects,
inheritance, and all that stuff, without being too difficult. I think this assignment
satisfies those constraints reasonably well.
There is a Forest containing four kinds of magical Creatures
(as well as a lot of empty space). Each kind of Creature is a different
color, because in the next assignment we'll be displaying all this on a computer screen,
but for the first assignment we're just doing text output.
The program progresses in a series of turns, and at each turn, each Creature
has a chance to move. A Creature cannot move outside the forest or enter a
space already occupied by another Creature. If it tries to enter a space
occupied by another Creature, we say it bumps into that Creature.
The Forest is (for now) a class containing a 10x10 array--but make this
flexible, because we'll be changing the size of the Forest later. The Forest
is mostly empty, but it has some of each kind of creature--say, 10 Green
creatures and 5 of each other kind of creature (again, keep it flexible!).
A Red creature is a random wanderer. It starts in a random direction (chosen
randomly when it is constructed), which may be either up, down, right, or left (or, if you
prefer, north, south, east, and west). After that, it randomly (with 75% probability, or 3
chances out of four) tries to move one square in the same direction; the rest of the time
(25%) it chooses a new direction. In either case, if it can't move in the chosen
direction, it stays where it is (but chooses a new direction for use next time). A Red
creature's special ability is the ability to reproduce: If it goes 5 turns without bumping
into another Creature, it reproduces--the very next time it successfully
moves, it leaves behind a new Red creature.
A Yellow creature is a migrator. At each turn, it tries to move
to the right. If it can't move right, it tries to move down. If it can't move down, it
tries to move up. It never tries to move left. If it can't move, it stays where it is and
turns into a Green creature (a "tree"). A Yellow
creature's special ability is to move end-around: if it moves off the right edge
of the Forest, it reappears on the left edge; if it moves off the bottom, it reappears at
the top; and so on. Of course, it can't move into an occupied square, whether it's moving
normally or end-around.
A Green creature is a tree. It doesn't move at all. Its special
ability is to die. A Green creature lives for at least 5 turns, but
after that, each turn it has a 1 in 5 chance of dying (disappearing from the Forest).
A Blue creature changes other Creatures. It moves
exactly like a Red creature, but each time it bumps into another Creature,
it changes that other Creature into a brand-new Creature
of a different type: it changes Red to Yellow, Yellow
to Green, Green to Blue, and Blue to Red.
Your job is to write a Java simulation of the above. I've named six classes you should
use: Forest, Creature, Red, Yellow, Green,
and Blue (feel free to invent additional classes as needed). You need to
figure out what fields and methods each class should have. (Is there anything a Forest
needs to do?)
Since Red and Blue creatures move the same way, I suggest
that you make this the default, that is, make it a method of Creature. Yellow
and Green creatures move differently, so you can override the movement method
as needed. The way that a Yellow creature checks whether a move is possible
is a somewhat expanded version of the way Red and Blue creatures
check, so you can probably make use of that fact. If you find yourself duplicating several
lines of code, with possibly minor changes, you are probably doing something wrong.
For this first version, print out the Forest after each turn. Use dots (.)
to represent empty spaces, and R, Y, G, B
to represent the various kinds of creatures.
If you follow good programming practices, that will greatly simplify the task of writing the next version of this program. Some good programming practices are:
.,
R, Y, G, B) with graphics of some
sort, so don't mix the two any more than necessary.To get a random number between 0.0 and 1.0, use Math.random(). To get a
random integer between 1 and 4, use (int) (Math.random () * 4).
The idea of the program is just to run the simulation for a while and see what happens.
Due date: February 3.
Turn in: A listing of your program, and a sample output (run the
simulation for 15 or 20 steps). Please include your name and section number (TR 4:30 is
section 1, R 6:15 is section 2) on both.
I have written up some notes that should help you with this assignment.