OOP_F3_Project/src/backend/Wolf.java

67 lines
1.4 KiB
Java

package backend;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
public class Wolf extends Agent {
int hunger;
Random rand;
Wolf(int x,int y){
//first we call the constructor of the superClass(Animal)
//with the values we want.
// here we decide that a Sheep is initially white using this constructor
super(x,y,Color.red);
// we give our sheep a hunger value of zero at birth
hunger = 0;
//we initialize the random number generator we will use to move randomly
rand = new Random();
}
/**
* action of the animal
* it can interact with the cells or with other animals
* as you wish
*/
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if (!neighbors.isEmpty()) { //checks if the adjacent cells are empty ; if it's a sheep, it makes another sheep appear and delete it, which makes both disappear
if(world.typeAnimals(x,y, neighbors)== "Sheep") {
Sheep sheep = new Sheep(x,y) ;
world.getAnimals().remove(sheep);
hunger=hunger-1;
}else {
hunger++;
}
} else {
hunger++;
}
this.moveRandom();
if (hunger<=10) {
return true;
}else {
return false;
}
}
private void moveRandom() {
int direction = rand.nextInt(4);
if(direction == 0) {
x+=1;
}
if(direction == 1) {
y+=1;
}
if(direction == 2) {
x-=1;
}
if(direction == 3) {
y-=1;
}
}
}