From be6e9da50d4e226c5dcbbc463d40a9b4817d63ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cdorian=2Eveloso=E2=80=9D?= Date: Wed, 22 May 2024 22:07:19 +0200 Subject: [PATCH] rules added and half of agents --- src/backend/Simulator.java | 133 ++++++++++++------------------------- src/backend/World.java | 124 ++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 92 deletions(-) create mode 100644 src/backend/World.java diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 40a0cb8..4a4e53d 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -18,13 +18,16 @@ public class Simulator extends Thread { private ArrayList fieldBirthValues; private ArrayList agents; - + ArrayList> rule = new ArrayList<>(); + ArrayList birthConditions = new ArrayList<>(); + ArrayList survivalConditions = new ArrayList<>(); + private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - private Grid grid; + private World world; //TODO : add missing attribute(s) @@ -38,8 +41,17 @@ public class Simulator extends Thread { agents = new ArrayList(); fieldBirthValues = new ArrayList(); fieldSurviveValues = new ArrayList(); - grid = new Grid(60, 60); - //TODO : add missing attribute initialization + + + // Normal Game of Life rules + birthConditions.add(3); // Birth condition + survivalConditions.add(2); // Survival condition + survivalConditions.add(3); // Survival condition + + rule.add(birthConditions); + rule.add(survivalConditions); + + world = new World(COL_NUM, LINE_NUM, rule); @@ -51,12 +63,12 @@ public class Simulator extends Thread { } public int getWidth() { - return grid.getWidth(); + return world.getWidth(); } public int getHeight() { //TODO : replace with proper return - return grid.getHeight(); + return world.getHeight(); } //Should probably stay as is @@ -119,61 +131,7 @@ public class Simulator extends Thread { * and the count is in the birth list, * then the cell becomes alive */ - Grid nextGrid = new Grid(60,60); - for (int x = 0; x < getWidth(); x++) { - for (int y = 0; y < getHeight(); y++) { - int actualState = getCell(x,y); - int futureState =0; - // high life - if(actualState==0) { - for (int i = 1; i<=5; i++) { - if(checkNeighbors(x,y,i)==3||checkNeighbors(x,y,i)==6) { - futureState = i; - } - } - } else { - if(checkNeighbors(x,y,actualState)==3||checkNeighbors(x,y,actualState)==2) { - futureState = actualState; - } - } - - nextGrid.setCell(x,y,futureState); - } - } - grid = nextGrid; - } - - private int checkNeighbors(int x, int y, int state) { - int nbNeighbors = 0; - - for (int i = x - 1; i <= x + 1; i++) { - for (int j = y - 1; j <= y + 1; j++) { - int x_looped = i; - int y_looped = j; - - if (loopingBorder) { - if (x_looped < 0) { - x_looped = getWidth() - 1; - } else if (x_looped >= getWidth()) { - x_looped = 0; - } - if (y_looped < 0) { - y_looped = getHeight() - 1; - } else if (y_looped >= getHeight()) { - y_looped = 0; - } - } - - if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) { - if (state == grid.getCell(x_looped, y_looped).getState()) { - nbNeighbors++; - } - } - } - } - - System.out.println(nbNeighbors); - return nbNeighbors; + world.makeStep(); } /* @@ -194,7 +152,7 @@ public class Simulator extends Thread { * method called when clicking on a cell in the interface */ public void clickCell(int x, int y) { - grid.getCell(x, y).toggleCell(); + world.clickCell(x, y);; } /** @@ -204,7 +162,7 @@ public class Simulator extends Thread { * @return value of cell */ public int getCell(int x, int y) { - return grid.getCell(x, y).getState(); + return world.getCell(x, y); } /** * @@ -238,7 +196,7 @@ public class Simulator extends Thread { * @param val to set in cell */ public void setCell(int x, int y, int val) { - grid.getCell(x, y).setState(val); + world.setCell(x, y, val); } /** @@ -299,15 +257,7 @@ public class Simulator extends Thread { * maybe just make a constructor in there * and use it here */ - for (int y = 0; y < getHeight(); y++) { - for (int x = 0; x < getWidth(); x++) { - if (Math.random() < chanceOfLife) { - setCell(x, y, 1); - } else { - setCell(x, y, 0); - } - } - } + world.generateRandom(chanceOfLife); } public boolean isLoopingBorder() { @@ -323,21 +273,23 @@ public class Simulator extends Thread { } public void toggleClickAction() { - //TODO : complete method + clickActionFlag = !clickActionFlag; } - /** - * prepare the content of a file saving present ruleSet - * as you might want to save a state, - * initialy written in this class constructor - * as a file for future use - * @return File content as an ArrayList of Lines (String) - * @see loadRule for inverse process - */ public ArrayList getRule() { - //TODO : complete method with proper return - - return null; + ArrayList lines = new ArrayList<>(); + + // Add birth conditions + for (Integer birthCondition : rule.get(0)) { + lines.add(birthCondition.toString()); + } + + // Add survival conditions + for (Integer survivalCondition : rule.get(1)) { + lines.add(survivalCondition.toString()); + } + + return lines; } public void loadRule(ArrayList lines) { @@ -345,8 +297,7 @@ public class Simulator extends Thread { System.out.println("empty rule file"); return; } - //TODO : remove previous rule (=emptying lists) - + rule.clear(); String surviveLine = lines.get(0); String birthLine = lines.get(1); @@ -354,7 +305,7 @@ public class Simulator extends Thread { for(int x=0; x> rule; + + public World(int sizeX, int sizeY, ArrayList> _rule) { + width = sizeX; + height = sizeY; + rule = _rule; + grid = new Grid(sizeX, sizeY); + } + + public void generateRandom(float chanceOfLife) { + //TODO : complete method + /* + * Advice : + * as you should probably have a separate class + * representing the field of cells... + * maybe just make a constructor in there + * and use it here + */ + for (int y = 0; y < getHeight(); y++) { + for (int x = 0; x < getWidth(); x++) { + if (Math.random() < chanceOfLife) { + setCell(x, y, 1); + } else { + setCell(x, y, 0); + } + } + } + } + + public void makeStep() { + + Grid nextGrid = new Grid(width,height); + for (int x = 0; x < getWidth(); x++) { + for (int y = 0; y < getHeight(); y++) { + int actualState = getCell(x,y); + int futureState =0; + // high life + if(actualState==0) { + for (int i = 1; i<=5; i++) { + //Birth + if(rule.get(0).contains(checkNeighbors(x, y, i))) { + futureState = i; + } + } + } else { + //Survival + if(rule.get(1).contains(checkNeighbors(x, y, actualState))) { + futureState = actualState; + } + } + + nextGrid.setCell(x,y,futureState); + } + } + grid = nextGrid; + } + + private int checkNeighbors(int x, int y, int state) { + int nbNeighbors = 0; + + for (int i = x - 1; i <= x + 1; i++) { + for (int j = y - 1; j <= y + 1; j++) { + int x_looped = i; + int y_looped = j; + + if (loopingBorder) { + if (x_looped < 0) { + x_looped = getWidth() - 1; + } else if (x_looped >= getWidth()) { + x_looped = 0; + } + if (y_looped < 0) { + y_looped = getHeight() - 1; + } else if (y_looped >= getHeight()) { + y_looped = 0; + } + } + + if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) { + if (state == grid.getCell(x_looped, y_looped).getState()) { + nbNeighbors++; + } + } + } + } + + System.out.println(nbNeighbors); + return nbNeighbors; + } + + public void clickCell(int x, int y) { + grid.getCell(x, y).toggleCell(); + } + + public void setCell(int x, int y, int val) { + grid.getCell(x, y).setState(val); + } + + public void setLoopingBorders(boolean loop) { + loopingBorder = loop; + } + + public int getCell(int x, int y) { + return grid.getCell(x, y).getState(); + } + + public int getWidth() { + return grid.getWidth(); + } + + public int getHeight() { + return grid.getHeight(); + } +}