package backend; import java.awt.Color; import java.util.ArrayList; import java.util.Random; public class Sheep extends Agent { int hunger; Random rand; public Sheep(int x, int y, Color color) { super(x, y, color); } Sheep(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.white); // 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 neighbors, Simulator world) { if(world.getCell(x, y)==1) { world.setCell(x, y, 0); hunger = 0; // Reset hunger if the sheep finds food } else { hunger++; } this.moveRandom(); return hunger <= 10; // Sheep survives if hunger is 10 or less } private void moveRandom() { int direction = rand.nextInt(4); switch(direction) { case 0: x += 1; break; case 1: y += 1; break; case 2: x -= 1; break; case 3: y -= 1; break; } } // Parsing method to create Sheep from a string public static Sheep parse(String line) { String[] parts = line.split(";"); int x = Integer.parseInt(parts[0]); int y = Integer.parseInt(parts[1]); // We don't parse color here since Sheep is always white Sheep sheep = new Sheep(x, y); sheep.hunger = Integer.parseInt(parts[2]); // Parsing hunger value return sheep; } @Override public String toString() { return x + ";" + y + ";" + hunger; } }