Iam always adding new wolf code...

This commit is contained in:
Alperen 2024-06-01 17:15:17 +02:00
parent 69bdb53030
commit e5dcb12a26
1 changed files with 96 additions and 0 deletions

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

@ -0,0 +1,96 @@
package backend;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
public class Wolf extends Agent {
int hungerLevel;
int turnsSinceLastIncrease;
Random rand = new Random();
public Wolf(int x, int y) {
super(x, y, Color.GRAY); // Wolves are gray
this.hungerLevel = 10; // Initial hunger level
this.turnsSinceLastIncrease = 0;
}
@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 = Math.max(0, hungerLevel - 5);
// Logic to remove the sheep from the simulation might be implemented here
sheep.setAlive(false);
}
@Override
public void move(int newX, int newY) {
super.move(newX, newY);
}
@Override
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
boolean hasEaten = false;
for (Agent neighbor : neighbors) {
if (neighbor instanceof Sheep) {
eat((Sheep) neighbor);
neighbors.remove(neighbor);
hasEaten = true;
break;
}
}
if (!hasEaten) {
turnsSinceLastIncrease++;
// Her üç turda bir açlık seviyesi 1 artar
if (turnsSinceLastIncrease >= 4) {
hungerLevel++;
turnsSinceLastIncrease = 0; // Sayacı sıfırla
}
} else {
turnsSinceLastIncrease = 0; // Kurt yemek yediğinde sayacı sıfırla
}
// Rastgele hareket etme
moveRandom(world);
// Açlık seviyesi 15'i geçerse ölür
return hungerLevel <= 15;
}
private void moveRandom(Simulator world) {
int direction = rand.nextInt(4);
int newX = x;
int newY = y;
switch (direction) {
case 0: newX += 1; break;
case 1: newY += 1; break;
case 2: newX -= 1; break;
case 3: newY -= 1; break;
}
// Check for looping border
if (world.isLoopingBorder()) {
newX = (newX + world.getWidth()) % world.getWidth();
newY = (newY + world.getHeight()) % world.getHeight();
} else {
// Closed borders control
if (newX < 0 || newX >= world.getWidth() || newY < 0 || newY >= world.getHeight()) {
return; // Cancel movement if trying to move out of bounds
}
}
// Hücrenin geçilebilir olup olmadığını kontrol et
if (world.getCell(newX, newY) == 0) { // Sadece boş hücreler üzerinden hareket et
x = newX;
y = newY;
}
}
}