Compare commits

...

2 Commits

Author SHA1 Message Date
matheo.estor fed97404ed methods added 2024-05-14 16:38:20 +02:00
matheo.estor e6d5262178 Part 2: the Mandatory methods 2024-05-14 16:25:01 +02:00
1 changed files with 36 additions and 25 deletions

View File

@ -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>();
@ -50,13 +51,11 @@ public class Simulator extends Thread {
} }
public int getWidth() { public int getWidth() {
//TODO : replace with proper return return COL_NUM;
return 0;
} }
public int getHeight() { public int getHeight() {
//TODO : replace with proper return return LINE_NUM;
return 0;
} }
//Should probably stay as is //Should probably stay as is
@ -136,14 +135,20 @@ public class Simulator extends Thread {
* method called when clicking pause button * method called when clicking pause button
*/ */
public void togglePause() { public void togglePause() {
// TODO : actually toggle the corresponding flag pauseFlag = !pauseFlag;
} }
/** /**
* method called when clicking on a cell in the interface * method called when clicking on a cell in the interface
*/ */
public void clickCell(int x, int y) { public void clickCell(int x, int y) {
//TODO : complete method if (clickActionFlag) {
agents.add(new Sheep(x, y));
} else {
int currentValue = getCell(x, y);
int newValue = (currentValue == 0) ? 1 : 0;
setCell(x, y, newValue);}
} }
/** /**
@ -153,8 +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) {
//TODO : complete method with proper return if (x < 0 || x >= COL_NUM || y < 0 || y >= LINE_NUM) {
return 0; return 0;
}
return grid[x][y];
} }
/** /**
* *
@ -188,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) {
//TODO : complete method if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) {
grid[x][y] = val;
}
} }
/** /**
@ -197,7 +206,6 @@ 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
return null; return null;
} }
/** /**
@ -241,14 +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) {
//TODO : complete method Random rand = new Random();
/* for (int x = 0; x < COL_NUM; x++) {
* Advice : for (int y = 0; y < LINE_NUM; y++) {
* as you should probably have a separate class // Generate a random float between 0 and 1
* representing the field of cells... float randVal = rand.nextFloat();
* maybe just make a constructor in there if (randVal < chanceOfLife) {
* and use it here setCell(x, y, 1);
*/ } else {
setCell(x, y, 0);
}
}
}
} }
public boolean isLoopingBorder() { public boolean isLoopingBorder() {
@ -259,11 +271,11 @@ public class Simulator extends Thread {
loopingBorder = !loopingBorder; loopingBorder = !loopingBorder;
} }
public void setLoopDelay(int delay) { public void setLoopDelay(int delay) {
//TODO : complete method loopDelay = delay;
} }
public void toggleClickAction() { public void toggleClickAction() {
//TODO : complete method clickActionFlag = !clickActionFlag;
} }
/** /**
@ -321,9 +333,8 @@ public class Simulator extends Thread {
* @return String representation of click action * @return String representation of click action
*/ */
public String clickActionName() { public String clickActionName() {
// TODO : initially return "sheep" or "cell" return clickActionFlag ? "Sheep" : "Cell";
// depending on clickActionFlag
return "";
} }
} }