Fixed a few issues withe rules

This commit is contained in:
Isaac Enzo MOUANG TACHI 2024-06-01 14:53:34 +02:00
parent d8ea583889
commit e285ad96e3
1 changed files with 50 additions and 33 deletions

View File

@ -1,7 +1,7 @@
package backend;
import java.util.ArrayList;
import java.util.List;
//Hey there
import windowInterface.MyInterface;
public class Simulator extends Thread {
@ -23,37 +23,46 @@ public class Simulator extends Thread {
private boolean stopFlag;
private boolean pauseFlag;
private boolean loopingBorder;
private String clickActionFlag;
private boolean clickActionFlag;
private int loopDelay = 150;
private Grid grid;
//TODO : add missing attribute(s)
private final boolean CELL_MOD = true;
private final boolean MAIN_MOD = false;
private Grid grid;
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag=false;
pauseFlag=false;
loopingBorder=false;
clickActionFlag="CellMode";
clickActionFlag=CELL_MOD;
agents = new ArrayList<Agent>();
fieldSurviveValues = new ArrayList<>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
fieldSurviveValues.clear();
fieldBirthValues.clear();
fieldBirthValues.add(3);
fieldSurviveValues.add(2);
fieldSurviveValues.add(3);
fieldBirthValues = new ArrayList<>();
fieldBirthValues.add(3);
grid = new Grid(LINE_NUM, COL_NUM);
//There's an issue with fieldSurviveValues : at a moment, when 1 neighbor, it counts the cell as a survivor
grid = new Grid(LINE_NUM, COL_NUM);
//TODO : add missing attribute initialization
}
public int getWidth() {
//TODO : replace with proper return
return LINE_NUM;
}
public int getHeight() {
//TODO : replace with proper return
return COL_NUM;
}
@ -119,13 +128,13 @@ public class Simulator extends Thread {
*/
Grid nextGrid = new Grid(LINE_NUM, COL_NUM);
// Iterate over each cell in the grid
for (int i = 0; i < LINE_NUM; i++) {
for (int j = 0; j < COL_NUM; j++) {
int liveNeighbors = countLiveNeighbors(i, j);
if (getCell(i, j) == 1) {
if (grid.getValueCell(i, j) == 1) {
if (fieldSurviveValues.contains(liveNeighbors)) {
nextGrid.setValueCell(i, j, 1);
} else {
@ -142,7 +151,7 @@ public class Simulator extends Thread {
}
grid = nextGrid;
}
/*
@ -166,9 +175,11 @@ public class Simulator extends Thread {
if (neighborY < 0) neighborY = COL_NUM - 1;
else if (neighborY >= COL_NUM) neighborY = 0;
}
if (neighborX >= 0 && neighborX < LINE_NUM && neighborY >= 0 && neighborY < COL_NUM) {
if (getCell(neighborX, neighborY) == 1) {
if (grid.getValueCell(neighborX, neighborY) == 1) {
liveNeighbors++;
}
}
@ -177,8 +188,6 @@ public class Simulator extends Thread {
return liveNeighbors;
}
/*
* leave this as is
@ -191,12 +200,8 @@ public class Simulator extends Thread {
* method called when clicking pause button
*/
public void togglePause() {
if (pauseFlag == true ) {
pauseFlag=false;
}
else {
pauseFlag=true;
}
// TODO : actually toggle the corresponding flag
pauseFlag=!pauseFlag;
}
/**
@ -204,7 +209,7 @@ public class Simulator extends Thread {
*/
public void clickCell(int x, int y) {
//TODO : complete method
if (clickActionFlag=="CellMode") {
if (clickActionFlag==CELL_MOD) {
if (grid.getValueCell(x, y)==1) {
this.setCell(x, y, 0);
}
@ -221,6 +226,7 @@ public class Simulator extends Thread {
* @return value of cell
*/
public int getCell(int x, int y) {
//TODO : complete method with proper return
return grid.getValueCell(x, y);
}
/**
@ -255,6 +261,7 @@ public class Simulator extends Thread {
* @param val to set in cell
*/
public void setCell(int x, int y, int val) {
//TODO : complete method
grid.setValueCell(x, y, val);
}
@ -264,6 +271,7 @@ public class Simulator extends Thread {
* the simulated world in its present state
*/
public ArrayList<String> getSaveState() {
//TODO : complete method with proper return
ArrayList<String> saveState = new ArrayList<>();
for (int i = 0; i < LINE_NUM; i++) {
@ -319,7 +327,15 @@ public class Simulator extends Thread {
* to be alive in new state
*/
public void generateRandom(float chanceOfLife) {
for (int i = 0; i < LINE_NUM; i++) {
//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
*/
for (int i = 0; i < LINE_NUM; i++) {
for (int j = 0; j < COL_NUM; j++) {
float randomValue = (float) Math.random();
if (randomValue <= chanceOfLife) {
@ -332,25 +348,24 @@ public class Simulator extends Thread {
}
public boolean isLoopingBorder() {
//TODO : complete method with proper return
return loopingBorder;
}
public void toggleLoopingBorder() {
if (isLoopingBorder() == true) {
loopingBorder=false;
}else {
loopingBorder=true;
}
//TODO : complete method
loopingBorder =!loopingBorder;
}
public void setLoopDelay(int delay) {
//TODO : complete method
loopDelay=delay;
}
public void toggleClickAction() {
//TODO : complete method
if (clickActionFlag=="CellMode") {
if (clickActionFlag==CELL_MOD) {
}
}
@ -378,7 +393,7 @@ public class Simulator extends Thread {
System.out.println("empty rule file");
return;
}
//TODO : remove previous rule (=emptying lists)
fieldSurviveValues.clear();
fieldBirthValues.clear();
@ -388,6 +403,7 @@ public class Simulator extends Thread {
for(int x=0; x<surviveElements.length;x++) {
String elem = surviveElements[x];
int value = Integer.parseInt(elem);
//TODO : add value to possible survive values
fieldSurviveValues.add(value);
}
@ -395,6 +411,7 @@ public class Simulator extends Thread {
for(int x=0; x<birthElements.length;x++) {
String elem = birthElements[x];
int value = Integer.parseInt(elem);
//TODO : add value to possible birth values
fieldBirthValues.add(value);
}
@ -419,7 +436,7 @@ public class Simulator extends Thread {
// depending on clickActionFlag
String action = "Cell";
if (clickActionFlag == "CellMode") {
if (clickActionFlag == CELL_MOD) {
action = "Cell";
}