all page 3 methods are updated

This commit is contained in:
yoyom 2024-05-07 17:02:16 +02:00
parent fffd3b6e52
commit 81e4c7ed59
2 changed files with 22 additions and 19 deletions

View File

@ -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];
}
}

View File

@ -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() {