Exemplo n.º 1
0
Arquivo: Fox.java Projeto: mcguile/sd2
 /**
  * Check whether or not this fox is to give birth at this step. New births will be made into free
  * adjacent locations.
  *
  * @param newFoxes A list to return newly born foxes.
  */
 private void giveBirth(List<Animal> newFoxes) {
   // New foxes 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);
     Fox young = new Fox(false, field, loc);
     newFoxes.add(young);
   }
 }
Exemplo n.º 2
0
Arquivo: Fox.java Projeto: mcguile/sd2
 /**
  * Look for rabbits adjacent to the current location. Only the first live rabbit 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 Rabbit) {
       Rabbit rabbit = (Rabbit) animal;
       if (rabbit.isAlive()) {
         rabbit.setDead();
         foodLevel = RABBIT_FOOD_VALUE;
         return where;
       }
     }
   }
   return null;
 }