methods added
This commit is contained in:
parent
e6d5262178
commit
fed97404ed
|
|
@ -1,5 +1,6 @@
|
||||||
package backend;
|
package backend;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
|
|
@ -24,7 +25,7 @@ public class Simulator extends Thread {
|
||||||
private boolean loopingBorder;
|
private boolean loopingBorder;
|
||||||
private boolean clickActionFlag;
|
private boolean clickActionFlag;
|
||||||
private int loopDelay = 150;
|
private int loopDelay = 150;
|
||||||
|
public int[][] grid;
|
||||||
//TODO : add missing attribute(s)
|
//TODO : add missing attribute(s)
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
|
|
@ -33,7 +34,7 @@ public class Simulator extends Thread {
|
||||||
pauseFlag=false;
|
pauseFlag=false;
|
||||||
loopingBorder=false;
|
loopingBorder=false;
|
||||||
clickActionFlag=false;
|
clickActionFlag=false;
|
||||||
|
grid = new int[COL_NUM][LINE_NUM];
|
||||||
agents = new ArrayList<Agent>();
|
agents = new ArrayList<Agent>();
|
||||||
fieldBirthValues = new ArrayList<Integer>();
|
fieldBirthValues = new ArrayList<Integer>();
|
||||||
fieldSurviveValues = new ArrayList<Integer>();
|
fieldSurviveValues = new ArrayList<Integer>();
|
||||||
|
|
@ -157,7 +158,10 @@ public class Simulator extends Thread {
|
||||||
* @return value of cell
|
* @return value of cell
|
||||||
*/
|
*/
|
||||||
public int getCell(int x, int y) {
|
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
|
* @param val to set in cell
|
||||||
*/
|
*/
|
||||||
public void setCell(int x, int y, int val) {
|
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
|
* to be alive in new state
|
||||||
*/
|
*/
|
||||||
public void generateRandom(float chanceOfLife) {
|
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() {
|
public boolean isLoopingBorder() {
|
||||||
|
|
@ -317,6 +334,7 @@ public class Simulator extends Thread {
|
||||||
*/
|
*/
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
return clickActionFlag ? "Sheep" : "Cell";
|
return clickActionFlag ? "Sheep" : "Cell";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue