Exemple #1
0
 /** Randomly populate the field with foxes and rabbits. */
 private void populate() {
   Random rand = new Random();
   field.clear();
   for (int row = 0; row < field.getDepth(); row++) {
     for (int col = 0; col < field.getWidth(); col++) {
       if (rand.nextInt(120) <= HUMAN_CREATION_PROBABILITY) {
         if (nbHumans > 0) {
           Location location = new Location(row, col);
           Human h = new Human("Human" + row + col, HP_HUMANS, location, field);
           characterList.add(h);
           nbHumans--;
           switch (rand.nextInt(4)) {
             case 0:
               ShotGun weapon = new ShotGun(5, 2, field, location);
               h.setWeapon(weapon);
               break;
             case 1:
               LiquidNitrogen weapon1 = new LiquidNitrogen(2, field, location);
               h.setWeapon(weapon1);
               break;
             case 2:
               WoodenStake weapon2 = new WoodenStake(field, location);
               h.setWeapon(weapon2);
               break;
             default:
               break;
           }
         }
       } else if (rand.nextInt(120) <= ZOMBIE_CREATION_PROBABILITY) {
         if (nbZombies > 0) {
           Location location = new Location(row, col);
           Zombie z = new Zombie("Zombie" + row + col, HP_ZOMBIES, location, field);
           characterList.add(z);
           nbZombies--;
         }
       } else if (rand.nextInt(120) <= VAMPIRE_CREATION_PROBABILITY) {
         Location location = new Location(row, col);
         Vampire v = new Vampire("Vampire", HP_VAMPIRES, location, field);
         characterList.add(v);
       } else if (rand.nextInt(1000) <= MADZOMBIE_CREATION_PROBABILITY) {
         if (nbMadZombies > 0) {
           Location location = new Location(row, col);
           MadZombie mz = new MadZombie("Mad Zombie" + row + col, HP_ZOMBIES, location, field);
           characterList.add(mz);
           nbMadZombies--;
         }
       }
     }
   }
 }
Exemple #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 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);
  }