diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 010b67b..3742447 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -22,10 +22,10 @@ public class Simulator extends Thread { private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; - private int clickActionFlag; + private int clickActionFlag; // change from boolean to int private int loopDelay = 150; - World currentWorld = new World(getWidth(),getHeight()); + World currentWorld = new World(getWidth(),getHeight()); // create the instance of class World //TODO : add missing attribute(s) @@ -34,7 +34,7 @@ public class Simulator extends Thread { stopFlag=false; pauseFlag=false; loopingBorder=false; - clickActionFlag=0; + clickActionFlag=0; //initallize the clickactionflag by 0 agents = new ArrayList(); fieldBirthValues = new ArrayList(); @@ -52,8 +52,8 @@ public class Simulator extends Thread { } public int getWidth() { - //TODO : replace with proper return - return COL_NUM; + //TODO replace with proper return + return COL_NUM; } public int getHeight() { @@ -190,6 +190,9 @@ public class Simulator extends Thread { else if(clickActionFlag ==2) { agents.add(new Sheep(x,y)); } + else if (clickActionFlag == 3) { + agents.add(new Wolf(x,y)); + } } /** @@ -412,6 +415,9 @@ public class Simulator extends Thread { agents.add(new Cow(agentValues[0],agentValues[1])); } + if(agentValues[2]==3) { + agents.add(new Wolf(agentValues[0],agentValues[1])); + } } } @@ -423,14 +429,25 @@ public class Simulator extends Thread { // TODO : initially return "sheep" or "cell" // depending on clickActionFlag if(clickActionFlag == 1) { - return "Cow"; + return "cow"; } else if (clickActionFlag == 0) { return "cell"; } + else if(clickActionFlag == 2){ + return "wolf"; + } else { return "sheep"; } } + public String typeAnimals(int x , int y,ArrayList neighbor) { + Agent agent = neighbor.get(0); + if (agent instanceof Wolf) { + return "Wolf"; + } else { + return "Sheep"; + } + } } diff --git a/src/backend/Wolf.java b/src/backend/Wolf.java new file mode 100644 index 0000000..7ef89e4 --- /dev/null +++ b/src/backend/Wolf.java @@ -0,0 +1,67 @@ +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 neighbors, Simulator world) { + //ArrayList nearby=world.getNeighboringAnimals(x,y,1); //look if the cell has an agent + if (!neighbors.isEmpty()) { + 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; + } + } + + +}