json import
This commit is contained in:
parent
5e462cfd39
commit
204fd32ba7
|
|
@ -17,5 +17,7 @@ With the implied additional rules:
|
||||||
5. Any dead cell who doesn’t have exactly 3 living neighbors stays dead,
|
5. Any dead cell who doesn’t have exactly 3 living neighbors stays dead,
|
||||||
unchanged.
|
unchanged.
|
||||||
|
|
||||||
|
To download the json library:
|
||||||
TEST
|
https://code.google.com/archive/p/json-simple/downloads
|
||||||
|
tutorial to install it on vscode:
|
||||||
|
https://www.youtube.com/watch?v=g6vvEEm2hhs
|
||||||
|
|
@ -1,16 +1,17 @@
|
||||||
[{
|
[{"cell": {
|
||||||
"value" : 1,
|
"value" : 1,
|
||||||
"color" : [255,255,255],
|
"color" : [255,255,255],
|
||||||
"conditionCountNear" : [2,3],
|
"conditionCountNear" : [2,3],
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 1,
|
"ifValue" : 1,
|
||||||
"elseValue" : 0
|
"elseValue" : 0
|
||||||
},
|
}},
|
||||||
{
|
{"cell": {
|
||||||
"value" : 0,
|
"value" : 0,
|
||||||
"color" : [0,0,0],
|
"color" : [0,0,0],
|
||||||
"conditionCountNear" : [3],
|
"conditionCountNear" : [3],
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 1,
|
"ifValue" : 1,
|
||||||
"elseValue" : 0
|
"elseValue" : 0
|
||||||
}]
|
}}
|
||||||
|
]
|
||||||
24
gasRule.json
24
gasRule.json
|
|
@ -1,48 +1,48 @@
|
||||||
[{
|
[{"cell": {
|
||||||
"value" : 5,
|
"value" : 5,
|
||||||
"color" : [255,255,255],
|
"color" : [255,255,255],
|
||||||
"conditionCountNear" : null,
|
"conditionCountNear" : null,
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 4,
|
"ifValue" : 4,
|
||||||
"elseValue" : null
|
"elseValue" : null
|
||||||
},
|
}},
|
||||||
{
|
{"cell": {
|
||||||
"value" : 4,
|
"value" : 4,
|
||||||
"color" : [204,204,204],
|
"color" : [204,204,204],
|
||||||
"conditionCountNear" : null,
|
"conditionCountNear" : null,
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 3,
|
"ifValue" : 3,
|
||||||
"elseValue" : null
|
"elseValue" : null
|
||||||
},
|
}},
|
||||||
{
|
{"cell": {
|
||||||
"value" : 3,
|
"value" : 3,
|
||||||
"color" : [153,153,153],
|
"color" : [153,153,153],
|
||||||
"conditionCountNear" : null,
|
"conditionCountNear" : null,
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 2,
|
"ifValue" : 2,
|
||||||
"elseValue" : null
|
"elseValue" : null
|
||||||
},
|
}},
|
||||||
{
|
{"cell": {
|
||||||
"value" : 2,
|
"value" : 2,
|
||||||
"color" : [102,102,102],
|
"color" : [102,102,102],
|
||||||
"conditionCountNear" : null,
|
"conditionCountNear" : null,
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 1,
|
"ifValue" : 1,
|
||||||
"elseValue" : null
|
"elseValue" : null
|
||||||
},
|
}},
|
||||||
{
|
{"cell": {
|
||||||
"value" : 1,
|
"value" : 1,
|
||||||
"color" : [51,51,51],
|
"color" : [51,51,51],
|
||||||
"conditionCountNear" : null,
|
"conditionCountNear" : null,
|
||||||
"conditionHighestNear" : null,
|
"conditionHighestNear" : null,
|
||||||
"ifValue" : 0,
|
"ifValue" : 0,
|
||||||
"elseValue" : null
|
"elseValue" : null
|
||||||
},
|
}},
|
||||||
{
|
{"cell": {
|
||||||
"value" : 0,
|
"value" : 0,
|
||||||
"color" : [0,0,0],
|
"color" : [0,0,0],
|
||||||
"conditionCountNear" : null,
|
"conditionCountNear" : null,
|
||||||
"conditionHighestNear" : [5],
|
"conditionHighestNear" : [5],
|
||||||
"ifValue" : 5,
|
"ifValue" : 5,
|
||||||
"elseValue" : 0
|
"elseValue" : 0
|
||||||
}]
|
}}]
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class rules {
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Rule {
|
||||||
|
|
||||||
private int value;
|
private int value;
|
||||||
private int[] color;
|
private ArrayList<Integer> color;
|
||||||
private int[] conditionCountNear;
|
private ArrayList<Integer> conditionCountNear;
|
||||||
private int[] conditionHighestNear;
|
private ArrayList<Integer> conditionHighestNear;
|
||||||
private int ifValue;
|
private int ifValue;
|
||||||
private int elseValue;
|
private int elseValue;
|
||||||
|
|
||||||
public rules(int value , int[] color, int[] conditionCountNear, int[] conditionHighestNear, int ifValue, int elseValue) {
|
public Rule(int value , ArrayList<Integer> color, ArrayList<Integer> conditionCountNear, ArrayList<Integer> conditionHighestNear, int ifValue, int elseValue) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.color = color;
|
this.color = color;
|
||||||
this.conditionCountNear = conditionCountNear;
|
this.conditionCountNear = conditionCountNear;
|
||||||
|
|
@ -22,27 +24,27 @@ public class rules {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] getColor() {
|
public ArrayList<Integer> getColor() {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColor(int[] color) {
|
public void setColor(ArrayList<Integer> color) {
|
||||||
this.color = color;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] getConditionCountNear() {
|
public ArrayList<Integer> getConditionCountNear() {
|
||||||
return conditionCountNear;
|
return conditionCountNear;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConditionCountNear(int[] conditionCountNear) {
|
public void setConditionCountNear(ArrayList<Integer> conditionCountNear) {
|
||||||
this.conditionCountNear = conditionCountNear;
|
this.conditionCountNear = conditionCountNear;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] getConditionHighestNear() {
|
public ArrayList<Integer> getConditionHighestNear() {
|
||||||
return conditionHighestNear;
|
return conditionHighestNear;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConditionHighestNear(int[] conditionHighestNear) {
|
public void setConditionHighestNear(ArrayList<Integer> conditionHighestNear) {
|
||||||
this.conditionHighestNear = conditionHighestNear;
|
this.conditionHighestNear = conditionHighestNear;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,6 +1,11 @@
|
||||||
package backend;
|
package backend;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.util.ArrayList;
|
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;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
|
|
@ -32,6 +37,11 @@ public class Simulator extends Thread {
|
||||||
private Table table;
|
private Table table;
|
||||||
private boolean cellDensityToggle;
|
private boolean cellDensityToggle;
|
||||||
|
|
||||||
|
//Rules Arraylists
|
||||||
|
private ArrayList<Rule> ruleArrayList = new ArrayList<Rule>();
|
||||||
|
private ArrayList<ArrayList<Integer>> colorArrayList = new ArrayList<ArrayList<Integer>>();
|
||||||
|
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
mjf = mjfParam;
|
mjf = mjfParam;
|
||||||
stopFlag=false;
|
stopFlag=false;
|
||||||
|
|
@ -54,6 +64,7 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
|
|
||||||
//Default rule : Survive always, birth never
|
//Default rule : Survive always, birth never
|
||||||
|
|
||||||
for(int i =0; i<9; i++) {
|
for(int i =0; i<9; i++) {
|
||||||
fieldSurviveValues.add(i);
|
fieldSurviveValues.add(i);
|
||||||
}
|
}
|
||||||
|
|
@ -438,36 +449,59 @@ public class Simulator extends Thread {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
@SuppressWarnings("unchecked")
|
||||||
if(lines.size()<=0) {
|
public void loadRule(JSONArray cellList) {
|
||||||
System.out.println("empty rule file");
|
ruleArrayList.clear();
|
||||||
return;
|
colorArrayList.clear();
|
||||||
}
|
cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) );
|
||||||
//TODO-INPROGRESS : remove previous rule (=emptying lists)
|
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
|
||||||
|
|
||||||
String surviveLine = lines.get(0);
|
|
||||||
String birthLine = lines.get(1);
|
|
||||||
|
|
||||||
String[] surviveElements = surviveLine.split(";");
|
|
||||||
for(int x=0; x<surviveElements.length;x++) {
|
|
||||||
String elem = surviveElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
|
||||||
//TODO-INPROGRESS : add value to possible survive values
|
|
||||||
fieldSurviveValues.add(value);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] birthElements = birthLine.split(";");
|
|
||||||
for(int x=0; x<birthElements.length;x++) {
|
|
||||||
String elem = birthElements[x];
|
|
||||||
int value = Integer.parseInt(elem);
|
|
||||||
//TODO-INPROGRESS : add value to possible birth values
|
|
||||||
fieldBirthValues.add(value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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<Integer> rgbList = new ArrayList<Integer>();
|
||||||
|
colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value))));
|
||||||
|
|
||||||
|
//Get Condition Count Near
|
||||||
|
JSONArray countNearJsonArray = (JSONArray) cellObject.get("color");
|
||||||
|
ArrayList<Integer> conditionCountNearList = new ArrayList<Integer>();
|
||||||
|
countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value))));
|
||||||
|
|
||||||
|
//Get Condition highest near
|
||||||
|
JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("color");
|
||||||
|
ArrayList<Integer> conditionHighestNearList = new ArrayList<Integer>();
|
||||||
|
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<Integer>());
|
||||||
|
}
|
||||||
|
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(){
|
public void applyRule(){
|
||||||
Table tempTable = new Table(this.height, this.width, this);
|
Table tempTable = new Table(this.height, this.width, this);
|
||||||
for(int x=0; x<width; x++) {
|
for(int x=0; x<width; x++) {
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,22 @@ import javax.swing.JLabel;
|
||||||
|
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
|
//added imports for loading jsons
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.json.simple.parser.*;
|
||||||
|
|
||||||
|
|
||||||
public class MyInterface extends JFrame {
|
public class MyInterface extends JFrame {
|
||||||
|
|
||||||
private static final long serialVersionUID = -6840815447618468846L;
|
private static final long serialVersionUID = -6840815447618468846L;
|
||||||
|
|
@ -300,17 +310,31 @@ public class MyInterface extends JFrame {
|
||||||
ArrayList<String> stringArray = new ArrayList<String>();
|
ArrayList<String> stringArray = new ArrayList<String>();
|
||||||
if (fileName.length()>0) {
|
if (fileName.length()>0) {
|
||||||
try {
|
try {
|
||||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
//TODO-INPROGRESS load json
|
||||||
String line = fileContent.readLine();
|
JSONParser jsonParser = new JSONParser();
|
||||||
while (line != null) {
|
try (FileReader reader = new FileReader(fileName))
|
||||||
stringArray.add(line);
|
{
|
||||||
line = fileContent.readLine();
|
//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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
mySimu.loadRule(stringArray);
|
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue