import java.util.List;

/**
 * A simple model of a sheep.
 * Sheep age, move, breed, and die.
 */
public class Sheep extends Animal
{
    // Characteristics shared by all rabbits (static fields).

    // The age at which a sheep can start to breed.
    private static final int BREEDING_AGE = 5;
    // The age to which a sheep can live.
    private static final int MAX_AGE = 40;
    // The likelihood of a sheep breeding.
    private static final double BREEDING_PROBABILITY = 0.15;
    // The maximum number of births.
    private static final int MAX_LITTER_SIZE = 4;
    
    // Individual characteristics (instance fields).
    
    // The sheep's age.
    private int age;

    /**
     * Create a new sheep. A sheep may be created with age
     * zero (a new born) or with a random age.
     * 
     * @param randomAge If true, the sheep will have a random age.
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Sheep(boolean randomAge, Field field, Location location)
    {
        super(field, location);
        age = 0;
        if(randomAge) {
            age = Simulator.randomi(MAX_AGE);
        }
    }
    
    /**
     * This is what the sheep does most of the time - it runs 
     * around. Sometimes it will breed or die of old age.
     * @param newSheeps A list to add newly born sheeps to.
     */
    public void act(List<Animal> newSheeps)
    {
        incrementAge();
        if(isAlive()) {
            giveBirth(newSheeps);            
            // Try to move into a free location.
            Location newLocation = getField().freeAdjacentLocation(getLocation());
            if(newLocation != null) {
                setLocation(newLocation);
            }
            else {
                // Overcrowding.
                setDead();
            }
        }
    }

    /**
     * Increase the age.
     * This could result in the sheep's death.
     */
    private void incrementAge()
    {
        age++;
        if(age > MAX_AGE) {
            setDead();
        }
    }
    
    /**
     * Check whether or not this sheep is to give birth at this step.
     * New births will be made into free adjacent locations.
     * @param newSheeps A list to add newly born sheeps to.
     */
    private void giveBirth(List<Animal> newSheeps)
    {
        // New sheeps 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);
            Sheep young = new Sheep(false, field, loc);
            newSheeps.add(young);
        }
    }
        
    /**
     * Generate a number representing the number of births,
     * if it can breed.
     * @return The number of births (may be zero).
     */
    private int breed()
    {
        int births = 0;
        if(canBreed() && Simulator.randomd() <= BREEDING_PROBABILITY) {
            births = Simulator.randomi(MAX_LITTER_SIZE) + 1;
        }
        return births;
    }

    /**
     * A sheep can breed if it has reached the breeding age.
     * @return true if the sheep can breed, false otherwise.
     */
    private boolean canBreed()
    {
        return age >= BREEDING_AGE;
    }
}
