Example #1
0
  /** Reset the simulation to a starting position. */
  public void reset() {
    step = 0;
    populate();

    // Show the starting state in the view.
    view.showStatus(step, field);
  }
Example #2
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);
  }
Example #3
0
  /** Reset the simulation to a starting position. */
  public void reset() {
    step = 0;
    rabbits.clear();
    foxes.clear();
    populate();

    // Show the starting state in the view.
    view.showStatus(step, field);
  }
Example #4
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 humans.
    List<Human> newHumans = new ArrayList<Human>();
    // Provide space for newborn vampires.
    List<Vampire> newVampires = new ArrayList<Vampire>();
    // Provide space for newborn wolfs.
    List<Zombie> newZombies = new ArrayList<Zombie>();
    // Let all characters act.
    for (Iterator<Character> it = characterList.iterator(); it.hasNext(); ) {
      Character c = it.next();
      if (c.getCharacter() == TypeCharacter.HUMAN) {
        Human h = (Human) c;
        h.run(newHumans);
      }
      if (c.getCharacter() == TypeCharacter.VAMPIRE) {
        Vampire v = (Vampire) c;
        v.hunt(newVampires);
      }
      if (c.getCharacter() == TypeCharacter.ZOMBIE) {
        Zombie z = (Zombie) c;
        z.hunt(newZombies);
      }

      if (c.getCharacter() == TypeCharacter.MADZOMBIE) {
        MadZombie mz = (MadZombie) c;
        mz.hunt(newZombies);
      }
    }

    // Add the newly born humans, vampires and zombies to the main lists.
    characterList.addAll(newHumans);
    characterList.addAll(newVampires);
    characterList.addAll(newZombies);

    view.showStatus(step, field);
  }