Example #1
0
  /**
   * Look for foxes and wolfs adjacent to the current location. Only the first live foxes or wolf is
   * eaten.
   *
   * @return Where food was found, or null if it wasn't.
   */
  private Location findFood() {
    Field field = getField();
    List<Location> adjacent = field.adjacentLocations(getLocation());
    Iterator<Location> it = adjacent.iterator();
    while (it.hasNext()) {
      Location where = it.next();
      Object animal = field.getObjectAt(where);
      if (animal instanceof Wolf) {
        Wolf wolf = (Wolf) animal;
        if (wolf.isAlive()) {
          wolf.setDead();
          setFoodLevel(WOLFS_FOOD_VALUE);
          return where;
        }
      } else if (animal instanceof Fox) {
        Fox fox = (Fox) animal;
        if (fox.isAlive()) {
          fox.setDead();
          setFoodLevel(FOX_FOOD_VALUE);
          return where;
        }

      } else if (animal instanceof Rabbit) {
        Rabbit rabbit = (Rabbit) animal;
        if (rabbit.isAlive()) {
          rabbit.setDead();
          setFoodLevel(RABBIT_FOOD_VALUE);
          return where;
        }
      }
    }
    return null;
  }
Example #2
0
 /**
  * Check whether or not this bear is to give birth at this step. New births will be made into free
  * adjacent locations.
  *
  * @param newbearrs A list to return newly born bears.
  */
 private void giveBirth(List<Actor> newBears) {
   // New bears are born into adjacent locations.
   // Get a list of adjacent free locations.
   Field field = getField();
   List<Location> free = field.getFreeAdjacentLocations(getLocation());
   int births = breed();
   for (int b = 0; b < births && free.size() > 0; b++) {
     Location loc = free.remove(0);
     Bear young = new Bear(false, field, loc);
     newBears.add(young);
   }
 }