gas rules works

This commit is contained in:
Guillaume BONABAU 2024-05-22 09:51:59 +02:00
parent 0e00903a86
commit 5aa4513148
2 changed files with 126 additions and 112 deletions

View File

@ -18,8 +18,8 @@ public class Simulator extends Thread {
private MyInterface mjf; private MyInterface mjf;
private final int COL_NUM = 10; private final int COL_NUM = 100;
private final int LINE_NUM = 10; private final int LINE_NUM = 100;
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;
@ -382,120 +382,115 @@ public class Simulator extends Thread {
public void loadRule(String fileName) { public void loadRule(String fileName) {
System.out.println(fileName); System.out.println(fileName);
//TODO-INPROGRESS load json //TODO-INPROGRESS load json
JSONParser jsonParser = new JSONParser(); JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader(fileName)) try (FileReader reader = new FileReader(fileName))
{ {
//Read JSON file //Read JSON file
Object obj = jsonParser.parse(reader); Object obj = jsonParser.parse(reader);
JSONArray cellList = (JSONArray) obj; JSONArray cellList = (JSONArray) obj;
ruleArrayList.clear(); ruleArrayList.clear();
colorArrayList.clear(); colorArrayList.clear();
cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) ); cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) );
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
//DEBUG
//printRules(ruleArrayList);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void parseCellObject(JSONObject cell) { private void parseCellObject(JSONObject cell) {
//Get cell object within list // Get cell object within list
JSONObject cellObject = (JSONObject) cell.get("cell"); JSONObject cellObject = (JSONObject) cell.get("cell");
//Get value // Get value
String cellValueString = String.valueOf((Long)cellObject.get("value")); int cellValue = ((Long) cellObject.get("value")).intValue();
int cellValue = Integer.valueOf(cellValueString); System.out.println("cell value rule loaded: " + cellValue);
System.out.println("cell value rule loaded: "+cellValue);
// Get color
//Get color JSONArray colorValueJsonArray = (JSONArray) cellObject.get("color");
JSONArray colorValueJsonArray = (JSONArray) cellObject.get("color"); ArrayList<Integer> rgbList = new ArrayList<>();
ArrayList<Integer> rgbList = new ArrayList<Integer>(); colorValueJsonArray.forEach(value -> rgbList.add(((Long) value).intValue()));
colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value))));
// Get Condition Count Near
//Get Condition Count Near JSONArray countNearJsonArray = (JSONArray) cellObject.get("conditionCountNear");
JSONArray countNearJsonArray = (JSONArray) cellObject.get("conditionCountNear"); ArrayList<Integer> conditionCountNearList = new ArrayList<>();
ArrayList<Integer> conditionCountNearList = new ArrayList<Integer>(); countNearJsonArray.forEach(value -> conditionCountNearList.add(((Long) value).intValue()));
countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value))));
// Get Condition Highest Near
//Get Condition highest near JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("conditionHighestNear");
JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("conditionHighestNear"); ArrayList<Integer> conditionHighestNearList = new ArrayList<>();
ArrayList<Integer> conditionHighestNearList = new ArrayList<Integer>(); conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(((Long) value).intValue()));
conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(Integer.valueOf(String.valueOf((Long)value))));
// Get ifValue
//Get ifValue int ifValue = ((Long) cellObject.get("ifValue")).intValue();
String ifValueString = String.valueOf((Long)cellObject.get("ifValue"));
int ifValue = Integer.valueOf(ifValueString); // Get elseValue
int elseValue = ((Long) cellObject.get("elseValue")).intValue();
//Get elseValue
String elseValueString = String.valueOf((Long)cellObject.get("elseValue")); // Ensure the colorArrayList is large enough
int elseValue = Integer.valueOf(elseValueString); while (colorArrayList.size() <= cellValue) {
colorArrayList.add(new ArrayList<>());
while (cellValue > colorArrayList.size()) {
colorArrayList.add(new ArrayList<Integer>());
} }
colorArrayList.add(cellValue,rgbList); colorArrayList.set(cellValue, rgbList);
Rule newRule = new Rule(cellValue, rgbList, conditionCountNearList, conditionHighestNearList, ifValue, elseValue);
// Ensure the ruleArrayList is large enough
while (cellValue > ruleArrayList.size()) { while (ruleArrayList.size() <= cellValue) {
ruleArrayList.add(newRule); ruleArrayList.add(null);
} }
ruleArrayList.add(cellValue,newRule); ruleArrayList.set(cellValue, new Rule(cellValue, rgbList, conditionCountNearList, conditionHighestNearList, ifValue, elseValue));
}
}
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++) {
for(int y=0; y<height; y++) { for(int y = 0; y < height; y++) {
int valueCountNear = table.countNear(x, y); int valueCountNear = table.countNear(x, y);
int valueHighestNear = table.highestNear(x,y); int valueHighestNear = table.highestNear(x, y);
if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() ==0 && int currentValue = table.getCell(x, y).getValue();
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() ==0) { Rule currentRule = ruleArrayList.get(currentValue);
//both conditions lists are empty, directly take if value
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getIfValue()); if (currentRule.getConditionCountNear().isEmpty() && currentRule.getConditionHighestNear().isEmpty()) {
// Both condition lists are empty, directly take if value
tempTable.getCell(x, y).setValue(currentRule.getIfValue());
} else if (!currentRule.getConditionCountNear().isEmpty() && currentRule.getConditionHighestNear().isEmpty()) {
// Only countNear condition
if (currentRule.getConditionCountNear().contains(valueCountNear)) {
tempTable.getCell(x, y).setValue(currentRule.getIfValue());
} else {
tempTable.getCell(x, y).setValue(currentRule.getElseValue());
}
} else if (currentRule.getConditionCountNear().isEmpty() && !currentRule.getConditionHighestNear().isEmpty()) {
// Only highestNear condition
if (currentRule.getConditionHighestNear().contains(valueHighestNear)) {
tempTable.getCell(x, y).setValue(currentRule.getIfValue());
} else {
tempTable.getCell(x, y).setValue(currentRule.getElseValue());
}
} else if (!currentRule.getConditionCountNear().isEmpty() && !currentRule.getConditionHighestNear().isEmpty()) {
// Both conditions
if (currentRule.getConditionHighestNear().contains(valueHighestNear)
&& currentRule.getConditionCountNear().contains(valueCountNear)) {
tempTable.getCell(x, y).setValue(currentRule.getIfValue());
} else {
tempTable.getCell(x, y).setValue(currentRule.getElseValue());
}
}
// DEBUG:
} else if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() !=0 && //System.out.println("Applying rule to cell: " + x + ", " + y +" | countNear = " + valueCountNear +" | highestNear = " + valueHighestNear +" | current cell value = " + currentValue +" | new cell value = " + tempTable.getCell(x, y).getValue());
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() == 0) { }
//only countnear condition }
if (ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().contains(valueCountNear)){ this.table = tempTable;
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getIfValue()); //DEBUG
}else{ //table.serialPrint();
tempTable.getCell(x, y).setValue(ruleArrayList.get(table.getCell(x, y).getValue()).getElseValue());
}
} else if ( ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().size() ==0 &&
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() != 0) {
//only highest near condition
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().size() !=0 &&
ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().size() != 0) {
//both conditions
if (ruleArrayList.get(table.getCell(x, y).getValue()).getConditionHighestNear().contains(valueHighestNear)
&& ruleArrayList.get(table.getCell(x, y).getValue()).getConditionCountNear().contains(valueCountNear)){
}
}
//DEBUG:
//System.out.println("applying rule to cell: "+x+", "+y + " | countnear = " + valueCountNear + " | new cell value = " + tempTable.getCell(x,y).getValue());
}
}
this.table = tempTable;
table.serialPrint();
} }
@ -524,4 +519,21 @@ public class Simulator extends Thread {
} }
} }
public void printRules(ArrayList<Rule> ruleArrayList) {
System.out.println("-----------------------------------");
System.out.println("Rule list size: "+ruleArrayList.size());
System.out.println("-----------------------------------");
for (Rule rule : ruleArrayList) {
System.out.println("Rule for value: " + rule.getValue());
System.out.println("Color: " + rule.getColor());
System.out.println("Condition Count Near: " + rule.getConditionCountNear());
System.out.println("Condition Highest Near: " + rule.getConditionHighestNear());
System.out.println("If Value: " + rule.getIfValue());
System.out.println("Else Value: " + rule.getElseValue());
System.out.println("-----------------------------------");
}
}
} }

