Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.Iterator;
- import java.util.Random;
- /**
- * A simple model of a fox.
- * Foxes age, move, eat rabbits, and die.
- *
- * @author Gede
- * @version (22/11/2018)
- */
- public class Fox
- {
- // Characteristics shared by all foxes (static fields).
- // The age at which a fox can start to breed.
- private static final int BREEDING_AGE = 10;
- // The age to which a fox can live.
- private static final int MAX_AGE = 150;
- // The likelihood of a fox breeding.
- private static final double BREEDING_PROBABILITY = 0.35;
- // The maximum number of births.
- private static final int MAX_LITTER_SIZE = 5;
- // The food value of a single rabbit. In effect, this is the
- // number of steps a fox can go before it has to eat again.
- private static final int RABBIT_FOOD_VALUE = 7;
- // A shared random number generator to control breeding.
- private static final Random rand = Randomizer.getRandom();
- // Individual characteristics (instance fields).
- // The fox's age.
- private int age;
- // Whether the fox is alive or not.
- private boolean alive;
- // The fox's position.
- private Location location;
- // The field occupied.
- private Field field;
- // The fox's food level, which is increased by eating rabbits.
- private int foodLevel;
- /**
- * Create a fox. A fox can be created as a new born (age zero
- * and not hungry) or with a random age and food level.
- *
- * @param randomAge If true, the fox will have random age and hunger level.
- * @param field The field currently occupied.
- * @param location The location within the field.
- */
- public Fox(boolean randomAge, Field field, Location location)
- {
- age = 0;
- alive = true;
- this.field = field;
- setLocation(location);
- if(randomAge) {
- age = rand.nextInt(MAX_AGE);
- foodLevel = rand.nextInt(RABBIT_FOOD_VALUE);
- }
- else {
- // leave age at 0
- foodLevel = RABBIT_FOOD_VALUE;
- }
- }
- /**
- * This is what the fox does most of the time: it hunts for
- * rabbits. In the process, it might breed, die of hunger,
- * or die of old age.
- * @param field The field currently occupied.
- * @param newFoxes A list to add newly born foxes to.
- */
- public void hunt(List<Fox> newFoxes)
- {
- incrementAge();
- incrementHunger();
- if(alive) {
- giveBirth(newFoxes);
- // Move towards a source of food if found.
- Location newLocation = findFood(location);
- if(newLocation == null) {
- // No food found - try to move to a free location.
- newLocation = field.freeAdjacentLocation(location);
- }
- // See if it was possible to move.
- if(newLocation != null) {
- setLocation(newLocation);
- }
- else {
- // Overcrowding.
- setDead();
- }
- }
- }
- /**
- * Check whether the fox is alive or not.
- * @return True if the fox is still alive.
- */
- public boolean isAlive()
- {
- return alive;
- }
- /**
- * Return the fox's location.
- * @return The fox's location.
- */
- public Location getLocation()
- {
- return location;
- }
- /**
- * Place the fox at the new location in the given field.
- * @param newLocation The fox's new location.
- */
- private void setLocation(Location newLocation)
- {
- if(location != null) {
- field.clear(location);
- }
- location = newLocation;
- field.place(this, newLocation);
- }
- /**
- * Increase the age. This could result in the fox's death.
- */
- private void incrementAge()
- {
- age++;
- if(age > MAX_AGE) {
- setDead();
- }
- }
- /**
- * Make this fox more hungry. This could result in the fox's death.
- */
- private void incrementHunger()
- {
- foodLevel--;
- if(foodLevel <= 0) {
- setDead();
- }
- }
- /**
- * Tell the fox to look for rabbits adjacent to its current location.
- * Only the first live rabbit is eaten.
- * @param location Where in the field it is located.
- * @return Where food was found, or null if it wasn't.
- */
- private Location findFood(Location location)
- {
- List<Location> adjacent = field.adjacentLocations(location);
- 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;
- // Remove the dead rabbit from the field.
- return where;
- }
- }
- }
- return null;
- }
- /**
- * 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 add newly born foxes to.
- */
- private void giveBirth(List<Fox> newFoxes)
- {
- // New foxes are born into adjacent locations.
- // Get a list of adjacent free locations.
- List<Location> free = field.getFreeAdjacentLocations(location);
- 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);
- }
- }
- /**
- * 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() && rand.nextDouble() <= BREEDING_PROBABILITY) {
- births = rand.nextInt(MAX_LITTER_SIZE) + 1;
- }
- return births;
- }
- /**
- * A fox can breed if it has reached the breeding age.
- */
- private boolean canBreed()
- {
- return age >= BREEDING_AGE;
- }
- /**
- * Indicate that the fox is no longer alive.
- * It is removed from the field.
- */
- private void setDead()
- {
- alive = false;
- if(location != null) {
- field.clear(location);
- location = null;
- field = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment