示例#1
0
  /**
   * 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++;

    // Provide space for newborn rabbits.
    List<Rabbit> newRabbits = new ArrayList<Rabbit>();
    // Let all rabbits act.
    for (Iterator<Rabbit> it = rabbits.iterator(); it.hasNext(); ) {
      Rabbit rabbit = it.next();
      rabbit.run(newRabbits);
      if (!rabbit.isAlive()) {
        it.remove();
      }
    }

    // Provide space for newborn foxes.
    List<Fox> newFoxes = new ArrayList<Fox>();
    // Let all foxes act.
    for (Iterator<Fox> it = foxes.iterator(); it.hasNext(); ) {
      Fox fox = it.next();
      fox.hunt(newFoxes);
      if (!fox.isAlive()) {
        it.remove();
      }
    }

    // Add the newly born foxes and rabbits to the main lists.
    rabbits.addAll(newRabbits);
    foxes.addAll(newFoxes);

    view.showStatus(step, field);
  }