View File

@ -89,16 +89,17 @@ public class Table {
return count; return count;
} }
public int highestNear(int row, int column){ public int highestNear(int row, int column) {
int highest = 0; int highest = Integer.MIN_VALUE; // Initialize to the minimum integer value
boolean loopingBorder = isLoopingBorder(); boolean loopingBorder = isLoopingBorder();
// Define the relative positions of neighboring cells (assuming 8 neighbors) // Define the relative positions of neighboring cells (assuming 8 neighbors)
int[][] neighbors = { int[][] neighbors = {
{-1, -1}, {-1, 0}, {-1, 1}, {-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1}, {0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1} {1, -1}, {1, 0}, {1, 1}
}; };
for (int[] neighbor : neighbors) { for (int[] neighbor : neighbors) {
int x = row + neighbor[0]; int x = row + neighbor[0];
int y = column + neighbor[1]; int y = column + neighbor[1];
@ -106,15 +107,16 @@ public class Table {
if (loopingBorder) { if (loopingBorder) {
x = (x + width) % width; x = (x + width) % width;
y = (y + height) % height; y = (y + height) % height;
} } else {
else {
if (x < 0 || x >= width || y < 0 || y >= height) { if (x < 0 || x >= width || y < 0 || y >= height) {
// Border cell is dead // Border cell is outside the grid
continue; continue;
} }
} }
if (highest < this.getCell(x, y).getValue()) {
highest = this.getCell(x, y).getValue(); int cellValue = this.getCell(x, y).getValue();
if (cellValue > highest) {
highest = cellValue;
} }
} }