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,
"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
}}

View File

@ -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

View File

@ -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<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");
JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("conditionHighestNear");
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"));
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<width; x++) {
for(int y=0; y<height; y++) {
int resultCountNear = this.table.countNear(x, y);
if (this.getCell(x,y)==1) {
if (this.fieldSurviveValues.contains(resultCountNear)) {
tempTable.getCell(x, y).setValue(1);
} else {
tempTable.getCell(x, y).setValue(0);
int valueCountNear = table.countNear(x, y);
int valueHighestNear = table.highestNear(x,y);
if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() ==0 &&
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() ==0) {
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getIfValue());
} 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) {
if (this.fieldBirthValues.contains(resultCountNear)) {
tempTable.getCell(x, y).setValue(1);
} else {
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()).getConditionHighestNear().contains(valueHighestNear)){
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 ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear() !=null &&
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear() != null) {
}
//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;
table.serialPrint();
}

View File

@ -88,6 +88,38 @@ public class Table {
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

View File

@ -310,26 +310,8 @@ public class MyInterface extends JFrame {
ArrayList<String> stringArray = new ArrayList<String>();
if (fileName.length()>0) {
try {
//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;
//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();
}
mySimu.loadRule(fileName);
} catch (Exception e) {
e.printStackTrace();