CSE 4051: Project #7

Due: Friday, 22 February 2008

The Task

Simulate a world of living things though several generations. In this world are three living things: foxes, rabbits, and grass. They should all subclasses of a given abstract class Living, as is a fourth class Empty representing the absence of any living thing. You are given the class Living:
abstract class Living {
   abstract Living next (Neighbors n);  // next generation
   abstract char toChar ();             // character representation
}
Neighbors is a class of your own design. It represents the living creatures in the local neighborhood.

You must use this abstract class. You may not change it. Do not turn the file Living.java in. It will be deleted and replaced by the file above.

In the design of the classes, try to make it as easy as possible to add other living things to the world.

All the interior squares of the world are initialized in row-major order

for (int row=1; row<size-1; row++) {
    for (int col=1; col<size-1; col++) {
to living creatures according to the probabilities as expressed in the following method:
Living gen () {
    double x = rng.nextDouble();
    if (x < 0.1)      return /* Fox    */
    else if (x < 0.3) return /* Rabbit */
    else if (x < 0.6) return /* Grass  */
    else              return /* Empty  */
}
The method nextDouble() is in the class A seed determines the sequence of random numbers, running the program twice with the same seed should yield identical results. So all programs with the same seed should have the same initial world. The seed is an optional argument to the program, if it is omitted then the program should generate some unspecified initial world (a different one each time the seed is omitted).

The next inhabitant at a spot in the world is determined only by the 8 neighboring squares surrounding the spot. Here are the rules for death and procreation in the world.

The edges of the world remain empty throughout.

The program will be compiled and run as follows:

javac -classpath dir:. *.java
where dir is the directory with a copy of Living.java and
java -classpath dir:. Simulation size cycles seed
Output must be to the standard output.

Aging. Take a rabbit with life expectancy of three world-cycles. This means that the same rabbit may occupy a square first in some cycle, say cycle 0. The same rabbit may occupy (if not eaten or starved) the square in cycle 1, 2 and 3. But the rabbit is absent in cycle 4, i.e., it is replaced by empty. The world

java Simulation 7 4 397983435
is an example where the rabbit on (3,4) dies. It appears in generations 0, 1, 2, and 3. But not in generation 4.

Example output #1

If the program is invoked as follows
java Simulation 5 3 12349893
then the output must be as follows.
Cycle = 0
.....
.X.G.
..RR.
..R..
.....

Cycle = 1
.....
.XG.. ;; at (1,3) .->G because not(#R>2) and not(#G>4) but #G>0
..RR. ;; at (2,3) and (2,4) R ages because not(#G<1) and not(#X>=#R)
...X. ;; at (3,2) R->. because #G<1
..... ;; at (3,3) .->X because #R>2

Cycle = 2
.....
.X.G.
.G...
...X.
.....

Cycle = 3
.....
..G..
..GG.
.GG..
.....

The seed determines the random numbers. The init code above determines the initial configuration (row major order on the interior squares). All other configurations follow from the previous ones, so all programs should get the same output.

You may make the third argument optional. In this case the initial configuration should be a different one each time.

One blank line separates the cycles.

Example output #2

Here is another example. Starting this way:
java Simulation 10 1 765
yields:
Cycle = 0
..........
..R..RGGG.
.G.GGGGXG.
.XR.R.RRG.
..GX.GRGR.
...G.GR...
..R.XX.G..
.GGRRR..R.
.G..GGR.R.
..........

Cycle = 1
..........
.G.GG.RRR.
..G....XR.
.X.G.XRR..
.G.XG.R.R.
.GG.G..XG.
.GRXXXG.G.
.G.R..GXR.
.RGG..RX..
..........

Example output #3

Another example can be found in a separate WWW document.

Helpful Stuff

Turning it in

Turn in the Java source code for the program using the submission server. The project tag for this project is proj07. The name of the file with the main program must be Simulation.java. Remember: one top-level class per file. Be sure your name is in comments at the beginning of your program as required in the standard header for this class. You are encouraged to turn in one of more files. For that you will need the more multifile WWW interface to the Submit Server.

File 1
Control code:
Course=cse4051
Project=proj07

Acknowledgments

Pohl and McDowell, Java By Dissection, 2000, Section 7.8 An Example: Predator-Prey Simulation.


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Sat Feb 16 16:49:33 EST 2008