Merge remote-tracking branch 'origin/dev_bnb'

This commit is contained in:
Guillaume BONABAU 2024-05-08 09:55:04 +02:00
commit 19c9688615
2 changed files with 60 additions and 43 deletions

View File

@ -74,6 +74,7 @@ public class Simulator extends Thread {
//Should probably stay as is
public void run() {
int stepCount=0;
System.out.println("Step Count: "+ stepCount);
while(!stopFlag) {
stepCount++;
makeStep();
@ -117,26 +118,10 @@ public class Simulator extends Thread {
}
//then evolution of the field
// TODO-INPROGRESS : apply game rule to all cells of the field
Table tempTable = new Table(this.height, this.width, this);
for(int x=0; x<width; x++) {
for(int y=0; y<height; y++) {
if (table.getCell(x, y).getValue()==1) {
if (table.countNear(x,y)<2) {
tempTable.getCell(x,y).setValue(0);
} else if(table.countNear(x,y)>3) {
tempTable.getCell(x,y).setValue(0);
}
} else {
if(table.countNear(x,y)==3) {
tempTable.getCell(x,y).setValue(1);
}
}
this.applyRule();
}
}
this.table = tempTable;
}
/* you should distribute this action in methods/classes
* don't write everything here !
@ -263,7 +248,6 @@ public class Simulator extends Thread {
public void setCell(int x, int y, int val) {
//TODO : complete method
this.table.getCell(x, y).setValue(val);
// set cell value to !currentCellValue
}
public void countAround(int x, int y) {
@ -395,26 +379,57 @@ public class Simulator extends Thread {
System.out.println("empty rule file");
return;
}
//TODO : remove previous rule (=emptying lists)
//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 : add value to possible survive values
//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 : add value to possible birth values
//TODO-INPROGRESS : add value to possible birth values
fieldBirthValues.add(value);
}
}
public void applyRule(){
Table tempTable = new Table(this.height, this.width, this);
for(int x=0; x<width; x++) {
for(int y=0; y<height; y++) {
if (this.getCell(x,y)==1) {
if (this.fieldSurviveValues.contains(this.table.countNear(x, y))) {
this.setCell(x, y, 1);
} else {
this.setCell(x, y, 0);
}
}
else if(this.getCell(x,y)==0) {
if (this.fieldBirthValues.contains(this.table.countNear(x, y))) {
this.setCell(x, y, 1);
} else {
this.setCell(x, y, 0);
}
}
System.out.println("applying rule to cell: "+x+", "+y);
}
}
this.table = tempTable;
}
public ArrayList<String> getAgentsSave() {
//TODO : Same idea as the other save method, but for agents

View File

@ -56,25 +56,27 @@ public class Table {
this.table.get(row).set(column, cell);
}
//TODO-complete : count near (xy) -> return how many cells around this cell
//TODO-INPROGRESS : count near (xy) -> return how many cells around this cell
// NEED A STRONG OPTIMISATION
public int countNear(int row, int column){
int cellCount =0;
// if border is true
for (int i = row-1;i<=row+1;i++){
for (int j = column-1;j<=column+1;column++){
if (!(i == j)){
if (i<0 || i>=width || j<0 || j>=height){
//TODO what to do when out of bound (wall alive or dead/ looping border) (wall dead by default)
} else {
cellCount += this.getCell(i,j).getValue();
}
int count = 0;
// 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];
// Check if the new indices are within the table boundaries
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
count+= this.getCell(x, y).getValue();
}
}
}
return cellCount;
//if border is false
return count;
}
//TODO : set agent (x y agent) load an agent to coordinates x,y