diff --git a/src/backend/Agent.java b/src/backend/Agent.java index 1b0845a..2596d4a 100644 --- a/src/backend/Agent.java +++ b/src/backend/Agent.java @@ -7,7 +7,8 @@ public abstract class Agent { protected int x; protected int y; protected Color color; - + protected ArrayList agents; + boolean deathMarker=false; protected Agent(int x, int y, Color color) { this.x = x; this.y = y; @@ -30,6 +31,12 @@ public abstract class Agent { return dist neighbors, Simulator world); public abstract int getAgentType(); + public void toggleDeathMarker() { + deathMarker=true; + } } diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index 3f51310..17af1ac 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -13,6 +13,7 @@ public class Sheep extends Agent { int hunger; Random rand; + Sheep(int x,int y){ //first we call the constructor of the superClass(Animal) //with the values we want. @@ -30,14 +31,17 @@ public class Sheep extends Agent { * as you wish */ public boolean liveTurn(ArrayList neighbors, Simulator world) { + if(deathMarker) { + return false; + } if(world.getCell(x, y)==1) { world.setCell(x, y, 0); - hunger-=2; + hunger-=5; } else { hunger++; } this.moveRandom(); - return hunger<10; + return hunger<30; } private void moveRandom() { @@ -58,5 +62,5 @@ public class Sheep extends Agent { public int getAgentType() { return 1; } - + } diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index e86c5f3..0c486c8 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -29,8 +29,7 @@ public class Simulator extends Thread { private boolean loopingBorder; private int clickActionFlag; private int loopDelay = 150; - private ArrayList ruleSurviveCriteria= new ArrayList(); - + private ArrayList ruleSurviveCriteria= new ArrayList();//array lists used to compute the Rules private ArrayList ruleBirthCriteria=new ArrayList() ; //Rules rule = new Rules(); //TODO : add missing attribute(s) @@ -186,17 +185,18 @@ public class Simulator extends Thread { // only modify if sure of what you do // to modify agent behavior, see liveTurn method // in agent classes + ArrayList arrayDeadAgent=new ArrayList(); for(Agent agent : agents) { ArrayList neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); if(!agent.liveTurn(neighbors,this)) { //agents.remove(agent); - arrayDeadAgent.add(agents.indexOf(agent)); + arrayDeadAgent.add(agents.indexOf(agent));//if an agent doesn't fulfill its condition to live it will be listed as dead before being removed } } - for(Integer i : arrayDeadAgent) { - agents.remove(i.intValue()-arrayDeadAgent.indexOf(i)); + for(Integer i : arrayDeadAgent) {//removing agent from agents taking into account the change in index + agents.remove(i.intValue()-arrayDeadAgent.indexOf(i));//since the size of the array lowers at each iteration } //then evolution of the field @@ -266,8 +266,10 @@ public class Simulator extends Thread { */ public void clickCell(int x, int y) { //TODO : complete method - //if clickActionFlag = true then create an agent - //else if flag = false then change value of cell + //if clickActionFlag = 0 then get the position + //else if flag = 1 then change value of cell + //else if flag=2 then set a sheep + //else if flag=3 then set a wolf switch (clickActionFlag) { case 0 : System.out.print(x ); @@ -281,7 +283,7 @@ public class Simulator extends Thread { case 2 : Sheep Shaun=new Sheep(x,y); agents.add(Shaun); - System.out.println(agents); + //System.out.println(agents); break; case 3 : Wolf agrou=new Wolf(x,y); @@ -304,7 +306,7 @@ public class Simulator extends Thread { return status; } - public int getNewCell(int x, int y) { + public int getNewCell(int x, int y) {//getCell for the new array of cells for the next step //TODO : complete method with proper return int status = newCells.get(x).get(y).getAlive(); //int status = newCells.get(x).get(y); @@ -346,7 +348,7 @@ public class Simulator extends Thread { public void setCell(int x, int y, int status) { cells.get(x).get(y).setAlive(status); } - public void setNewCell(int x, int y, int status) { + public void setNewCell(int x, int y, int status) {//same but for the new cells array newCells.get(x).get(y).setAlive(status); } @@ -360,10 +362,10 @@ public class Simulator extends Thread { //We need to create an array of strings //each string should be the sum of all int transformed to strings in each Arrays of int ArrayList arrayLines=new ArrayList(); - for(int x=0;x arrayline=new ArrayList(); String lineOne=""; - for(int i=0;i lines) { + public void loadRule(ArrayList lines) {//opposite of getRule if(lines.size()<=0) { System.out.println("empty rule file"); return; @@ -608,7 +610,7 @@ public class Simulator extends Thread { public String clickActionName() { // TODO : initially return "sheep" or "cell" // depending on clickActionFlag - switch (clickActionFlag) { + switch (clickActionFlag) {//returns a string depending pn the value pf clickActionFlag case 0: return "position"; case 1: diff --git a/src/backend/Wolf.java b/src/backend/Wolf.java index aa812fa..251f13b 100644 --- a/src/backend/Wolf.java +++ b/src/backend/Wolf.java @@ -6,8 +6,9 @@ import java.util.Random; // example of basic animal. // do not hesitate to make it more complex -// and DO add at least another species that interact with it -// for example wolves that eat Wolf +//not very peculiar agent +//this class creates wolves that kill the agents that are of the Sheep subclass +//the wolves objects are moving towards any agent, making their behavior as a pack and going towards Sheep object to eat them public class Wolf extends Agent { int hunger; @@ -30,13 +31,43 @@ public class Wolf extends Agent { * as you wish */ public boolean liveTurn(ArrayList neighbors, Simulator world) { - if(world.getCell(x, y)==1) { - world.setCell(x, y, 0); - } else { - hunger++; + ArrayList inAreaAgents=new ArrayList(); + for(Agent agent : world.getAnimals()) { + if (this.getDistance(agent)<=1) { + agent.toggleDeathMarker();//if a wolf is near a sheep(radius of 1) then the sheep will die the following liveTurn + hunger-=50; + } + else if (agent.isInArea(agent.getX(), agent.getY(), 5)) {//look for agents in area and add them to the array list inAreaAgents + inAreaAgents.add(agent); + } + + }//gets the agent with the smallest distance with this wolf + if (inAreaAgents.size()>0) { + Agent smallestDistanceAgent=inAreaAgents.get(0); + for(Agent agentNear:inAreaAgents) { + if(this.getDistance(agentNear)0) { + x-=1; + }else if ((x-smallX)<0) { + x+=1; + } + if((y-smallY)>0) { + y-=1; + }else if ((y-smallY)<0) { + y+=1; + } + }else { + this.moveRandom(); } - this.moveRandom(); - return hunger>10; + + + return hunger<50; } private void moveRandom() { diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index 0b1d4b5..625d966 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -13,6 +13,7 @@ import javax.swing.event.ChangeListener; import backend.Simulator; +import javax.imageio.ImageReader; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; @@ -155,6 +156,14 @@ public class MyInterface extends JFrame { generateRandomBoard(); } }); + /**panelRight.add(btnLoadImage); + + JButton btnLoadImage = new JButton("Load Image"); + btnLoadImage.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + clicLoadImage; + } + });*/ panelRight.add(btnRandGen); @@ -321,6 +330,25 @@ public class MyInterface extends JFrame { this.repaint(); } } + /**public void clicLoadImage() { + String fileName=SelectFile(); + //ArrayList stringArray = new ArrayList(); + if (fileName.length()>0) { + try { + ImageReader fileContent = new ImageReader(new FileReader(fileName)); + String line = fileContent.readLine(); + while (line != null) { + stringArray.add(line); + line = fileContent.readLine(); + } + fileContent.close(); + } catch (Exception e) { + e.printStackTrace(); + } + mySimu.loadAgents(stringArray); + this.repaint(); + } + }*/ public void clicSaveToFileButton() {