diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index e1ae068..40a4f17 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -18,8 +18,8 @@ public class Simulator extends Thread { private MyInterface mjf; - private final int COL_NUM = 10; - private final int LINE_NUM = 10; + private final int COL_NUM = 100; + private final int LINE_NUM = 100; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -382,120 +382,115 @@ public class Simulator extends Thread { public void loadRule(String fileName) { System.out.println(fileName); //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; - ruleArrayList.clear(); - colorArrayList.clear(); - cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) ); + JSONParser jsonParser = new JSONParser(); + try (FileReader reader = new FileReader(fileName)) + { + //Read JSON file + Object obj = jsonParser.parse(reader); + + JSONArray cellList = (JSONArray) obj; + ruleArrayList.clear(); + colorArrayList.clear(); + cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) ); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); - } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + //DEBUG + //printRules(ruleArrayList); } @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("conditionCountNear"); - ArrayList conditionCountNearList = new ArrayList(); - countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value)))); - - //Get Condition highest near - JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("conditionHighestNear"); - ArrayList conditionHighestNearList = new ArrayList(); - conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(Integer.valueOf(String.valueOf((Long)value)))); - - //Get ifValue - String ifValueString = String.valueOf((Long)cellObject.get("ifValue")); - int ifValue = Integer.valueOf(ifValueString); - - //Get elseValue - String elseValueString = String.valueOf((Long)cellObject.get("elseValue")); - int elseValue = Integer.valueOf(elseValueString); - - while (cellValue > colorArrayList.size()) { - colorArrayList.add(new ArrayList()); + // Get cell object within list + JSONObject cellObject = (JSONObject) cell.get("cell"); + + // Get value + int cellValue = ((Long) cellObject.get("value")).intValue(); + 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(((Long) value).intValue())); + + // Get Condition Count Near + JSONArray countNearJsonArray = (JSONArray) cellObject.get("conditionCountNear"); + ArrayList conditionCountNearList = new ArrayList<>(); + countNearJsonArray.forEach(value -> conditionCountNearList.add(((Long) value).intValue())); + + // Get Condition Highest Near + JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("conditionHighestNear"); + ArrayList conditionHighestNearList = new ArrayList<>(); + conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(((Long) value).intValue())); + + // Get ifValue + int ifValue = ((Long) cellObject.get("ifValue")).intValue(); + + // Get elseValue + int elseValue = ((Long) cellObject.get("elseValue")).intValue(); + + // Ensure the colorArrayList is large enough + while (colorArrayList.size() <= cellValue) { + 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); + colorArrayList.set(cellValue, rgbList); + + // Ensure the ruleArrayList is large enough + while (ruleArrayList.size() <= cellValue) { + ruleArrayList.add(null); } - ruleArrayList.add(cellValue,newRule); - - } + ruleArrayList.set(cellValue, new Rule(cellValue, rgbList, conditionCountNearList, conditionHighestNearList, ifValue, elseValue)); + } public void applyRule(){ Table tempTable = new Table(this.height, this.width, this); - for(int x=0; x= width || y < 0 || y >= height) { - // Border cell is dead + // Border cell is outside the grid continue; } } - if (highest < this.getCell(x, y).getValue()) { - highest = this.getCell(x, y).getValue(); + + int cellValue = this.getCell(x, y).getValue(); + if (cellValue > highest) { + highest = cellValue; } }