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