From 81e4c7ed594ace2b9f159d5a0bdbfb0b74dafe1b Mon Sep 17 00:00:00 2001 From: yoyom Date: Tue, 7 May 2024 17:02:16 +0200 Subject: [PATCH] all page 3 methods are updated --- src/backend/Grid.java | 10 ++++++---- src/backend/Simulator.java | 31 ++++++++++++++++--------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/backend/Grid.java b/src/backend/Grid.java index 645dc38..b309d6e 100644 --- a/src/backend/Grid.java +++ b/src/backend/Grid.java @@ -1,10 +1,12 @@ package backend; public class Grid { - private int[][] grid; + public static int[][] grid; + int width=Simulator.COL_NUM; + int height=Simulator.LINE_NUM; - public Grid(int width, int height) { - grid = new int[width][height]; - } + public Grid() { + grid = new int[Simulator.COL_NUM][Simulator.LINE_NUM]; + } } diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index f16d65b..9b0de52 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; @@ -7,8 +8,8 @@ public class Simulator extends Thread { private MyInterface mjf; - private final int COL_NUM = 100; - private final int LINE_NUM = 100; + public static final int COL_NUM = 100; + public static final int LINE_NUM = 100; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -24,8 +25,6 @@ public class Simulator extends Thread { private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - - private int[][] grid; //TODO : add missing attribute(s) @@ -148,7 +147,7 @@ public class Simulator extends Thread { agents.add(newAgent); } else { - grid[x][y] = (grid[x][y] == 0) ? 1 : 0; // 0=dead and 1=alive + Grid.grid[x][y] = (Grid.grid[x][y] == 0) ? 1 : 0; // 0=dead and 1=alive } } @@ -160,7 +159,7 @@ public class Simulator extends Thread { */ public int getCell(int x, int y) { if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) { - return grid[x][y]; + return Grid.grid[x][y]; } else { System.out.println("Error: Coordinates out of the grid."); @@ -200,7 +199,7 @@ public class Simulator extends Thread { */ public void setCell(int x, int y, int val) { if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) { - grid[x][y] = val; + Grid.grid[x][y] = val; } else { System.out.println("Error: Coordinates out of the grid."); @@ -257,14 +256,16 @@ public class Simulator extends Thread { * to be alive in new state */ public void generateRandom(float chanceOfLife) { - //TODO : complete method - /* - * Advice : - * as you should probably have a separate class - * representing the field of cells... - * maybe just make a constructor in there - * and use it here - */ + Random random = new Random(); + for (int x = 0; x < COL_NUM; x++) { + for (int y = 0; y < LINE_NUM; y++) { + if (random.nextFloat() < chanceOfLife) { + setCell(x, y, 1); + } else { + setCell(x, y, 0); + } + } + } } public boolean isLoopingBorder() {