From 263ffa8a79d586584df9814860489f33e2d6562f 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 | 105 +++++++++++++++++---------- src/backend/Table.java | 32 ++++++++ src/windowInterface/MyInterface.java | 22 +----- 5 files changed, 118 insertions(+), 79 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 7864704..5111b2d 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,11 +1,17 @@ package backend; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; import java.awt.Color; 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; @@ -59,15 +65,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"); } @@ -180,20 +183,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) { @@ -450,10 +443,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") @@ -472,21 +483,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()) { @@ -506,28 +517,42 @@ public class Simulator extends Thread { Table tempTable = new Table(this.height, this.width, this); for(int x=0; x