diff --git a/Rule/BlobRule.csv b/Rule/BlobRule.csv index 922060c..f182c11 100644 --- a/Rule/BlobRule.csv +++ b/Rule/BlobRule.csv @@ -1,2 +1,2 @@ -1;3;5;8 -3;5;7 +"1;3;5;8" +"3;5;7" diff --git a/Rule/ConwayRule.csv b/Rule/ConwayRule.csv index 3563b76..705c40a 100644 --- a/Rule/ConwayRule.csv +++ b/Rule/ConwayRule.csv @@ -1,2 +1,2 @@ -2;3 -3 \ No newline at end of file +"2;3" +3 diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 795a360..e77f48c 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -29,9 +29,11 @@ public class Simulator extends Thread { private int clickActionFlag; private int loopDelay = 150; private int[][] world; - private int [] survivalRulesArray; - private int[] birthRulesArray; - + //private int[] survivalRulesArray; + //private int[] birthRulesArray; + private int[] birthRulesArrays; + private int[] surviveRulesArrays; + //TODO : add missing attribute(s) private int stepCount; @@ -67,42 +69,73 @@ public class Simulator extends Thread { } public void loadRule(ArrayList row) { - String surviveRulesRow = row.get(0); + String surviveRulesRow = row.get(0).replace("\"","").trim(); String[] surviveCells = surviveRulesRow.split(";"); - String birthRulesRow = row.get(1); + String birthRulesRow = row.get(1).replace("\"","").trim(); String[] birthCells = birthRulesRow.split(";"); - survivalRulesArray = new int[surviveCells.length]; - birthRulesArray = new int[birthCells.length]; + this.birthRulesArrays = new int[birthCells.length]; + this.surviveRulesArrays = new int[surviveCells.length]; if (row.size() <= 0) { System.out.println("wrong file buddy, this one's empty"); - }else if(surviveCells.length<=0) { + }else if(row.size() <= 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; + try { + for (int x = 0; x < surviveCells.length; x++) { + String elem = surviveCells[x].trim(); + int value = Integer.parseInt(elem); + surviveRulesArrays[x] = value; + } + + for (int x = 0; x < birthCells.length; x++) { + String elem = birthCells[x].trim(); + int value = Integer.parseInt(elem); + birthRulesArrays[x] = value; + } + + System.out.println("Rules loaded successfully"); + } catch (NumberFormatException e) { + System.out.println("Error parsing rule values: " + e.getMessage()); + } + } + /*for (int x = 0; x < birthCells.length; x++) { + String[] birthElements = birthCells[x].split(","); + int[] birthRulesArray = new int[birthElements.length]; + for (int y = 0; y < birthElements.length; y++) { + String elem = birthElements[y]; + int value = Integer.parseInt(elem); + birthRulesArray[y] = value; + } + this.birthRulesArrays[x] = birthRulesArray; + System.out.println("birth rule taken into account"); } + //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; - } + String[] surviveElements = surviveCells[x].split(","); + int[] surviveArray = new int[surviveElements.length]; + for (int y = 0; y < surviveElements.length; y++) { + String elem = surviveElements[y]; + int value = Integer.parseInt(elem); + surviveArray[y] = value; + } + this.surviveRulesArrays[x] = surviveArray; + System.out.println("survival rule taken into account"); + }*/ //determines the number of alive neighboring cells needed to survive, and places them in the surviveCells list } - } + public int[] getBirthRulesArray() { - return this.birthRulesArray; + return this.birthRulesArrays; } public int[] getSurvivalRulesArray() { - return this.survivalRulesArray; + return this.surviveRulesArrays; }