Due: Friday, 22 February 2008
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 program will be compiled and run as follows:
javac -classpathwheredir :. *.java
java -classpathOutput must be to the standard output.dir :. Simulation size cycles seed
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 397983435is an example where the rabbit on (3,4) dies. It appears in generations 0, 1, 2, and 3. But not in generation 4.
java Simulation 5 3 12349893then 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.
java Simulation 10 1 765yields:
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.. ..........
Another example can be found in a separate WWW document.
|
Course=cse4051 Project=proj07 |