Java assignment: Wator
David Matuszek,
dave@acm.org
Fall 1998, Villanova University
"Wator" (a misspelling of "water") is a simple predator-prey
simulation. It was first described by A. K. Dewdney, in his "Computer
Recreations" article in the December, 1984 issue of Scientific
American. In this version, there are sharks and tuna. Sharks move,
eat tuna, and reproduce; they might starve to death. Tuna move and
reproduce; they never starve, but might get eaten. Neither tuna nor
sharks die of old age.
This simulation will read the following input parameters from the
command line (or equivalent):
|
- Size of the ocean.
- Number of steps.
|
|
- Initial number of tuna.
- Tuna gestation period.
|
|
- Initial number of sharks.
- Shark gestation period.
- Shark starvation period.
|
|
The ocean is an NxN array (the size N is an input parameter). It
"wraps" to form a torus: a cell on the right edge is adjacent to cells
on the left edge, and a cell on the bottom edge is adjacent to cells
on the top edge.
A location in the array can be empty, or it can hold one tuna or one
shark (but not both). At the beginning of the simulation, put the
tuna and sharks in random locations in the ocean.
The simulation progresses in a series of "steps." At each step,
each tuna will
- Pick a random direction (one of eight directions,
including diagonals), and try to move in that direction.
Succeed if the move is within the array and the new square is
empty.
- If it is time for the tuna to reproduce, and the tuna was able
to move, create a new tuna in the just vacated square. Both the
old and the new tuna begin a new gestation period.
- If it is time for the tuna to reproduce, but the tuna could
not move, the tuna does not move but remains ready to reproduce at
the earliest opportunity.
At each step, each shark will
- Check whether it is adjacent to a tuna, and if so, move
in that direction (and eat the tuna). Otherwise, it picks a
random direction, and tries to move in that direction.
- Reproduce according to the same rules as a tuna (if it is time
and the shark can move).
- If the shark has not eaten for the time specified, it starves
to death (and disappears).
Run the simulation for the given number of steps, and at each step
print out the number of tuna and the number of sharks. Stop the
simulation early if you run out of tuna or run out of sharks.
|
Turn in:
|
- A listing of your source code.
- A listing of your printed results.
- Documentation for your program, created with javadoc.
|
|
It's the Java language that I'm concerned with, not the
simulation. So if some details of the simulation aren't clear, just
do something reasonable.
Note: The next Java assignment will build on this
one, so it is important to get this one working.