//Tasha Wallage //Period 7 //herbivore species that eats Food and gets eaten by Predator public class Organism implements Species { private int speed, energy, defense, gender, xregen, stepper, range; private double deathRate, age; private World wd; private Location myLoc; private Food food; public Organism(World wd, int range) { this.wd = wd; this.range = range; int x = (int)(Math.random()*range); int y = (int)(Math.random()*range); myLoc = new Location(x, y); speed = 1; energy = 500; age = 0; defense = 5; gender = 1; xregen = 10; stepper = 0; deathRate = 0.009; food = (Food)wd.getClosest("Food",getLoc()); } public Organism(World wd, Location loc, int speed, int defense, int gender, int xregen, int range) { this.range = range; this.wd = wd; myLoc = loc; this.speed = speed; this.defense = defense; this.gender = gender; this.xregen = xregen; energy = 500; age = 0; stepper = 0; deathRate = 0.009; food = (Food)wd.getClosest("Food",getLoc()); } //determines the next action of an Organism based on age (different preferences such as breeding) public void act() { stepper ++; energy --; age = ((double)stepper/(double)speed)/365.25; food = (Food)wd.getClosest("Food",getLoc()); if(age < 1){ if(food !=null) move(food.getLoc()); die(); } else if (age >=1 && age < 40){ deathRate+=.005; if(!breed(((Organism)wd.getClosest("Organism",getLoc())))) if(food !=null) move(food.getLoc()); die(); } else if (age >=40 && age < 60){ deathRate+=.001; if((int)Math.random()*2%2==0 || !breed(((Organism)wd.getClosest("Organism",getLoc())))) if(food !=null) move(food.getLoc()); die(); } else if(age > 60){ deathRate+=.01; if(food !=null) move(food.getLoc()); die(); } if(energy==0) wd.remove(this); } //moves the Organism closer to the specified Location public void move(Location loc) { Location oldLoc = getLoc(); int x = myLoc.getX(); int y = myLoc.getY(); int xLoc = loc.getX(); int yLoc = loc.getY(); if(xLoc==-1 && yLoc == -1) return; else if(x==xLoc && y==yLoc && wd.consume(food, this)) { food = (Food)wd.getClosest("Food", getLoc()); return; } else if((xLoc!=x) && (yLoc == y || (int)(Math.random()*2)%2==0)) { wd.remove(this); if(x > xLoc && x>0 ) x--; else if( x < xLoc && x yLoc && y>0) y--; else if(y < yLoc && y 3 && childSpeed >3) childSpeed=2; else if(childDefense>6 && childSpeed>2) childSpeed=1; Organism child = new Organism(wd, wd.getVacantSpot(o.getLoc()), childSpeed, childDefense, childGender, childXregen, range); return child; } //crosses over the genes of the parents to form a new unique individual (sometimes mutation occurs) public int crossOver(int trait1, int trait2) { //determines the trait of the organism based on that of the parents int childTrait = 0; if(trait2 trait1){ childTrait = (int)(Math.random()*trait1) + trait2; if(((int)(Math.random()*101)==9)) break; } } else{ childTrait = trait2+1; while(childTrait > trait2){ int rand = (int)(Math.random()*3); if(rand==0) childTrait = (int)(Math.random()*trait2) + trait1; else if(rand==1) childTrait = (int)(Math.random()*trait1) + trait2; else childTrait = (int)(Math.random()*trait2) + (int)(Math.random()*trait1); if(((int)(Math.random()*10)==9)) break; } } return childTrait; } public Location getLoc() { return myLoc; } public int getSpeed() { return speed; } public int getDefense() { return defense; } public int getXregen() { return xregen; } public String getName() { return "Organism"; } public String toString() { return "O["+myLoc.getX()+", "+myLoc.getY()+"]"+"S"+speed+" D"+defense+" E"+energy+" A"+age+"G"+gender; } }