The goal of this lab is to practive using the strategy pattern.
Today we go back to the beloved predator/prey simulation. This time we will be adjusting the algorithm the animals use to find prey and elude predators.
The current version of the simulation can be copied from a subdirectory of the course public directory.
cp /homeemp/tvandrun/pub/245/simulation3/* .
The algorithm that all animals use is to scan the the visible area around it, starting with the upper left hand corner, and given the first thing seen that is either a predator or a prey,
We have seen the effects of this. Rabbits congregate on the upper left can corner of a patch of clover-- either moving right into a predator's mouth even though a there's a predator-free clover-filled area nearby, or forming a swarm which moves from left to right. Animals lower on the food chain get away from predators above and to the left of them, but are practically blind to those below or to the right. Predators chase rabbits to the left of the board, or compete with each other for the same rabbit, when a swarm is only a few squares to the right. These animals could help their survival and the survival of their species if they only had smarter instincts.
In the current version of the simulation, the behavior of the scan method is mostly delegated to a strategy. Look at the code to familiarize yourself with how it works.
Your task is to encode new behavior for the animals by writing new
ScanStrategy
classes.
Some things to think about:
weight()
is a public method, a predator can
pick its prey based on which is the fattest.
StupidStrategy
is a singleton, but Strateg
ies
don't have to be.
To remember who you're chasing, the strategy might need state.
Experiment. Start out by observing the current behavior. There basically are two possible outcomes: The clover wins, because all the rabbits are eaten before the clover is eaten, and then the clover expands with nothing to stop it; or, the rabbits win, by forming an unstoppable swarm that sweeps the clover off the map. I have found that if we hold constant the number of clover at 100, rabbits at 20, cougars at 1, and bears at 1 and vary the number of foxes, the boundary is somewhere around 12 foxes. At 10 foxes, the rabbits almost always win. At 15 foxes, the clover almost always wins.