From e1a3b96e04bc457658c8b20f5825b62dfa4b69c3 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Wed, 29 May 2024 12:24:30 +0200 Subject: [PATCH 1/3] stop reset sliders --- src/windowInterface/MyInterface.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index 60dbdf8..bcb7dbc 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -210,6 +210,8 @@ public class MyInterface extends JFrame { mySimu = new Simulator(this); panelDraw.setSimu(mySimu); panelDraw.repaint(); + randSlider.setValue(50); + speedSlider.setValue(30); } public void setStepBanner(String s) { From cafe8800eb3eb2eef194224244877d7ba9b4c974 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Wed, 29 May 2024 13:55:02 +0200 Subject: [PATCH 2/3] agents now makestep --- src/backend/Sheep.java | 2 +- src/backend/Simulator.java | 18 ++++++++++-------- src/backend/Table.java | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index b652cc2..9420f05 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -38,7 +38,7 @@ public class Sheep extends Agent { hunger++; } this.moveRandom(); - return hunger>10; + return hunger<10; //condition to be alive } private void moveRandom() { diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 0619dd6..0647a65 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -122,17 +122,19 @@ public class Simulator extends Thread { // only modify if sure of what you do // to modify agent behavior, see liveTurn method // in agent classes - for(Agent agent : agents) { + /*for(Agent agent : agents) { ArrayList neighbors = - this.getNeighboringAnimals( - agent.getX(), - agent.getY(), - ANIMAL_AREA_RADIUS); - if(!agent.liveTurn( - neighbors, - this)) { + this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); + if(!agent.liveTurn(neighbors,this)) { agents.remove(agent); } + }*/ + + for(int i=0; i neighbors = this.getNeighboringAnimals(agents.get(i).getX(), agents.get(i).getY(), ANIMAL_AREA_RADIUS); + if(!agents.get(i).liveTurn(neighbors,this)) { + agents.remove(i); + } } //then evolution of the field //TODO-INPROGRESS : apply game rule to all cells of the field diff --git a/src/backend/Table.java b/src/backend/Table.java index b8f472e..1abbbec 100644 --- a/src/backend/Table.java +++ b/src/backend/Table.java @@ -8,7 +8,7 @@ public class Table { private ArrayList> table; private Simulator simulator; - //TODO-INPROGRESS : create constructor + //TODO-COMPLETE : create constructor public Table(int height, int width, Simulator tempSimulator) { this.height = height; this.width = width; From 8cd282c8c6cd767bcfcce79609bfe79a3d2972b1 Mon Sep 17 00:00:00 2001 From: Balthazar Squinabol Date: Wed, 29 May 2024 14:25:16 +0200 Subject: [PATCH 3/3] Fix --- src/backend/Simulator.java | 109 ++++++++++++++++------------ src/windowInterface/JPanelDraw.java | 1 - 2 files changed, 63 insertions(+), 47 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 8654ee3..256a03a 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -31,7 +31,7 @@ public class Simulator extends Thread { private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; - private boolean clickActionFlag; + private int clickActionFlag; private int loopDelay = 150; //TODO : add missing attribute(s) @@ -40,7 +40,9 @@ public class Simulator extends Thread { private int height; private boolean enableLogs; private Table table; - private boolean cellDensityToggle; + private int lowestCellValue = 0; + private int highestCellValue = 0; + private int ruleArrayListLength = 0; //Rules Arraylists private ArrayList ruleArrayList = new ArrayList(); @@ -52,7 +54,7 @@ public class Simulator extends Thread { stopFlag=false; pauseFlag=false; loopingBorder=false; - clickActionFlag=false; + clickActionFlag=1; //1 for cell, 2 for sheep, 3 for wolf agents = new ArrayList(); fieldBirthValues = new ArrayList(); @@ -64,12 +66,11 @@ public class Simulator extends Thread { this.height=LINE_NUM; enableLogs = true; // for debugging purposes table = new Table(height, width, this); - cellDensityToggle=true; //Default rule : Survive always, birth never - loadRule("ressources\\Rule\\conwayRule.json"); + loadRule("ressources/Rule/conwayRule.json"); } @@ -184,32 +185,42 @@ public class Simulator extends Thread { * method called when clicking on a cell in the interface */ public void clickCell(int x, int y) { - if (clickActionFlag) { - int currentCellValue = getCell(x, y); - int newCellValue = 0; - if(cellDensityToggle) { - if (currentCellValue <6) { - newCellValue = currentCellValue +1; - } else { - newCellValue=-1; - } - } else { - if (currentCellValue == 0) { - newCellValue = 1; - } else { - newCellValue = 0; + //ruleArrayList + if (ruleArrayListLength == 0 && highestCellValue == 0){ + + int ruleArrayListLength = ruleArrayList.size(); + + for (int i = 0; i < ruleArrayListLength; i++) { + if (ruleArrayList.get(i).getValue() > highestCellValue) { + highestCellValue = ruleArrayList.get(i).getValue(); } } + int lowestCellValue = 0; + + for (int i = 0; i < ruleArrayListLength; i++) { + if (ruleArrayList.get(i).getValue() < lowestCellValue) { + lowestCellValue = ruleArrayList.get(i).getValue(); + } + } + + System.out.println("ruleArrayListLength: " + ruleArrayListLength); + System.out.println("highestCellValue: " + highestCellValue); + System.out.println("lowestCellValue: " + lowestCellValue); + } - if (enableLogs) { - System.out.println("clickCell Called, cell :" + x + "," + y + " is now" + newCellValue + ""); + int currentCellValue = getCell(x, y); + //TODO : find highest value in ruleArrayList + int newCellValue = 0; + if(clickActionFlag == 1) { //cell + if (currentCellValue != highestCellValue) { + newCellValue = currentCellValue +1; + } + else { + newCellValue = lowestCellValue; } - this.setCell(x, y, newCellValue); - - - } - else { + } + else if(clickActionFlag == 2) { //sheep int i=0; Agent agent = new Sheep(x,y); @@ -246,10 +257,16 @@ public class Simulator extends Thread { System.out.println("clickAgent Called, Agent created at: " + x + "," + y + ""); } } - } - } - + else if(clickActionFlag == 3) { //wolf + //Make agent sheep/wolf + return; + } + if (enableLogs) { + System.out.println("clickCell Called, cell :" + x + "," + y + " is now" + newCellValue + ""); + } + } + //TODO-INPROGRESS : set agent (x y agent) load an agent to coordinates x,y public void setSheep(int x,int y){ @@ -421,13 +438,13 @@ public class Simulator extends Thread { public void toggleClickAction() { //TODO-COMPLETE : complete method - clickActionFlag = !clickActionFlag; + if (clickActionFlag != 3) { + clickActionFlag++; + } else { + clickActionFlag = 1; + } if (enableLogs) { - if (clickActionFlag) { - System.out.println("toggleClickAction called, set clickActionFlag to true"); - } else { - System.out.println("toggleClickAction called, set clickActionFlag to false"); - } + System.out.println("toggleClickAction called, set clickActionFlag to : " + clickActionFlag); } } @@ -470,6 +487,9 @@ public class Simulator extends Thread { } //DEBUG //printRules(ruleArrayList); + lowestCellValue = 0; + highestCellValue = 0; + ruleArrayListLength = 0; } @SuppressWarnings("unchecked") @@ -576,23 +596,20 @@ public class Simulator extends Thread { * @return String representation of click action */ public String clickActionName() { - // TODO-COMPLETE : initially return "sheep" or "cell" - // depending on clickActionFlag - if (clickActionFlag){ + if (clickActionFlag == 1){ return "cell"; } - else { + else if (clickActionFlag == 2){ return "sheep"; } + else if (clickActionFlag == 3){ + return "wolf"; + } + else { + return "error"; + } } - - - - - - - //debug print the list of rules public void printRules(ArrayList ruleArrayList) { System.out.println("-----------------------------------"); diff --git a/src/windowInterface/JPanelDraw.java b/src/windowInterface/JPanelDraw.java index c5711de..056c83a 100644 --- a/src/windowInterface/JPanelDraw.java +++ b/src/windowInterface/JPanelDraw.java @@ -17,7 +17,6 @@ public class JPanelDraw extends JPanel { private Simulator mySimu; private MyInterface interfaceGlobal; ArrayList> colorArrayList; - ArrayList> colorArrayList; public JPanelDraw(MyInterface itf) { super();