Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

Organism.txt

VIEWS: 11 PAGES: 5

									//Tasha Wallage //Period 7 //herbivore species that eats Food and gets eaten by Predator public class Organism implements Species { private private private private private int speed, energy, defense, gender, xregen, stepper, range; double deathRate, age; World wd; Location myLoc; 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<range) x++; } else{

wd.remove(this); if(y > yLoc && y>0) y--; else if(y < yLoc && y <range) y++; } myLoc.setLoc(x, y); wd.add(this); } //removes the Organism from the World based on the deathRate probability (increases with age) public void die() { if(Math.random() < deathRate) wd.remove(this); } //recharges energy (eating Food) public void eat(int units) { energy+=units; } //creates a new Organsim only if there is space and a partner to breed with public boolean breed(Organism o) { if(gender==2 || xregen==0) return false; if(wd.getVacantSpot(o.getLoc())==null) return false; wd.add(breed2(o)); return true; } //reduces energy and xregen and creates a new Organism based on the traits of itself and the father public Organism breed2(Organism o) { energy-=10; xregen-=1; int int int int childSpeed = crossOver(speed, o.getSpeed()); childDefense = crossOver(defense, o.getDefense()); childGender = (int)(Math.random()*2+1); childXregen = crossOver(xregen, o.getXregen());

if(childDefense>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 = trait1+1; while(childTrait > 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; } }


								
To top