From 5766fa1d0086f445ce868f94bf1eb65ce568775d Mon Sep 17 00:00:00 2001 From: titou Date: Mon, 27 May 2024 17:27:29 +0200 Subject: [PATCH] makeStep --- src/backend/Simulator.java | 73 +++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 1b01aec..eb72514 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -95,20 +95,15 @@ public class Simulator extends Thread { // only modify if sure of what you do // to modify agent behavior, see liveTurn method // in agent classes - for(Agent agent : agents) { - ArrayList neighbors = - this.getNeighboringAnimals( - agent.getX(), - agent.getY(), - ANIMAL_AREA_RADIUS); - if(!agent.liveTurn( - neighbors, - this)) { + for (int i = 0; i < agents.size(); i++) { + Agent agent = agents.get(i); + ArrayList neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); + if (!agent.liveTurn(neighbors, this)) { agents.remove(agent); + i--; } } //then evolution of the field - // TODO : apply game rule to all cells of the field /* you should distribute this action in methods/classes * don't write everything here ! @@ -123,12 +118,57 @@ public class Simulator extends Thread { * and the count is in the birth list, * then the cell becomes alive */ - - - - + int[][] newField = new int[LINE_NUM][COL_NUM]; + + for (int y = 0; y < LINE_NUM; y++) { + for (int x = 0; x < COL_NUM; x++) { + int aliveNeighbors = countAliveNeighbors(x, y); + if (field[y][x] == 1) { + if (fieldSurviveValues.contains(aliveNeighbors)) { + newField[y][x] = 1; + } else { + newField[y][x] = 0; + } + } else { + if (fieldBirthValues.contains(aliveNeighbors)) { + newField[y][x] = 1; + } else { + newField[y][x] = 0; + } + } + } + } + field = newField; } + private int countAliveNeighbors(int x, int y) { + int count = 0; + for (int i = -LIFE_AREA_RADIUS; i <= LIFE_AREA_RADIUS; i++) { + for (int j = -LIFE_AREA_RADIUS; j <= LIFE_AREA_RADIUS; j++) { + if (i == 0 && j == 0) { + continue; + } + int neighborX = x + i; + int neighborY = y + j; + + if (loopingBorder) { + neighborX = (neighborX + COL_NUM) % COL_NUM; + neighborY = (neighborY + LINE_NUM) % LINE_NUM; + } else { + if (neighborX < 0 || neighborY < 0 || neighborX >= COL_NUM || neighborY >= LINE_NUM) { + continue; + } + } + + if (field[neighborY][neighborX] == 1) { + count++; + } + } + } + return count; + } + + /* * leave this as is */ @@ -148,7 +188,6 @@ public class Simulator extends Thread { * method called when clicking on a cell in the interface */ public void clickCell(int x, int y) { - //TODO : complete method if (clickActionFlag) { for (Agent agent : agents) { if(agent.getX() == x && agent.getY() == y) { @@ -162,6 +201,7 @@ public class Simulator extends Thread { field[y][x] = (field[y][x] == 0) ? 1 : 0; } } + //DONE /** * get cell value in simulated world @@ -215,7 +255,6 @@ public class Simulator extends Thread { * the simulated world in its present state */ public ArrayList getSaveState() { - //TODO : complete method with proper return ArrayList saveState = new ArrayList<>(); for (int y = 0; y