Example #1
0
 /** @return the number of human characters currently in the game */
 public int nbZombiesAlive() {
   // Need to iterate through the list of characters
   // and count the number of humans
   int nbZombies = 0;
   for (Character character : characterList) {
     if (character.getCharacter() == TypeCharacter.ZOMBIE) {
       ++nbZombies;
     }
   }
   return nbZombies;
 }
Example #2
0
  /** Perform all game logic for next turn. */
  public void nextTurn() {
    // All characters encounter the next character in the list (question 5)
    /*for (int i = 0; i < characterList.size(); ++i) {
    	Character c = characterList.get(i);
    	Character encountered = characterList.get((i+1)%(characterList.size()));
    	c.encounterCharacter(encountered);
    }*/
    // Dead characters are removed from the character list
    // ... add your code here (question 6) ...
    // Need an iterator for removing objects while looping over a collection
    for (Iterator<Character> it = characterList.iterator(); it.hasNext(); ) {
      Character c = it.next();
      if (c.getHealthPoints() <= 0) {
        if (c.getCharacter() == TypeCharacter.HUMAN && !((Human) c).getHasBeenKillByZombie()) {
          it.remove();
          c.setDead();
        }
      }
    }
    // Each vampire (if he is thirsty) bites the first Human in the list who has not been bitten yet
    // ... add your code here (question 7a) ...
    /*for (Character c1 : characterList) {
        if ((c1 instanceof Vampire) && ((Vampire) c1).getIsThirsty()) {
            Vampire v = (Vampire) c1;

            // Find the first human in the list, and bite him
            boolean hasBitten = false;
            for (Character c2 : characterList) {
                if (!hasBitten && (c2 instanceof Human) && !((Human) c2).getHasBeenBitten()) {
                    v.bite((Human) c2);
                    hasBitten = true;
                }
            }
        }
    }*/
    // Humans that have been bitten become vampires

    // ... add your code here (question 7b) ...

    for (int i = 0; i < characterList.size(); ++i) {
      Character c = characterList.get(i);
      if (c.getCharacter() == TypeCharacter.HUMAN && ((Human) c).getHasBeenKillByZombie()) {
        Zombie newZombie = ((Human) c).turnIntoZombie();
        characterList.set(i, newZombie);
        c = null;
      } else if (c.getCharacter() == TypeCharacter.HUMAN && ((Human) c).getHasBeenBitten()) {
        Vampire newVampire = ((Human) c).turnIntoVampire();
        characterList.set(i, newVampire);
        c = null;
      }
    }

    // Perform end-of-turn actions for all characters (question 4)
    for (int i = 0; i < characterList.size(); ++i) {
      Character c = characterList.get(i);
      c.endOfTurn();
    }
  }
Example #3
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);
  }