Java Reference
In-Depth Information
The main classes we will focus on in our discussion are Simulator , Fox , and Rabbit . The
Fox and Rabbit classes provide simple models of the behavior of a predator and prey, respec-
tively. In this particular implementation, we have not tried to provide an accurate biological
model of real foxes and rabbits; rather, we are simply trying to illustrate the principles of typical
predator-prey simulations. Our main concerns will be on the aspects that most affect population
size: birth, death, and food supply.
The Simulator class is responsible for creating the initial state of the simulation, and then
controlling and executing it. The basic idea is simple: the simulator holds collections of foxes
and rabbits, and it repeatedly gives those animals an opportunity to live through one step 2 of
their life cycle. At each step, each fox and each rabbit is allowed to carry out the actions that
characterize their behaviors. After each step (when all the animals have had the chance to act),
the new current state of the field is displayed on screen.
We can summarize the purpose of the remaining classes as follows:
Field represents a two-dimensional enclosed field. The field is composed of a fixed num-
ber of locations, which are arranged in rows and columns. At most, one animal may occupy
a single location within the field. Each field location can hold an animal or it can be empty.
Location represents a two-dimensional position within the field, specified by a row and a
column value.
These five classes together ( Simulator , Fox , Rabbit , Field , and Location ) provide the
model for the simulation. They completely determine the simulation behavior.
The Randomizer class provides us with a degree of control over random aspects of the
simulation, such as when new animals are born.
The classes SimulatorView , FieldStats , and Counter provide a graphical display of
the simulation. The display shows an image of the field and counters for each species (the
current number of rabbits and foxes).
SimulatorView provides a visualization of the state of the field. An example can be seen in
Figure 10.2.
FieldStats provides to the visualization counts of the numbers of foxes and rabbits in the field.
A Counter stores a current count for one type of animal to assist with the counting.
Try the following exercises to gain an understanding of how the simulation operates before
reading about its implementation.
Exercise 10.1 Create a Simulator object, using the constructor without parameters,
and you should see an initial state of the simulation similar to that in Figure 10.2. The more
numerous rectangles represent the rabbits. Does the number of foxes change if you call the
simulateOneStep method just once?
2
We won't define how much time a “step” actually represents. In practice, this has to be decided by a
combination of such things as what we are trying to discover, what events we are simulating, and how
much real time is available to run the simulation.
 
Search WWH ::




Custom Search