Java Reference
In-Depth Information
Code 10.3
Part of the
Simulator
class
//
import statements and class comment omitted
public
class
Simulator
{
//
static variables omitted
// Lists of animals in the field.
private
List<Rabbit> rabbits;
private
List<Fox> foxes;
// The current state of the field.
private
Field field;
// The current step of the simulation.
private
int
step;
// A graphical view of the simulation.
private
SimulatorView view;
/**
* Create a simulation field with the given size.
* @param depth Depth of the field.
* Must be greater than zero.
* @param width Width of the field.
* Must be greater than zero.
*/
public
Simulator(
int
depth,
int
width)
{
if
(width <= 0 || depth <= 0) {
System.out.println(
"The dimensions must be greater than zero."
);
System.out.println(
"Using default values."
);
depth = DEFAULT_DEPTH;
width = DEFAULT_WIDTH;
}
rabbits =
new
ArrayList<Rabbit>();
foxes =
new
ArrayList<Fox>();
field =
new
Field(depth, width);
// Create a view of the state of each location in the
// field.
view =
new
SimulatorView(depth, width);
view.setColor(Rabbit.
class
, Color.ORANGE);
view.setColor(Fox.
class
, Color.BLUE);
// Set up a valid starting point.
reset();
}
/**
* Run the simulation from its current state for the
Search WWH ::
Custom Search