igede

Untitled

Nov 22nd, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1.  import java.util.List;  
  2.  import java.util.Random;  
  3.  /**  
  4.   * A simple model of a rabbit.  
  5.   * Rabbits age, move, breed, and die.  
  6.   *  
  7.   * @author Gede
  8.   * @version (22/11/2018)
  9.   */  
  10.  public class Rabbit  
  11.  {  
  12.    // Characteristics shared by all rabbits (static fields).  
  13.    // The age at which a rabbit can start to breed.  
  14.    private static final int BREEDING_AGE = 5;  
  15.    // The age to which a rabbit can live.  
  16.    private static final int MAX_AGE = 40;  
  17.    // The likelihood of a rabbit breeding.  
  18.    private static final double BREEDING_PROBABILITY = 0.15;  
  19.    // The maximum number of births.  
  20.    private static final int MAX_LITTER_SIZE = 4;  
  21.    // A shared random number generator to control breeding.  
  22.    private static final Random rand = Randomizer.getRandom();  
  23.    // Individual characteristics (instance fields).  
  24.    // The rabbit's age.  
  25.    private int age;  
  26.    // Whether the rabbit is alive or not.  
  27.    private boolean alive;  
  28.    // The rabbit's position.  
  29.    private Location location;  
  30.    // The field occupied.  
  31.    private Field field;  
  32.    /**  
  33.     * Create a new rabbit. A rabbit may be created with age  
  34.     * zero (a new born) or with a random age.  
  35.     *  
  36.     * @param randomAge If true, the rabbit will have a random age.  
  37.     * @param field The field currently occupied.  
  38.     * @param location The location within the field.  
  39.     */  
  40.    public Rabbit(boolean randomAge, Field field, Location location)  
  41.    {  
  42.      age = 0;  
  43.      alive = true;  
  44.      this.field = field;  
  45.      setLocation(location);  
  46.      if(randomAge) {  
  47.        age = rand.nextInt(MAX_AGE);  
  48.      }  
  49.    }  
  50.    /**  
  51.     * This is what the rabbit does most of the time - it runs  
  52.     * around. Sometimes it will breed or die of old age.  
  53.     * @param newRabbits A list to add newly born rabbits to.  
  54.     */  
  55.    public void run(List<Rabbit> newRabbits)  
  56.    {  
  57.      incrementAge();  
  58.      if(alive) {  
  59.        giveBirth(newRabbits);        
  60.        // Try to move into a free location.  
  61.        Location newLocation = field.freeAdjacentLocation(location);  
  62.        if(newLocation != null) {  
  63.          setLocation(newLocation);  
  64.        }  
  65.        else {  
  66.          // Overcrowding.  
  67.          setDead();  
  68.        }  
  69.      }  
  70.    }  
  71.    /**  
  72.     * Check whether the rabbit is alive or not.  
  73.     * @return true if the rabbit is still alive.  
  74.     */  
  75.    public boolean isAlive()  
  76.    {  
  77.      return alive;  
  78.    }  
  79.    /**  
  80.     * Indicate that the rabbit is no longer alive.  
  81.     * It is removed from the field.  
  82.     */  
  83.    public void setDead()  
  84.    {  
  85.      alive = false;  
  86.      if(location != null) {  
  87.        field.clear(location);  
  88.        location = null;  
  89.        field = null;  
  90.      }  
  91.    }  
  92.    /**  
  93.     * Return the rabbit's location.  
  94.     * @return The rabbit's location.  
  95.     */  
  96.    public Location getLocation()  
  97.    {  
  98.      return location;  
  99.    }  
  100.    /**  
  101.     * Place the rabbit at the new location in the given field.  
  102.     * @param newLocation The rabbit's new location.  
  103.     */  
  104.    private void setLocation(Location newLocation)  
  105.    {  
  106.      if(location != null) {  
  107.        field.clear(location);  
  108.      }  
  109.      location = newLocation;  
  110.      field.place(this, newLocation);  
  111.    }  
  112.    /**  
  113.     * Increase the age.  
  114.     * This could result in the rabbit's death.  
  115.     */  
  116.    private void incrementAge()  
  117.    {  
  118.      age++;  
  119.      if(age > MAX_AGE) {  
  120.        setDead();  
  121.      }  
  122.    }  
  123.    /**  
  124.     * Check whether or not this rabbit is to give birth at this step.  
  125.     * New births will be made into free adjacent locations.  
  126.     * @param newRabbits A list to add newly born rabbits to.  
  127.     */  
  128.    private void giveBirth(List<Rabbit> newRabbits)  
  129.    {  
  130.      // New rabbits are born into adjacent locations.  
  131.      // Get a list of adjacent free locations.  
  132.      List<Location> free = field.getFreeAdjacentLocations(location);  
  133.      int births = breed();  
  134.      for(int b = 0; b < births && free.size() > 0; b++) {  
  135.        Location loc = free.remove(0);  
  136.        Rabbit young = new Rabbit(false, field, loc);  
  137.        newRabbits.add(young);  
  138.      }  
  139.    }  
  140.    /**  
  141.     * Generate a number representing the number of births,  
  142.     * if it can breed.  
  143.     * @return The number of births (may be zero).  
  144.     */  
  145.    private int breed()  
  146.    {  
  147.      int births = 0;  
  148.      if(canBreed() && rand.nextDouble() <= BREEDING_PROBABILITY) {  
  149.        births = rand.nextInt(MAX_LITTER_SIZE) + 1;  
  150.      }  
  151.      return births;  
  152.    }  
  153.    /**  
  154.     * A rabbit can breed if it has reached the breeding age.  
  155.     * @return true if the rabbit can breed, false otherwise.  
  156.     */  
  157.    private boolean canBreed()  
  158.    {  
  159.      return age >= BREEDING_AGE;  
  160.    }  
  161.  }
Advertisement
Add Comment
Please, Sign In to add comment