This assignment is worth 50 points.
Wa-Tor is completely covered in water, there is no land. The Tor part of the name comes from the geography of the world, a torus. The world is a grid where the south edge is connected to the north edge, and the east edge is connect to the west edge. In other words, a fish swimming due east will eventually end up in the far west, and a fish swimming due north will end up in the south. (Take a piece of paper, label the edges and connect them as described).
In this world, fish eat the nutrients that exist in the sea, swim at random, and reproduce at regular intervals. To move, a fish examines the sectors to the north, south, east and west, notes which are empty and moves to one of these chosen at random. If no sector near a fish is empty, they remain where they are. They reproduce every fishBreed
turns if they have moved, then a new fish is generated and placed in the square where the parent was before it moved. Fish never die of old age.
Sharks on this world feed only on fish. They must eat a fish every sharkStarve
turns or die. To move, they first note any squares near them containing fish, and select randomly from one of these as the destination of their move. The fish in this square is removed, as it has been eaten by the shark, and the shark's lastMeal
counter is reset to 0. If there are no neighboring fish, the shark moves as a fish and the shark's lastMeal
counter is incremented by 1. If a shark's lastMeal
counter ever reaches sharkStarve
the shark dies, and should be removed. Sharks breed as fish, only they breed every sharkBreed
turns.
Your program should employ an animal hierarchy. You should build a pure virtual base class from which you derive fish and shark classes. These classes should move and reproduce based on the random movements described above.
Once the basic simulation is running, derive a new fish and a new shark class. Give each new class a new and distinct behavior. Fish, for example, may attempt to flee nearby sharks, or perhaps attempt to school. Sharks might attempt to look a number of squares away and swim towards greater fish densities.
Store your data directly in a two dimensional array representing the world.
To start your program you should read from the file wator.init. This file contains:
fishBreed
sharkStarve
sharkBreed
Each turn you should print out the following clearly labeled:
Turn 0 Fish (f): 5 Sharks (S): 3 0 1 2 3 4 5 6 7 8 9 0 . . . . . . . . . . 1 . f . . . . S . . . 2 . . . . . . . . . . 3 . . . . . . . . . . 4 . . . . . f S . S . 5 . . . . . . . . f . 6 . . . . . . . . . . 7 . . . f . . . . . . 8 . . . . . . . . f . 9 . . . . . . . . . . press enter to continue:
Rand and srand will do fine, but if you are interested, you can employ the c++ random class. For details look here.
For the following discussions, I assume the coordinates are (x,y), not (row, column). It really doesn't mater as long as you are consistent.
You need to be careful when moving an animal. I assume for the following discussion that the upper left hand corner of the screen is (0,0) and the world size is 10x10. If a animal is at (0,0) and decides to move up, it will go to position (0,9). If it decides to go left, it will move to position (9,0). If you build a class where the animal can move to any of the 8 neighbors and it decides to go up and left, it will end at position (9,0).
If you process the array from top to bottom, left to right, the natural way to do this, you need to use care that an animal does not move twice. Assume an animal starts at position (0,0). When it is processed it moves to cell (1,0) make sure that this fish does not move again when cell (1,0) is processed.
Please note, I have not written this program in a while. I will try to do this over Thanksgiving break, but there may be holes in the write up. I will use Dewdney's article as the basis for any decisions. Please make sure you read both the assignment and the article carefully and ask for clarifications before you begin implementing.
Also note, this is not impossible, but it is a challenging exercises. Please start early.