import java.util.ArrayList;

import info.gridworld.actor.Actor;
import info.gridworld.grid.Location;

public class Cell extends Actor
{
   public void plan()
   {      
      ArrayList<Location> neighborLocs = getGrid().getValidAdjacentLocations(getLocation());
      int neighborCount = getGrid().getOccupiedAdjacentLocations(getLocation()).size();
      die = neighborCount < 2 || neighborCount > 3;
      breedingLocations = new ArrayList<Location>();
      for (Location l : neighborLocs)
      {
         if (getGrid().getOccupiedAdjacentLocations(l).size() == 3)
            breedingLocations.add(l);
      }
   }
   
   public void live() 
   {
      for (Location bl : breedingLocations)
      {
         if (getGrid().get(bl) == null)
            new Cell().putSelfInGrid(getGrid(), bl);
      }
      if (die) removeSelfFromGrid();      
   }

   public void act()
   {
      if (planning) 
         plan();
      else
         live();
      
      planning = !planning;
   }

   private ArrayList<Location> breedingLocations;
   private boolean die;
   private boolean planning = true;
}
