conway working, gas flickering

This commit is contained in:
Guillaume BONABAU 2024-05-16 11:55:54 +02:00
parent 30155a1648
commit c423981264
5 changed files with 120 additions and 81 deletions

View File

@ -2,15 +2,15 @@
"value" : 1, "value" : 1,
"color" : [255,255,255], "color" : [255,255,255],
"conditionCountNear" : [2,3], "conditionCountNear" : [2,3],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 1, "ifValue" : 1,
"elseValue" : 0 "elseValue" : 0
}}, }},
{"cell": { {"cell": {
"value" : 0, "value" : 0,
"color" : [0,0,0], "color" : [102,0,0],
"conditionCountNear" : [3], "conditionCountNear" : [3],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 1, "ifValue" : 1,
"elseValue" : 0 "elseValue" : 0
}} }}

View File

@ -1,47 +1,47 @@
[{"cell": { [{"cell": {
"value" : 5, "value" : 5,
"color" : [255,255,255], "color" : [255,255,255],
"conditionCountNear" : null, "conditionCountNear" : [],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 4, "ifValue" : 4,
"elseValue" : null "elseValue" : 4
}}, }},
{"cell": { {"cell": {
"value" : 4, "value" : 4,
"color" : [204,204,204], "color" : [204,204,204],
"conditionCountNear" : null, "conditionCountNear" : [],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 3, "ifValue" : 3,
"elseValue" : null "elseValue" : 3
}}, }},
{"cell": { {"cell": {
"value" : 3, "value" : 3,
"color" : [153,153,153], "color" : [153,153,153],
"conditionCountNear" : null, "conditionCountNear" : [],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 2, "ifValue" : 2,
"elseValue" : null "elseValue" : 2
}}, }},
{"cell": { {"cell": {
"value" : 2, "value" : 2,
"color" : [102,102,102], "color" : [102,102,102],
"conditionCountNear" : null, "conditionCountNear" : [],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 1, "ifValue" : 1,
"elseValue" : null "elseValue" : 1
}}, }},
{"cell": { {"cell": {
"value" : 1, "value" : 1,
"color" : [51,51,51], "color" : [51,51,51],
"conditionCountNear" : null, "conditionCountNear" : [],
"conditionHighestNear" : null, "conditionHighestNear" : [],
"ifValue" : 0, "ifValue" : 0,
"elseValue" : null "elseValue" : 0
}}, }},
{"cell": { {"cell": {
"value" : 0, "value" : 0,
"color" : [0,0,0], "color" : [0,0,0],
"conditionCountNear" : null, "conditionCountNear" : [],
"conditionHighestNear" : [5], "conditionHighestNear" : [5],
"ifValue" : 5, "ifValue" : 5,
"elseValue" : 0 "elseValue" : 0

View File

@ -1,10 +1,16 @@
package backend; package backend;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
//import for json
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import windowInterface.MyInterface; import windowInterface.MyInterface;
@ -12,8 +18,8 @@ public class Simulator extends Thread {
private MyInterface mjf; private MyInterface mjf;
private final int COL_NUM = 100; private final int COL_NUM = 10;
private final int LINE_NUM = 100; private final int LINE_NUM = 10;
private final int LIFE_TYPE_NUM = 4; private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1 //Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1; private final int LIFE_AREA_RADIUS = 1;
@ -60,15 +66,12 @@ public class Simulator extends Thread {
this.height=LINE_NUM; this.height=LINE_NUM;
enableLogs = true; // for debugging purposes enableLogs = true; // for debugging purposes
table = new Table(height, width, this); table = new Table(height, width, this);
cellDensityToggle=false; cellDensityToggle=true;
//Default rule : Survive always, birth never //Default rule : Survive always, birth never
loadRule("OOP_F1_Project\\conwayRule.json");
for(int i =0; i<9; i++) {
fieldSurviveValues.add(i);
}
} }
@ -181,20 +184,10 @@ public class Simulator extends Thread {
int currentCellValue = getCell(x, y); int currentCellValue = getCell(x, y);
int newCellValue = 0; int newCellValue = 0;
if(cellDensityToggle) { if(cellDensityToggle) {
if (currentCellValue == -1) { if (currentCellValue <6) {
newCellValue = 0; newCellValue = currentCellValue +1;
} } else {
if (currentCellValue == 0) { newCellValue=-1;
newCellValue = 1;
}
if (currentCellValue == 1) {
newCellValue = 2;
}
if (currentCellValue == 2) {
newCellValue = 3;
}
if (currentCellValue == 3) {
newCellValue = -1;
} }
} else { } else {
if (currentCellValue == 0) { if (currentCellValue == 0) {
@ -386,10 +379,28 @@ public class Simulator extends Thread {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void loadRule(JSONArray cellList) { public void loadRule(String fileName) {
ruleArrayList.clear(); System.out.println(fileName);
colorArrayList.clear(); //TODO-INPROGRESS load json
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();
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -408,21 +419,21 @@ public class Simulator extends Thread {
colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value)))); colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value))));
//Get Condition Count Near //Get Condition Count Near
JSONArray countNearJsonArray = (JSONArray) cellObject.get("color"); JSONArray countNearJsonArray = (JSONArray) cellObject.get("conditionCountNear");
ArrayList<Integer> conditionCountNearList = new ArrayList<Integer>(); ArrayList<Integer> conditionCountNearList = new ArrayList<Integer>();
countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value)))); countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value))));
//Get Condition highest near //Get Condition highest near
JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("color"); JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("conditionHighestNear");
ArrayList<Integer> conditionHighestNearList = new ArrayList<Integer>(); ArrayList<Integer> conditionHighestNearList = new ArrayList<Integer>();
conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(Integer.valueOf(String.valueOf((Long)value)))); conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(Integer.valueOf(String.valueOf((Long)value))));
//Get ifValue //Get ifValue
String ifValueString = String.valueOf((Long)cellObject.get("value")); String ifValueString = String.valueOf((Long)cellObject.get("ifValue"));
int ifValue = Integer.valueOf(ifValueString); int ifValue = Integer.valueOf(ifValueString);
//Get elseValue //Get elseValue
String elseValueString = String.valueOf((Long)cellObject.get("value")); String elseValueString = String.valueOf((Long)cellObject.get("elseValue"));
int elseValue = Integer.valueOf(elseValueString); int elseValue = Integer.valueOf(elseValueString);
while (cellValue > colorArrayList.size()) { while (cellValue > colorArrayList.size()) {
@ -442,28 +453,42 @@ public class Simulator extends Thread {
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++) {
for(int y=0; y<height; y++) { for(int y=0; y<height; y++) {
int resultCountNear = this.table.countNear(x, y); int valueCountNear = table.countNear(x, y);
if (this.getCell(x,y)==1) { int valueHighestNear = table.highestNear(x,y);
if (this.fieldSurviveValues.contains(resultCountNear)) { if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() ==0 &&
tempTable.getCell(x, y).setValue(1); ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() ==0) {
} else { tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getIfValue());
tempTable.getCell(x, y).setValue(0);
} else if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() !=0 &&
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() == 0) {
if (ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().contains(valueCountNear)){
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getIfValue());
}else{
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getElseValue());
} }
}
else if(this.getCell(x,y)==0) { } else if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() ==0 &&
if (this.fieldBirthValues.contains(resultCountNear)) { ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() != 0) {
tempTable.getCell(x, y).setValue(1); if (ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().contains(valueHighestNear)){
} else { tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getIfValue());
tempTable.getCell(x, y).setValue(0); }else{
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getElseValue());
} }
} else if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear() !=null &&
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear() != null) {
} }
//DEBUG: //DEBUG:
//System.out.println("applying rule to cell: "+x+", "+y + " | countnear = " + resultCountNear + " | new cell value = " + this.getCell(x, y)); //System.out.println("applying rule to cell: "+x+", "+y + " | countnear = " + valueCountNear + " | new cell value = " + tempTable.getCell(x,y).getValue());
} }
} }
this.table = tempTable; this.table = tempTable;
table.serialPrint();
} }

View File

@ -88,6 +88,38 @@ public class Table {
return count; return count;
} }
public int highestNear(int row, int column){
int highest = 0;
boolean loopingBorder = isLoopingBorder();
// Define the relative positions of neighboring cells (assuming 8 neighbors)
int[][] neighbors = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (int[] neighbor : neighbors) {
int x = row + neighbor[0];
int y = column + neighbor[1];
if (loopingBorder) {
x = (x + width) % width;
y = (y + height) % height;
}
else {
if (x < 0 || x >= width || y < 0 || y >= height) {
// Border cell is dead
continue;
}
}
if (highest < this.getCell(x, y).getValue()) {
highest = this.getCell(x, y).getValue();
}
}
return highest;
}
//TODO : set agent (x y agent) load an agent to coordinates x,y //TODO : set agent (x y agent) load an agent to coordinates x,y

View File

@ -310,26 +310,8 @@ 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 {
//TODO-INPROGRESS load json mySimu.loadRule(fileName);
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();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();