methods added

This commit is contained in:
matheo.estor 2024-05-14 16:38:20 +02:00
parent e6d5262178
commit fed97404ed
1 changed files with 23 additions and 5 deletions

View File

@ -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<Agent>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
@ -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";
}
}