diff --git a/README.md b/README.md index 6c3fd2e..6f81b49 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,7 @@ With the implied additional rules: 5. Any dead cell who doesn’t have exactly 3 living neighbors stays dead, unchanged. - -TEST \ No newline at end of file +To download the json library: +https://code.google.com/archive/p/json-simple/downloads +tutorial to install it on vscode: +https://www.youtube.com/watch?v=g6vvEEm2hhs \ No newline at end of file diff --git a/conwayRule.json b/conwayRule.json index 34bb7a3..2c5ce56 100644 --- a/conwayRule.json +++ b/conwayRule.json @@ -1,16 +1,17 @@ -[{ +[{"cell": { "value" : 1, "color" : [255,255,255], "conditionCountNear" : [2,3], "conditionHighestNear" : null, "ifValue" : 1, "elseValue" : 0 -}, -{ +}}, +{"cell": { "value" : 0, "color" : [0,0,0], "conditionCountNear" : [3], "conditionHighestNear" : null, "ifValue" : 1, "elseValue" : 0 -}] \ No newline at end of file +}} +] \ No newline at end of file diff --git a/gasRule.json b/gasRule.json index 58b3c5a..70ba527 100644 --- a/gasRule.json +++ b/gasRule.json @@ -1,48 +1,48 @@ -[{ +[{"cell": { "value" : 5, "color" : [255,255,255], "conditionCountNear" : null, "conditionHighestNear" : null, "ifValue" : 4, "elseValue" : null -}, -{ +}}, +{"cell": { "value" : 4, "color" : [204,204,204], "conditionCountNear" : null, "conditionHighestNear" : null, "ifValue" : 3, "elseValue" : null -}, -{ +}}, +{"cell": { "value" : 3, "color" : [153,153,153], "conditionCountNear" : null, "conditionHighestNear" : null, "ifValue" : 2, "elseValue" : null -}, -{ +}}, +{"cell": { "value" : 2, "color" : [102,102,102], "conditionCountNear" : null, "conditionHighestNear" : null, "ifValue" : 1, "elseValue" : null -}, -{ +}}, +{"cell": { "value" : 1, "color" : [51,51,51], "conditionCountNear" : null, "conditionHighestNear" : null, "ifValue" : 0, "elseValue" : null -}, -{ +}}, +{"cell": { "value" : 0, "color" : [0,0,0], "conditionCountNear" : null, "conditionHighestNear" : [5], "ifValue" : 5, "elseValue" : 0 -}] \ No newline at end of file +}}] \ No newline at end of file diff --git a/src/backend/rules.java b/src/backend/Rule.java similarity index 56% rename from src/backend/rules.java rename to src/backend/Rule.java index cbe5c94..1b6bf12 100644 --- a/src/backend/rules.java +++ b/src/backend/Rule.java @@ -1,15 +1,17 @@ package backend; -public class rules { +import java.util.ArrayList; + +public class Rule { private int value; - private int[] color; - private int[] conditionCountNear; - private int[] conditionHighestNear; + private ArrayList color; + private ArrayList conditionCountNear; + private ArrayList conditionHighestNear; private int ifValue; private int elseValue; - public rules(int value , int[] color, int[] conditionCountNear, int[] conditionHighestNear, int ifValue, int elseValue) { + public Rule(int value , ArrayList color, ArrayList conditionCountNear, ArrayList conditionHighestNear, int ifValue, int elseValue) { this.value = value; this.color = color; this.conditionCountNear = conditionCountNear; @@ -22,27 +24,27 @@ public class rules { return value; } - public int[] getColor() { + public ArrayList getColor() { return color; } - public void setColor(int[] color) { + public void setColor(ArrayList color) { this.color = color; } - public int[] getConditionCountNear() { + public ArrayList getConditionCountNear() { return conditionCountNear; } - public void setConditionCountNear(int[] conditionCountNear) { + public void setConditionCountNear(ArrayList conditionCountNear) { this.conditionCountNear = conditionCountNear; } - public int[] getConditionHighestNear() { + public ArrayList getConditionHighestNear() { return conditionHighestNear; } - public void setConditionHighestNear(int[] conditionHighestNear) { + public void setConditionHighestNear(ArrayList conditionHighestNear) { this.conditionHighestNear = conditionHighestNear; } diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 7b4ff54..d521d05 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,5 +1,10 @@ package backend; import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import windowInterface.MyInterface; @@ -33,6 +38,11 @@ public class Simulator extends Thread { private Table table; private boolean cellDensityToggle; + //Rules Arraylists + private ArrayList ruleArrayList = new ArrayList(); + private ArrayList> colorArrayList = new ArrayList>(); + + public Simulator(MyInterface mjfParam) { mjf = mjfParam; stopFlag=false; @@ -55,6 +65,7 @@ public class Simulator extends Thread { //Default rule : Survive always, birth never + for(int i =0; i<9; i++) { fieldSurviveValues.add(i); } @@ -374,36 +385,59 @@ public class Simulator extends Thread { return null; } - public void loadRule(ArrayList lines) { - if(lines.size()<=0) { - System.out.println("empty rule file"); - return; - } - //TODO-INPROGRESS : remove previous rule (=emptying lists) - fieldSurviveValues = new ArrayList(); - fieldBirthValues = new ArrayList(); - - String surviveLine = lines.get(0); - String birthLine = lines.get(1); - - String[] surviveElements = surviveLine.split(";"); - for(int x=0; x parseCellObject( (JSONObject) cell ) ); } + @SuppressWarnings("unchecked") + private void parseCellObject(JSONObject cell) { + //Get cell object within list + JSONObject cellObject = (JSONObject) cell.get("cell"); + + //Get value + String cellValueString = String.valueOf((Long)cellObject.get("value")); + int cellValue = Integer.valueOf(cellValueString); + System.out.println("cell value rule loaded: "+cellValue); + + //Get color + JSONArray colorValueJsonArray = (JSONArray) cellObject.get("color"); + ArrayList rgbList = new ArrayList(); + colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value)))); + + //Get Condition Count Near + JSONArray countNearJsonArray = (JSONArray) cellObject.get("color"); + ArrayList conditionCountNearList = new ArrayList(); + countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value)))); + + //Get Condition highest near + JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("color"); + ArrayList conditionHighestNearList = new ArrayList(); + conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(Integer.valueOf(String.valueOf((Long)value)))); + + //Get ifValue + String ifValueString = String.valueOf((Long)cellObject.get("value")); + int ifValue = Integer.valueOf(ifValueString); + + //Get elseValue + String elseValueString = String.valueOf((Long)cellObject.get("value")); + int elseValue = Integer.valueOf(elseValueString); + + while (cellValue > colorArrayList.size()) { + colorArrayList.add(new ArrayList()); + } + colorArrayList.add(cellValue,rgbList); + Rule newRule = new Rule(cellValue, rgbList, conditionCountNearList, conditionHighestNearList, ifValue, elseValue); + + while (cellValue > ruleArrayList.size()) { + ruleArrayList.add(newRule); + } + ruleArrayList.add(cellValue,newRule); + + } + public void applyRule(){ Table tempTable = new Table(this.height, this.width, this); for(int x=0; x stringArray = new ArrayList(); if (fileName.length()>0) { try { - BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); - String line = fileContent.readLine(); - while (line != null) { - stringArray.add(line); - line = fileContent.readLine(); + //TODO-INPROGRESS load json + JSONParser jsonParser = new JSONParser(); + try (FileReader reader = new FileReader(fileName)) + { + //Read JSON file + Object obj = jsonParser.parse(reader); + + JSONArray cellList = (JSONArray) obj; + //System.out.println(cellList); + mySimu.loadRule(cellList); + //Iterate over employee array + //cellList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) ); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); } - fileContent.close(); + } catch (Exception e) { e.printStackTrace(); } - mySimu.loadRule(stringArray); + this.repaint(); } }