diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 6450bde..5659dc6 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -29,6 +29,8 @@ public class Simulator extends Thread { private int clickActionFlag; private int loopDelay = 150; private int[][] world; + private int [] survivalRulesArray; + private int[] birthRulesArray; //TODO : add missing attribute(s) private int stepCount; @@ -63,6 +65,48 @@ public class Simulator extends Thread { public int getHeight() { return LINE_NUM; } + + public int[] getBirthRulesArray() { + return this.birthRulesArray; + } + + public int[] getSurvivalRulesArray() { + return this.survivalRulesArray; + } + + public void loadRule(ArrayList row) { + String surviveRulesRow = row.get(0); + String[] surviveCells = surviveRulesRow.split(";"); + String birthRulesRow = row.get(1); + String[] birthCells = birthRulesRow.split(";"); + survivalRulesArray = new int[surviveCells.length]; + birthRulesArray = new int[birthCells.length]; + + + if (row.size() <= 0) { + System.out.println("wrong file buddy, this one's empty"); + }else if(surviveCells.length<=0) { + System.out.println("wrong file buddy, this one's does not have survival rules, won't work"); + return; + }else { + for (int x = 0; x < birthCells.length; x++) { + String elem = birthCells[x]; + int value = Integer.parseInt(elem); + birthRulesArray[x] = value; + } +//determines the number of alive neighboring cells needed to birth, and places them in the birthCells list + for (int x = 0; x < surviveCells.length; x++) { + String elem = surviveCells[x]; + int value = Integer.parseInt(elem); + survivalRulesArray[x] = value; + } +//determines the number of alive neighboring cells needed to survive, and places them in the surviveCells list + } + + + } + + //Should probably stay as is public void run() { @@ -112,6 +156,13 @@ public class Simulator extends Thread { } + /*public void setWorld(int[][] world, int width, int height) { + this.world = world; + this.COL_NUM = width; + this.LINE_NUM = height; + }*/ + + public void makeStep() { // agent behaviors first @@ -120,8 +171,6 @@ public class Simulator extends Thread { // in agent classes - int[][] newWorld = new int[getWidth()][getHeight()]; - ArrayList newAgents = new ArrayList<>(); for(Agent agent : agents) { ArrayList neighbors = @@ -137,7 +186,7 @@ public class Simulator extends Thread { // Apply Game of Life rules - for (int x = 0; x < getWidth(); x++) { + /*for (int x = 0; x < getWidth(); x++) { for (int y = 0; y < getHeight(); y++) { int aliveNeighbors = countAliveNeighbors(x, y); if (world[x][y] == 1) { @@ -147,14 +196,34 @@ public class Simulator extends Thread { } } - } - world = newWorld; + }*/ + + + int[][] newWorld = new int[getWidth()][getHeight()]; + + for (int x = 0; x < getWidth(); x++) { + for (int y = 0; y < getHeight(); y++) { + int aliveNeighbors = countAliveNeighbors(x, y); + if (getCell(x, y) == 1) { + //newWorld[x][y] = shouldSurvive(aliveNeighbors) ? 1 : 0; + } else { + //newWorld[x][y] = shouldBeBorn(aliveNeighbors) ? 1 : 0; + } + } + } + + //setWorld(newWorld, getWidth(), getHeight()); + } + + + + //world = newWorld; - } + //then evolution of the field // TODO : apply game rule to all cells of the field @@ -303,12 +372,7 @@ public class Simulator extends Thread { } } - /** - * called by button, with slider providing the argument - * makes a new world state with random cell states - * @param chanceOfLife the chance for each cell - * to be alive in new state - */ + public ArrayList getRule() { //to check ArrayList rule = new ArrayList<>(); for (int i = 0; i < getHeight(); i++) { @@ -323,35 +387,7 @@ public class Simulator extends Thread { } return rule; } - public void loadRule(ArrayList lines) { //to check - /* - * First some checks that the file is usable - * We call early returns in conditions like this - * "Guard clauses", as they guard the method - * against unwanted inputs - */ - if(lines.size()<=0) { - return; - } - String firstLine = lines.get(0); - String[] firstLineElements = firstLine.split(";"); - if(firstLineElements.length<=0) { - return; - } - /* - * now we fill in the world - * with the content of the file - */ - for(int y =0; y