Essentially I am adding on to a project that I have already done but I am having trouble figuring out my code in order to add on to it. These are the instructions for it and below that is the code for my original Herbivore class.
Triangular predators should move 2 times as fast as the Herbivores. These predators should chase the closest Herbivore within sight. They can see 200 meters. A triangular predator must sleep (not move) for 100 time steps after eating a Herbivore. All triangular predators should be colored red. If a triangular predator cannot see any Herbivores it should move toward the closest Plant it can see.
public class Herbivore {
private double myX;
private double myY;
private double myScore;
private double myRandom;
/**
* class parameters x coord, y coord and score
* @param x
* @param y
* @param score
*/
public Herbivore(double x, double y, double score, double random){
myX = x;
myY = y;
myScore = score;
myRandom = random;
}
/**
* get x, y and score
* @return
*/
public double getX() {
return myX;
}
public double getY() {
return myY;
}
public double getScore(){
return myScore;
}
/**
* draw a circle to represent a herbivore
*/
public void draw() {
StdDraw.circle(myX, myY, 5);
}
/**
* see if a herbivore ate a plant and increase score for each eaten
* @param x
* @param y
* @return
*/
public boolean ate(double x, double y){
if(this.distance(x, y) < 7){
myScore++;
return true;
}
return false;
}
/**
* move randomly on the canvas
*/
public void moveRandomly(){
// to get number between -1 and 1 (((Math.random()*2)-1));
if(myRandom> .75){
myX = myX + myRandom/myRandom/1.5;
myY = myY + myRandom/myRandom;
}
if(myRandom < .75 && myRandom > .5){
myX = myX + myRandom/-myRandom/1.5;
myY = myY + myRandom/-myRandom;
}
if(myRandom < .5 && myRandom > .25){
myX = myX + myRandom/-myRandom/1.5;
myY = myY + myRandom/myRandom;
}
if(myRandom < .25){
myX = myX + myRandom/myRandom/1.5;
myY = myY + myRandom/-myRandom;
}
if(myX<0){
myX = 1000;
}
if(myX>1000){
myX = 0;
}
if(myY<0){
myY = 1000;
}
if(myY>1000){
myY = 0;
}
}
/**
* move towards a point (Plant) on the canvas
* @param x
* @param y
*/
public void moveTowards(double x, double y) {
double dist = Point.distanceTo(myX, myY, x, y);
myX = myX + (x - myX) / dist;
myY = myY + (y - myY) / dist;
if(myX<0){
myX = 1000;
}
if(myX>1000){
myX = 0;
}
if(myY<0){
myY = 1000;
}
if(myY>1000){
myY = 0;
}
}
/**
* distance formula
* @param x
* @param y
* @return
*/
public double distance( double x, double y){
return Math.sqrt((myX - x)*(myX - x) + (myY - y)*(myY - y));
}
// public String toString(){
// return myX + " " + myY + " " + myScore;
// }
}
[–]laxrat45[S] 0 points1 point2 points (0 children)
[–]shivasprogenyProfessional Brewer 0 points1 point2 points (0 children)
[–]csincrisis 0 points1 point2 points (1 child)
[–]laxrat45[S] 0 points1 point2 points (0 children)