From c4239812643d53fd78b039257a8bfdd2a2c3719b Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Thu, 16 May 2024 11:55:54 +0200 Subject: [PATCH] conway working, gas flickering --- conwayRule.json | 6 +- gasRule.json | 32 ++++---- src/backend/Simulator.java | 109 ++++++++++++++++----------- src/backend/Table.java | 32 ++++++++ src/windowInterface/MyInterface.java | 22 +----- 5 files changed, 120 insertions(+), 81 deletions(-) diff --git a/conwayRule.json b/conwayRule.json index 2c5ce56..7f1a76e 100644 --- a/conwayRule.json +++ b/conwayRule.json @@ -2,15 +2,15 @@ "value" : 1, "color" : [255,255,255], "conditionCountNear" : [2,3], - "conditionHighestNear" : null, + "conditionHighestNear" : [], "ifValue" : 1, "elseValue" : 0 }}, {"cell": { "value" : 0, - "color" : [0,0,0], + "color" : [102,0,0], "conditionCountNear" : [3], - "conditionHighestNear" : null, + "conditionHighestNear" : [], "ifValue" : 1, "elseValue" : 0 }} diff --git a/gasRule.json b/gasRule.json index 70ba527..9c4290b 100644 --- a/gasRule.json +++ b/gasRule.json @@ -1,47 +1,47 @@ [{"cell": { "value" : 5, "color" : [255,255,255], - "conditionCountNear" : null, - "conditionHighestNear" : null, + "conditionCountNear" : [], + "conditionHighestNear" : [], "ifValue" : 4, - "elseValue" : null + "elseValue" : 4 }}, {"cell": { "value" : 4, "color" : [204,204,204], - "conditionCountNear" : null, - "conditionHighestNear" : null, + "conditionCountNear" : [], + "conditionHighestNear" : [], "ifValue" : 3, - "elseValue" : null + "elseValue" : 3 }}, {"cell": { "value" : 3, "color" : [153,153,153], - "conditionCountNear" : null, - "conditionHighestNear" : null, + "conditionCountNear" : [], + "conditionHighestNear" : [], "ifValue" : 2, - "elseValue" : null + "elseValue" : 2 }}, {"cell": { "value" : 2, "color" : [102,102,102], - "conditionCountNear" : null, - "conditionHighestNear" : null, + "conditionCountNear" : [], + "conditionHighestNear" : [], "ifValue" : 1, - "elseValue" : null + "elseValue" : 1 }}, {"cell": { "value" : 1, "color" : [51,51,51], - "conditionCountNear" : null, - "conditionHighestNear" : null, + "conditionCountNear" : [], + "conditionHighestNear" : [], "ifValue" : 0, - "elseValue" : null + "elseValue" : 0 }}, {"cell": { "value" : 0, "color" : [0,0,0], - "conditionCountNear" : null, + "conditionCountNear" : [], "conditionHighestNear" : [5], "ifValue" : 5, "elseValue" : 0 diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index d521d05..21d1d15 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,10 +1,16 @@ package backend; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; +//import for json import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import windowInterface.MyInterface; @@ -12,8 +18,8 @@ public class Simulator extends Thread { private MyInterface mjf; - private final int COL_NUM = 100; - private final int LINE_NUM = 100; + private final int COL_NUM = 10; + private final int LINE_NUM = 10; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -60,15 +66,12 @@ public class Simulator extends Thread { this.height=LINE_NUM; enableLogs = true; // for debugging purposes table = new Table(height, width, this); - cellDensityToggle=false; + cellDensityToggle=true; //Default rule : Survive always, birth never - - for(int i =0; i<9; i++) { - fieldSurviveValues.add(i); - } + loadRule("OOP_F1_Project\\conwayRule.json"); } @@ -181,20 +184,10 @@ public class Simulator extends Thread { int currentCellValue = getCell(x, y); int newCellValue = 0; if(cellDensityToggle) { - if (currentCellValue == -1) { - newCellValue = 0; - } - if (currentCellValue == 0) { - newCellValue = 1; - } - if (currentCellValue == 1) { - newCellValue = 2; - } - if (currentCellValue == 2) { - newCellValue = 3; - } - if (currentCellValue == 3) { - newCellValue = -1; + if (currentCellValue <6) { + newCellValue = currentCellValue +1; + } else { + newCellValue=-1; } } else { if (currentCellValue == 0) { @@ -386,10 +379,28 @@ public class Simulator extends Thread { } @SuppressWarnings("unchecked") - public void loadRule(JSONArray cellList) { - ruleArrayList.clear(); - colorArrayList.clear(); - cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) ); + 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 ) ); + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } } @SuppressWarnings("unchecked") @@ -408,21 +419,21 @@ public class Simulator extends Thread { colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value)))); //Get Condition Count Near - JSONArray countNearJsonArray = (JSONArray) cellObject.get("color"); + 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("color"); + 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("value")); + String ifValueString = String.valueOf((Long)cellObject.get("ifValue")); int ifValue = Integer.valueOf(ifValueString); //Get elseValue - String elseValueString = String.valueOf((Long)cellObject.get("value")); + String elseValueString = String.valueOf((Long)cellObject.get("elseValue")); int elseValue = Integer.valueOf(elseValueString); while (cellValue > colorArrayList.size()) { @@ -442,28 +453,42 @@ public class Simulator extends Thread { Table tempTable = new Table(this.height, this.width, this); for(int x=0; x