WORKING but getRule ??
This commit is contained in:
parent
cd00a26119
commit
2e594ef409
|
|
@ -87,53 +87,64 @@ public class Simulator extends Thread {
|
|||
* makes all the actions to go from one step to the other
|
||||
*/
|
||||
public void makeStep() {
|
||||
// agent behaviors first
|
||||
// only modify if sure of what you do
|
||||
// to modify agent behavior, see liveTurn method
|
||||
// in agent classes
|
||||
for(Agent agent : agents) {
|
||||
ArrayList<Agent> neighbors =
|
||||
this.getNeighboringAnimals(
|
||||
agent.getX(),
|
||||
agent.getY(),
|
||||
ANIMAL_AREA_RADIUS);
|
||||
if(!agent.liveTurn(neighbors,this)) {
|
||||
agents.remove(agent);
|
||||
// Create a new matrix for the next state of the cells
|
||||
int[][] newCells = Cells.initializeMatrix(LINE_NUM, COL_NUM, 0);
|
||||
int nbNeighborAlive;
|
||||
|
||||
// Iterate through each cell in the grid
|
||||
for (int j = 0; j < LINE_NUM; j++) {
|
||||
for (int i = 0; i < COL_NUM; i++) {
|
||||
nbNeighborAlive = 0; // Reset the counter for each cell
|
||||
|
||||
// Check the surrounding cells
|
||||
for (int a = -1; a <= 1; a++) {
|
||||
for (int b = -1; b <= 1; b++) {
|
||||
if (a == 0 && b == 0) {
|
||||
continue; // Skip the current cell itself
|
||||
}
|
||||
|
||||
int neighborX = i + a;
|
||||
int neighborY = j + b;
|
||||
|
||||
if (loopingBorder) {
|
||||
// Wrap around the indices using modulo operation
|
||||
neighborX = (neighborX + COL_NUM) % COL_NUM;
|
||||
neighborY = (neighborY + LINE_NUM) % LINE_NUM;
|
||||
} else {
|
||||
// Ensure the indices are within bounds
|
||||
if (neighborX < 0 || neighborY < 0 || neighborX >= COL_NUM || neighborY >= LINE_NUM) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
int[][] newCells = Cells.initializeMatrix(100, 100, 0); //matrix of the future states of the cells to not disturb the counter
|
||||
int nbNeighborAlive = 0; //counter for alive cells
|
||||
for (int j=0; j< LINE_NUM;j++) { // test for each cells
|
||||
for (int i=0; i< COL_NUM;i++) {
|
||||
for (int a=-1; a<2;a++) { // test the surrounding cells
|
||||
for(int b = -1 ; b<2; b++) {
|
||||
if (i+a >0 && j+b >0 && i+a <100 && j+b <100) {
|
||||
if(cells[i+a][j+b] == 1) {
|
||||
nbNeighborAlive += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int aliveNextIteration = 0;
|
||||
if (cells[i][j] == 1) {
|
||||
nbNeighborAlive +=-1; //removes the actual cell that got counted
|
||||
for(int k =0; k<fieldSurviveValues.size();k++ ) {
|
||||
if (nbNeighborAlive == fieldSurviveValues.get(k)) {
|
||||
aliveNextIteration = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cells[i][j] == 0) {
|
||||
for(int k =0; k<fieldBirthValues.size();k++ ) {
|
||||
if (nbNeighborAlive == fieldBirthValues.get(k)) {
|
||||
aliveNextIteration = 1;
|
||||
|
||||
if (cells[neighborY][neighborX] == 1) {
|
||||
nbNeighborAlive++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newCells[i][j] = aliveNextIteration;
|
||||
// Initialize aliveNextIteration to 0
|
||||
int aliveNextIteration = 0;
|
||||
|
||||
// Apply survival rules
|
||||
if (cells[j][i] == 1) {
|
||||
// Check if the number of live neighbors is in the survive list
|
||||
if (fieldSurviveValues.contains(nbNeighborAlive)) {
|
||||
aliveNextIteration = 1;
|
||||
}
|
||||
} else {
|
||||
// Apply birth rules
|
||||
if (fieldBirthValues.contains(nbNeighborAlive)) {
|
||||
aliveNextIteration = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Set the new state of the cell
|
||||
newCells[j][i] = aliveNextIteration;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the current state of the cells with the new state
|
||||
cells = newCells;
|
||||
}
|
||||
//then evolution of the field
|
||||
|
|
@ -340,9 +351,7 @@ public class Simulator extends Thread {
|
|||
*/
|
||||
public ArrayList<String> getRule() {
|
||||
///FaLSE ArrayList<String> lines = new ArrayList<String>();
|
||||
fieldSurviveValues.add(csv.read)
|
||||
fieldSurviveValues = csv.read
|
||||
return lines
|
||||
|
||||
}
|
||||
public void loadRule(ArrayList<String> lines) {
|
||||
if (lines.size() <= 0) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue