From bf027c18aa8d85ccf377911eca6398241e07338b Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Wed, 8 May 2024 11:04:35 +0200 Subject: [PATCH 1/8] changed color of sheep --- src/backend/Sheep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index b05d141..1e04019 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -17,7 +17,7 @@ public class Sheep extends Agent { //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); + super(x,y,Color.pink); // 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 From 53dd740c037e6644aff1b5fa26b137a3ccefd0c6 Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Wed, 8 May 2024 11:04:35 +0200 Subject: [PATCH 2/8] changed color of sheep --- src/backend/Sheep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index b05d141..1e04019 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -17,7 +17,7 @@ public class Sheep extends Agent { //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); + super(x,y,Color.pink); // 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 From 65fe9efa34d414889d59a88e69ba25085f08467b Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Wed, 22 May 2024 11:51:28 +0200 Subject: [PATCH 3/8] agent implementation in progress --- src/backend/Agent.java | 11 +++++++++ src/backend/Cell.java | 6 ----- src/backend/Simulator.java | 46 +++++++++++++++++++++++++++++++++++++- src/backend/Table.java | 1 - 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/backend/Agent.java b/src/backend/Agent.java index 9eeef1e..b5c48ee 100644 --- a/src/backend/Agent.java +++ b/src/backend/Agent.java @@ -23,6 +23,17 @@ public abstract class Agent { public int getY() { return y; } +//presence of an agent at those coordinates + public boolean agentPresence(int x, int y){ + + if (this.getX() == x && this.getY() == y) { + return true; + } + else { + return false; + } + } + public boolean isInArea(int x, int y, int radius) { int diffX = this.x-x; int diffY = this.y-y; diff --git a/src/backend/Cell.java b/src/backend/Cell.java index 73a136f..8151c46 100644 --- a/src/backend/Cell.java +++ b/src/backend/Cell.java @@ -3,7 +3,6 @@ package backend; public class Cell { private int value; - public Cell(int value) { this.value = value; } @@ -12,12 +11,7 @@ public class Cell { return value; } - - public void setValue(int value) { this.value = value; } - - - } diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 7b4ff54..f767b0d 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,4 +1,5 @@ package backend; +import java.awt.Color; import java.util.ArrayList; import windowInterface.MyInterface; @@ -448,7 +449,7 @@ public class Simulator extends Thread { * @return String representation of click action */ public String clickActionName() { - // TODO : initially return "sheep" or "cell" + // TODO-COMPLETE : initially return "sheep" or "cell" // depending on clickActionFlag if (clickActionFlag){ return "cell"; @@ -458,4 +459,47 @@ public class Simulator extends Thread { } } + //TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y + public void setSheep(int x,int y){ + Sheep sheep = new Sheep(x,y); + agents.add(sheep); + printAgents(); + } + +//method for clickAgent first setup to spawn sheeps + public void clickAgent(int x, int y) { + if (clickActionFlag == false) { + Sheep sheep = new Sheep(x,y); + + this.setSheep(x,y); + //process to check if there is already an agent (removes the agent if clicking on it) + //if (agentPresence(x,y) == true) { + + + //} + + + if (enableLogs) { + System.out.println("clickAgent Called, Agent spawned at coordinates:" + x + "," + y + ""); + } + + + } + else { + return; + } + } + public void printAgents() { + for (Agent agent : agents) { + String agentType = agent.getClass().getSimpleName(); + int x = agent.getX(); + int y = agent.getY(); + Color color = agent.getDisplayColor(); + + System.out.println("Agent Type: " + agentType); + System.out.println("Position: (" + x + ", " + y + ")"); + System.out.println("Color: " + color); + System.out.println(); + } + } } diff --git a/src/backend/Table.java b/src/backend/Table.java index 878f86b..8d17373 100644 --- a/src/backend/Table.java +++ b/src/backend/Table.java @@ -90,7 +90,6 @@ public class Table { } - //TODO : set agent (x y agent) load an agent to coordinates x,y //TODO : set random (density) create a random table of determined density public void setRandom(double density) { From 99fb4fdb02b0f97fcd7576d9320f749a75954d70 Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Tue, 28 May 2024 14:09:15 +0200 Subject: [PATCH 4/8] advancement on agents with partial update on clickAgent removal process --- src/backend/Agent.java | 2 +- src/backend/Cell.java | 2 +- src/backend/Sheep.java | 2 + src/backend/Simulator.java | 105 +++++++++++++++++++------------------ 4 files changed, 59 insertions(+), 52 deletions(-) diff --git a/src/backend/Agent.java b/src/backend/Agent.java index b5c48ee..417c402 100644 --- a/src/backend/Agent.java +++ b/src/backend/Agent.java @@ -33,7 +33,7 @@ public abstract class Agent { return false; } } - +//presenec of an agent in an area public boolean isInArea(int x, int y, int radius) { int diffX = this.x-x; int diffY = this.y-y; diff --git a/src/backend/Cell.java b/src/backend/Cell.java index 8151c46..0f32eb6 100644 --- a/src/backend/Cell.java +++ b/src/backend/Cell.java @@ -14,4 +14,4 @@ public class Cell { public void setValue(int value) { this.value = value; } -} +} \ No newline at end of file diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index 1e04019..b652cc2 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -24,6 +24,8 @@ public class Sheep extends Agent { rand = new Random(); } + + /** * action of the animal * it can interact with the cells or with other animals diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index f767b0d..5c857c5 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -8,8 +8,8 @@ public class Simulator extends Thread { private MyInterface mjf; - private final int COL_NUM = 100; - private final int LINE_NUM = 100; + private final int COL_NUM = 10; + private final int LINE_NUM = 10; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -17,9 +17,7 @@ public class Simulator extends Thread { private final int ANIMAL_AREA_RADIUS = 2; private ArrayList fieldSurviveValues; private ArrayList fieldBirthValues; - private ArrayList agents; - private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; @@ -71,7 +69,10 @@ public class Simulator extends Thread { //TODO-COMPLETE : replace with proper return return this.height; } - + //we define the initial size of the arraylist as 1 in order to ease the code in the clickAgent part + public void StartingCap(int initialCapacity){ + agents = new ArrayList<>(initialCapacity); + } //Should probably stay as is public void run() { int stepCount=0; @@ -199,11 +200,56 @@ public class Simulator extends Thread { } this.setCell(x, y, newCellValue); - } else { - return; + + + } + else { + int radius = 1; + Agent agent = new Sheep(x,y); + + if(agent.isInArea(x,y,radius)){ + System.out.println("true"); + for(int i=0;i<=agents.size();i++){ + if (agents.get(i) instanceof Sheep){ + agents.remove(i); + } + else{ + setSheep(x,y); + } + } + } + + + if (enableLogs) { + System.out.println("clickAgent Called, Agent created at: " + x + "," + y + ""); + } } } + //TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y + public void setSheep(int x,int y){ + + Sheep sheep = new Sheep(x,y); + + agents.add(sheep); + } + + + + public void printAgents() { + for (Agent Agent : agents) { + String agentType = Agent.getClass().getSimpleName(); + int x = Agent.getX(); + int y = Agent.getY(); + Color color = Agent.getDisplayColor(); + + System.out.println("Agent Type: " + agentType); + System.out.println("Position: (" + x + ", " + y + ")"); + System.out.println("Color: " + color); + System.out.println(); + } + + } /** * get cell value in simulated world * @param x coordinate of cell @@ -459,47 +505,6 @@ public class Simulator extends Thread { } } - //TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y - public void setSheep(int x,int y){ - Sheep sheep = new Sheep(x,y); - agents.add(sheep); - printAgents(); - } - -//method for clickAgent first setup to spawn sheeps - public void clickAgent(int x, int y) { - if (clickActionFlag == false) { - Sheep sheep = new Sheep(x,y); - - this.setSheep(x,y); - //process to check if there is already an agent (removes the agent if clicking on it) - //if (agentPresence(x,y) == true) { - - - //} - - - if (enableLogs) { - System.out.println("clickAgent Called, Agent spawned at coordinates:" + x + "," + y + ""); - } - - - } - else { - return; - } - } - public void printAgents() { - for (Agent agent : agents) { - String agentType = agent.getClass().getSimpleName(); - int x = agent.getX(); - int y = agent.getY(); - Color color = agent.getDisplayColor(); - - System.out.println("Agent Type: " + agentType); - System.out.println("Position: (" + x + ", " + y + ")"); - System.out.println("Color: " + color); - System.out.println(); - } - } + + } From 184c5bd7598d78a82b9e25f4c0bdace561063ab1 Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Tue, 28 May 2024 14:54:32 +0200 Subject: [PATCH 5/8] ajout d'un null agent --- src/backend/NullAgent | 14 ++++++++++++++ src/backend/Simulator.java | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/backend/NullAgent diff --git a/src/backend/NullAgent b/src/backend/NullAgent new file mode 100644 index 0000000..212f6e5 --- /dev/null +++ b/src/backend/NullAgent @@ -0,0 +1,14 @@ +package backend; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.Random; +//null agent that signifies the absence of agents +public class NullAgent extends Agent { + + NullAgent(int x,int y){ + super(x,y,Color.black); + } + + +} \ No newline at end of file diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 5c857c5..2c9c4f3 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -23,7 +23,7 @@ public class Simulator extends Thread { private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - + //TODO : add missing attribute(s) private double randomDansitySlider = 0.5; private int width; @@ -71,7 +71,14 @@ public class Simulator extends Thread { } //we define the initial size of the arraylist as 1 in order to ease the code in the clickAgent part public void StartingCap(int initialCapacity){ + // Initial capacity of the agent arraylist + initialCapacity = COL_NUM*LINE_NUM; agents = new ArrayList<>(initialCapacity); + //we fill it with null values + for (int i = 0; i <= agents.size(); i++) { + agents.add(i,NullAgent.NullAgent); + } + System.out.println(agents); } //Should probably stay as is public void run() { @@ -206,15 +213,17 @@ public class Simulator extends Thread { else { int radius = 1; Agent agent = new Sheep(x,y); - + System.out.println(agents); if(agent.isInArea(x,y,radius)){ - System.out.println("true"); + for(int i=0;i<=agents.size();i++){ if (agents.get(i) instanceof Sheep){ agents.remove(i); + System.out.println("Corresponding agent found, proceeding with removal"); } else{ setSheep(x,y); + System.out.println("Corresponding agent not found, proceeding with creation"); } } } From 96c202115d3908dbe881db7101907ba1028996d3 Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Wed, 29 May 2024 11:10:29 +0200 Subject: [PATCH 6/8] functioning adding and removal of agents --- src/backend/NullAgent | 14 ---------- src/backend/Simulator.java | 52 ++++++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 src/backend/NullAgent diff --git a/src/backend/NullAgent b/src/backend/NullAgent deleted file mode 100644 index 212f6e5..0000000 --- a/src/backend/NullAgent +++ /dev/null @@ -1,14 +0,0 @@ -package backend; - -import java.awt.Color; -import java.util.ArrayList; -import java.util.Random; -//null agent that signifies the absence of agents -public class NullAgent extends Agent { - - NullAgent(int x,int y){ - super(x,y,Color.black); - } - - -} \ No newline at end of file diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 2c9c4f3..3da398e 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -69,17 +69,7 @@ public class Simulator extends Thread { //TODO-COMPLETE : replace with proper return return this.height; } - //we define the initial size of the arraylist as 1 in order to ease the code in the clickAgent part - public void StartingCap(int initialCapacity){ - // Initial capacity of the agent arraylist - initialCapacity = COL_NUM*LINE_NUM; - agents = new ArrayList<>(initialCapacity); - //we fill it with null values - for (int i = 0; i <= agents.size(); i++) { - agents.add(i,NullAgent.NullAgent); - } - System.out.println(agents); - } + //Should probably stay as is public void run() { int stepCount=0; @@ -211,24 +201,42 @@ public class Simulator extends Thread { } else { - int radius = 1; + int i=0; Agent agent = new Sheep(x,y); - System.out.println(agents); - if(agent.isInArea(x,y,radius)){ + + //if there are no agents, skip directly to adding one + boolean removal =false; + if (agents.size()>0 ){ - for(int i=0;i<=agents.size();i++){ - if (agents.get(i) instanceof Sheep){ + //if an agent is in this area we iterate in the arraylist agents in order to find which one it is + + for(i=0;i<=agents.size()-1;i++){ + + //if we proceed to find the agent on the coordinates of the click, we remove it + + if (agents.get(i).getX() == x && agents.get(i).getY() == y ){ agents.remove(i); System.out.println("Corresponding agent found, proceeding with removal"); + System.out.println(agents.size()); + removal = true; + } - else{ - setSheep(x,y); - System.out.println("Corresponding agent not found, proceeding with creation"); - } + + + } + if(i==agents.size() && removal ==false){ + //if we find no corresponding agent we add one + System.out.println("no agents to remove, proceeding with creation"); + setSheep(x, y); + } + + } + else{ + System.out.println("1st iteration"); + + setSheep(x,y); } - - if (enableLogs) { System.out.println("clickAgent Called, Agent created at: " + x + "," + y + ""); } From a42a6d69ea4437523c0856f988690260f48ff6ac Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Wed, 29 May 2024 11:12:02 +0200 Subject: [PATCH 7/8] changed a comment --- src/backend/Simulator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 3da398e..c0737ec 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -226,7 +226,7 @@ public class Simulator extends Thread { } if(i==agents.size() && removal ==false){ - //if we find no corresponding agent we add one + //if we find no corresponding agent after the for loop, we add one System.out.println("no agents to remove, proceeding with creation"); setSheep(x, y); } From 1e367b848bd47c7a425a7e09addee75b2e1f3202 Mon Sep 17 00:00:00 2001 From: g le-chartier Date: Wed, 29 May 2024 11:23:28 +0200 Subject: [PATCH 8/8] put back color in simulator --- src/backend/Simulator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 1eca4bc..7df39cd 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,4 +1,5 @@ package backend; +import java.awt.Color; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;