📄 simulator.java
字号:
import java.util.Random;import java.util.List;import java.util.ArrayList;import java.util.Iterator;import java.util.Collections;import java.awt.Color;/** * A simple predator-prey simulator, based on a field containing * rabbits and foxes. * * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */public class Simulator{ // Constants representing configuration information for the simulation. // The default width for the grid. private static final int DEFAULT_WIDTH = 50; // The default depth of the grid. private static final int DEFAULT_DEPTH = 50; // The probability that a fox will be created in any given grid position. private static final double FOX_CREATION_PROBABILITY = 0.02; // The probability that a rabbit will be created in any given grid position. private static final double RABBIT_CREATION_PROBABILITY = 0.08; // The list of animals in the field private List<Animal> animals; // The list of animals just born private List<Animal> newAnimals; // The current state of the field. private Field field; // A second field, used to build the next stage of the simulation. private Field updatedField; // The current step of the simulation. private int step; // A graphical view of the simulation. private SimulatorView view; /** * Construct a simulation field with default size. */ public Simulator() { this(DEFAULT_DEPTH, DEFAULT_WIDTH); } /** * 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; } animals = new ArrayList<Animal>(); newAnimals = new ArrayList<Animal>(); field = new Field(depth, width); updatedField = 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); // Setup a valid starting point. reset(); } /** * Run the simulation from its current state for a reasonably long period, * e.g. 500 steps. */ public void runLongSimulation() { simulate(500); } /** * Run the simulation from its current state for the given number of steps. * Stop before the given number of steps if it ceases to be viable. * @param numSteps How many steps to run for. */ public void simulate(int numSteps) { for(int step = 1; step <= numSteps && view.isViable(field); step++) { simulateOneStep(); } } /** * Run the simulation from its current state for a single step. * Iterate over the whole field updating the state of each * fox and rabbit. */ public void simulateOneStep() { step++; newAnimals.clear(); // let all animals act for(Iterator<Animal> it = animals.iterator(); it.hasNext(); ) { Animal animal = it.next(); animal.act(field, updatedField, newAnimals); // Remove dead animals from the simulation. if(! animal.isAlive()) { it.remove(); } } // add new born animals to the list of animals animals.addAll(newAnimals); // Swap the field and updatedField at the end of the step. Field temp = field; field = updatedField; updatedField = temp; updatedField.clear(); // Display the new field on screen. view.showStatus(step, field); } /** * Reset the simulation to a starting position. */ public void reset() { step = 0; animals.clear(); newAnimals.clear(); field.clear(); updatedField.clear(); populate(field); // Show the starting state in the view. view.showStatus(step, field); } /** * Populate a field with foxes and rabbits. * @param field The field to be populated. */ private void populate(Field field) { Random rand = new Random(); field.clear(); for(int row = 0; row < field.getDepth(); row++) { for(int col = 0; col < field.getWidth(); col++) { if(rand.nextDouble() <= FOX_CREATION_PROBABILITY) { Fox fox = new Fox(true); fox.setLocation(row, col); animals.add(fox); field.place(fox); } else if(rand.nextDouble() <= RABBIT_CREATION_PROBABILITY) { Rabbit rabbit = new Rabbit(true); rabbit.setLocation(row, col); animals.add(rabbit); field.place(rabbit); } // else leave the location empty. } } Collections.shuffle(animals); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -