From 7237dc03118d934c61d0b824f91541f625757e29 Mon Sep 17 00:00:00 2001 From: Alperen Date: Fri, 31 May 2024 23:07:20 +0200 Subject: [PATCH] I forgot wolf.java --- src/backend/Wolf.java | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/backend/Wolf.java diff --git a/src/backend/Wolf.java b/src/backend/Wolf.java new file mode 100644 index 0000000..640d0ff --- /dev/null +++ b/src/backend/Wolf.java @@ -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 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; + } + } +}