From 5663bc5030c3bec930235f1f86af80f75442f7e5 Mon Sep 17 00:00:00 2001 From: timeo Date: Sun, 26 May 2024 14:10:31 +0200 Subject: [PATCH] made it work, got the world that appears and can place life still have a pb on the rules because when i create a glider it does like 3 gen and becomes a block --- src/backend/Simulator.java | 96 ++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 6b3c441..cb7bac4 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -25,6 +25,7 @@ public class Simulator extends Thread { private boolean clickActionFlag; private int loopDelay = 150; private int[][] grid; + private int[][] world; //TODO : add missing attribute(s) @@ -38,9 +39,10 @@ public class Simulator extends Thread { agents = new ArrayList(); fieldBirthValues = new ArrayList(); fieldSurviveValues = new ArrayList(); + world =new int[getWidth()][getHeight()]; //TODO : add missing attribute initialization - grid = gridCreation(); + //Default rule : Survive always, birth never for(int i =0; i<9; i++) { @@ -48,35 +50,6 @@ public class Simulator extends Thread { } } - public int[][] gridCreation() { - int[][] newGrid = new int[COL_NUM][LINE_NUM]; - //create the grid - for (int x = 0; x < COL_NUM; x ++) { - for (int y = 0; y < LINE_NUM; y ++) { - if (grid[x][y] == 0) - System.out.print("0"); - else - System.out.print("1"); - //create cell - //cest fait jpeux pas enlever le todo - } - System.out.println(); - } - return newGrid; - } - - - - //TODO create a new generation - public int[][] nextGen(int gridCreation[][], int COL_NUM, int LINE_NUM){ - int[][] future = new int[COL_NUM][LINE_NUM]; - for (int x = 0; x < COL_NUM; x++) { - for (int y = 0; y < LINE_NUM; y++) { - - } - } - } - public int getWidth() { return COL_NUM; @@ -113,11 +86,53 @@ public class Simulator extends Thread { * method called at each step of the simulation * makes all the actions to go from one step to the other */ + + + + private int countAliveNeighbors(int x, int y) { + int count = 0; + int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; + for (int[] dir : directions) { + int nx = x + dir[0]; + int ny = y + dir[1]; + if (nx >= 0 && nx < getWidth() && ny >= 0 && ny < getHeight()) { + count += world[nx][ny]; + } else if (loopingBorder) { + nx = (nx + getWidth()) % getWidth(); + ny = (ny + getHeight()) % getHeight(); + count += world[nx][ny]; + } + } + return count; + } + + + public void makeStep() { // agent behaviors first // only modify if sure of what you do // to modify agent behavior, see liveTurn method // in agent classes + + + int[][] newWorld = new int[getWidth()][getHeight()]; + + // Apply Game of Life rules + for (int x = 0; x < getWidth(); x++) { + for (int y = 0; y < getHeight(); y++) { + int aliveNeighbors = countAliveNeighbors(x, y); + if (world[x][y] == 1) { + newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1; + } else { + newWorld[x][y] = (aliveNeighbors == 3) ? 1 : 0; + } + } + } + + world = newWorld; + + + for(Agent agent : agents) { ArrayList neighbors = this.getNeighboringAnimals( @@ -147,7 +162,18 @@ public class Simulator extends Thread { * then the cell becomes alive */ - + + + for (int x = 0; x < getWidth(); x++) { + for (int y = 0; y < getHeight(); y++) { + int aliveNeighbors = countAliveNeighbors(x, y); + if (world[x][y] == 1) { + newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1; + } else { + newWorld[x][y] = (aliveNeighbors == 3) ? 1 : 0; + } + } + } } @@ -171,7 +197,8 @@ 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 + setCell(x, y, getCell(x, y) == 1 ? 0 : 1); + } /** @@ -181,8 +208,8 @@ public class Simulator extends Thread { * @return value of cell */ public int getCell(int x, int y) { + return world[x][y]; //get the value (dead or alive) of my cell at x y - return 0; } /** * @@ -216,7 +243,8 @@ public class Simulator extends Thread { * @param val to set in cell */ public void setCell(int x, int y, int val) { - //TODO : complete method + world [x][y] = val; + } /**