I forgot wolf.java

This commit is contained in:
Alperen 2024-05-31 23:07:20 +02:00
parent a1bd248d15
commit 7237dc0311
1 changed files with 61 additions and 0 deletions

61
src/backend/Wolf.java Normal file
View File

@ -0,0 +1,61 @@
package backend;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
public class Wolf extends Agent {
int hungerLevel;
Random rand = new Random();
public Wolf(int x, int y) {
super(x, y, Color.GRAY); // Wolves are gray
this.hungerLevel = 10; // Initial hunger level
}
@Override
public void interact(Agent other) {
if (other instanceof Sheep) {
eat((Sheep) other);
}
}
public void eat(Sheep sheep) {
// Increase hunger level when eating a sheep
this.hungerLevel += 5;
// Logic to remove the sheep from the simulation might be implemented here
}
@Override
public void move(int newX, int newY) {
super.move(newX, newY);
}
@Override
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
// Wolves might prioritize eating sheep if they are nearby
for (Agent neighbor : neighbors) {
if (neighbor instanceof Sheep) {
eat((Sheep) neighbor);
neighbors.remove(neighbor);
break;
}
}
hungerLevel--;
this.moveRandom();
return hungerLevel > 0;
}
private void moveRandom() {
int direction = rand.nextInt(4);
if (direction == 0) {
x += 1;
} else if (direction == 1) {
y += 1;
} else if (direction == 2) {
x -= 1;
} else if (direction == 3) {
y -= 1;
}
}
}