From fed97404edb3609b4f0ebb84072feea9d5d0f8be Mon Sep 17 00:00:00 2001 From: "matheo.estor" Date: Tue, 14 May 2024 16:38:20 +0200 Subject: [PATCH] methods added --- src/backend/Simulator.java | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index eb672b9..2a9de5b 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -1,5 +1,6 @@ package backend; import java.util.ArrayList; +import java.util.Random; import windowInterface.MyInterface; @@ -24,7 +25,7 @@ public class Simulator extends Thread { private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - + public int[][] grid; //TODO : add missing attribute(s) public Simulator(MyInterface mjfParam) { @@ -33,7 +34,7 @@ public class Simulator extends Thread { pauseFlag=false; loopingBorder=false; clickActionFlag=false; - + grid = new int[COL_NUM][LINE_NUM]; agents = new ArrayList(); fieldBirthValues = new ArrayList(); fieldSurviveValues = new ArrayList(); @@ -157,7 +158,10 @@ public class Simulator extends Thread { * @return value of cell */ public int getCell(int x, int y) { - + if (x < 0 || x >= COL_NUM || y < 0 || y >= LINE_NUM) { + return 0; + } + return grid[x][y]; } /** * @@ -191,7 +195,9 @@ public class Simulator extends Thread { * @param val to set in cell */ public void setCell(int x, int y, int val) { - + if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) { + grid[x][y] = val; + } } /** @@ -243,7 +249,18 @@ public class Simulator extends Thread { * to be alive in new state */ public void generateRandom(float chanceOfLife) { - + Random rand = new Random(); + for (int x = 0; x < COL_NUM; x++) { + for (int y = 0; y < LINE_NUM; y++) { + // Generate a random float between 0 and 1 + float randVal = rand.nextFloat(); + if (randVal < chanceOfLife) { + setCell(x, y, 1); + } else { + setCell(x, y, 0); + } + } + } } public boolean isLoopingBorder() { @@ -317,6 +334,7 @@ public class Simulator extends Thread { */ public String clickActionName() { return clickActionFlag ? "Sheep" : "Cell"; + } }