I am trying to create a simulation to represent predation, it all works fine, but when I run it, the preys simple disappear (or die) and the predators eventually die of hunger (as there are no preys to eat). Could someone here help me trying to solve this problem. Thanks
the class with the terrain (field)
the class of the animal
the class of the predators
the class for the preys
an extra class which defines for place in the terrain
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* Write a description of class terrain here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class terrain extends World
{
private static final int pred_prob = 2;
private static final int prey_prob = 8;
// probability of creation of predators and preys anywhere (as a percentage)
private static final int width = 95;
private static final int height = 95;
/**
* Constructor for objects of class terrain.
*
*/
public terrain()
{
// Create a new world with 100x100 cells, with a cell size of 7x7 pixels.
super(width, height, 7);
add_animals();
}
private void add_animals() //adds the predators and the preys to the terrain
{
for(int i = 0; i < height; i ++)
{
for(int j = 0; j < width; j++)
{
if(Greenfoot.getRandomNumber(100) <= pred_prob)
{
predator pred = new predator(true);
addObject(pred, j, i);
}
else if(Greenfoot.getRandomNumber(100) <= prey_prob)
{
prey prey = new prey(true);
addObject(prey, j, i);
}
}
}
}
public Location random_space(int x, int y) //generate a random location within the terrain
{
int nX = x + Greenfoot.getRandomNumber(3) - 1;
int nY = y + Greenfoot.getRandomNumber(3) - 1;
if(nX < 0 || nX >= width || nY < 0 || nY >= height) //check if the location is within the terrain
{
return random_space(x, y);
}
else
{
return random_space(nX, nY);
}
}
public Location free_space(int x, int y) //try to find a free location in the terrain
{
Iterator<Location> adj = near_space(x, y);
while(adj.hasNext())
{
Location next = adj.next();
if(getObjectsAt(next.getX(), next.getY(), null).isEmpty())
{
return next;
}
}
//check is location is free
if(getObjectsAt(x, y, null).isEmpty())
{
return new Location(x, y);
}
else
{
return null;
}
}
public Iterator<Location> near_space(int x, int y) //
{
List<Location> places = new LinkedList<Location>();
for(int xo = 0; xo <= -1; xo ++)
{
int nX = x + xo;
if(nX >=0 && nX < width)
{
for(int yo = 0; yo <= 1; yo ++)
{
int nY = y + yo;
//excludes taken spaces ad the original space
if(nY >= 0 && nY < height && (xo != 0 || yo != 0))
{
places.add(new Location(nX, nY));
}
}
}
}
Collections.shuffle(places);
return places.iterator();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Animal here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Animal extends Actor
{
private int age; //the age of the animal
private boolean alive; //if it is alive or not
public Animal() // creates a new born animal (age = 0)
{
age = 0;
alive = true;
}
public boolean isAlive() // check if animal is alive, if yes, return true
{
return alive;
}
public void setDead() // change animal condition to dead
{
alive = false;
World world = getWorld();
if(world != null)
{
world.removeObject(this);
}
}
public void setAge(int age) //give the animal an age
{
this.age = age;
}
public int getAge()
{
return age;
}
public terrain get_terrain()
{
return (terrain) getWorld();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class predator here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class predator extends Animal
{
private static final int breeding_age = 14; // the age in which they can start to breed
private static final int max_age = 150; // the age the predator reaches before he dies 150
private static final int breeding_prob = 10; // the possibility of a predator breeding
private static final int max_space = 3; // the max number of births per predator
private static final int prey_value = 4; // the amount of steps a predator can take before he has to eat
// the predator's food level, it increases if it consumes a prey
private int foodLevel;
/**
* For testing purposes
*/
public predator()
{
this(true);
}
/**
* creates a fox with a random age and hunger level, if randomAge = true, fox will have random age
* and hunger level
*/
public predator(boolean randomAge)
{
super();
if(randomAge)
{
setAge(Greenfoot.getRandomNumber(max_age));
foodLevel = Greenfoot.getRandomNumber(prey_value);
}
else
{
// leaves age at 0
foodLevel = prey_value;
}
}
/**
* what the fox does the majority of the time, hunt for its prey, it might breed, or die of hunger
* of old age
*/
public void act()
{
increaseAge();
increaseHunger();
if(isAlive())
{
//new predators are born
int births = breed();
for(int b = 0; b < births; b++)
{
Location place = get_terrain().free_space(getX(), getY());
if(place != null)
{
predator newpred = new predator(false);
get_terrain().addObject(newpred, place.getX(), place.getY());
}
}
// move in the direction of the food source, if found
Location newplace = findFood(getX(), getY());
if(newplace == null)
{
newplace= get_terrain().free_space(getX(), getY());
}
if(newplace != null)
{
setLocation(newplace.getX(), getY());
}
else
{
// can move or stay where it is, leads to overpopulation
setDead();
}
}
}
/**
* increase the predator's age, this results in the predator's death
*/
private void increaseAge()
{
setAge(getAge() + 1);
if(getAge() > max_age)
{
setDead();
}
}
/**
* make the predaotr's hunger levels increase, this may result in death
*/
private void increaseHunger()
{
foodLevel --;
if(foodLevel <= 0)
{
setDead();
}
}
/**
* tells the predators to look for prey near its location
*/
private Location findFood(int x, int y)
{
List preys = getNeighbours(1, true, prey.class);
if(preys.isEmpty())
{
return null;
}
else
{
prey prey = (prey) preys.get(0);
Location place = new Location(prey.getX(), prey.getY());
prey.setDead(); // the predator eats the prey
foodLevel = prey_value;
return place;
}
}
/**
* Generate a number which represents the number of births
*/
private int breed()
{
int births = 0;
if(canBreed() && Greenfoot.getRandomNumber(100) <= breeding_prob)
{
births = Greenfoot.getRandomNumber(max_space) + 1;
}
return births;
}
/**
* String representation of the predators
*/
public String toString()
{
return "Predator, age " + getAge();
}
/**
* Determines if the preator can breed
*/
private boolean canBreed()
{
return getAge() >= breeding_age;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class prey here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class prey extends Animal
{
//characteristics shared between all preys
private static final int breeding_age = 5; //the age in which preys can start to breed
private static final int max_age = 40; //the age to which preys can live
private static final double breeding_prob = 12; //the chances for preys to breed (in percent)
private static final int max_space = 4; //the max number of preys born
/**
* Create a new prey, can be born with random age or as a new born (age = 0)
*/
public prey(boolean randomAge)
{
if(randomAge)
{
setAge(Greenfoot.getRandomNumber(max_age));
}
}
/**
* The actions performed by the preys, they can die of hunger or of old age
*/
public void act()
{
increaseAge();
if(isAlive())
{
int births = breed();
for(int b = 0; b < births; b++)
{
Location place = get_terrain().free_space(getX(), getY());
if(place != null)
{
prey newprey = new prey(false);
get_terrain().addObject(newprey, place.getX(), place.getY());
}
}
Location newplace = get_terrain().free_space(getX(), getY());
if(newplace != null) //only move if there is a freee space
{
prey prey1 = new prey(true);
setLocation(newplace.getX(), newplace.getY());
}
else //can move or remain in that place (leads to overpopulation)
{
setDead();
}
}
}
/**
* Increase the prey's age, could result in death
*/
private void increaseAge()
{
setAge(getAge() + 1);
if(getAge() > max_age)
{
setDead();
}
}
/**
* Generate a number which represents the number of births
*/
private int breed()
{
int births = 0;
if(canBreed() && Greenfoot.getRandomNumber(100) <= breeding_prob)
{
births = Greenfoot.getRandomNumber(max_space) + 1;
}
return births;
}
/**
* String representation of the preys
*/
public String toString()
{
return "Prey, age " + getAge();
}
/**
* Allows the prey to breed if they reached the breeding age
*/
private boolean canBreed()
{
return getAge() >= breeding_age;
}
}
/**
* @author (your name)
* @version (a version number or a date)
*/
public class Location //corresponds to a place on the terrain
{
// instance variables - replace the example below with your own
private int x; //horizontal coordinate
private int y; //vertical coordinate
/**
* Constructor for objects of class place
*/
public Location(int x, int y)
{
this.x = x;
this.y = y;
}
public boolean equals(Object obj)
{
if(obj instanceof Location)
{
Location other = (Location)obj;
return x == other.getX() && y == other.getY();
}
else
{
return false;
}
}
public int getX() //return x coordinate
{
return x;
}
public int getY() //return y cordinate
{
return y;
}
}
