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;
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>();
@ -50,13 +51,11 @@ public class Simulator extends Thread {
}
public int getWidth() {
//TODO : replace with proper return
return 0;
return COL_NUM;
}
public int getHeight() {
//TODO : replace with proper return
return 0;
return LINE_NUM;
}
//Should probably stay as is
@ -136,14 +135,20 @@ public class Simulator extends Thread {
* method called when clicking pause button
*/
public void togglePause() {
// TODO : actually toggle the corresponding flag
pauseFlag = !pauseFlag;
}
/**
* method called when clicking on a cell in the interface
*/
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
*/
public int getCell(int x, int y) {
//TODO : complete method with proper return
return 0;
if (x < 0 || x >= COL_NUM || y < 0 || y >= LINE_NUM) {
return 0;
}
return grid[x][y];
}
/**
*
@ -188,7 +195,9 @@ public class Simulator extends Thread {
* @param val to set in cell
*/
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
*/
public ArrayList<String> getSaveState() {
//TODO : complete method with proper return
return null;
}
/**
@ -241,14 +249,18 @@ public class Simulator extends Thread {
* to be alive in new state
*/
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
*/
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() {
@ -259,11 +271,11 @@ public class Simulator extends Thread {
loopingBorder = !loopingBorder;
}
public void setLoopDelay(int delay) {
//TODO : complete method
loopDelay = delay;
}
public void toggleClickAction() {
//TODO : complete method
clickActionFlag = !clickActionFlag;
}
/**
@ -321,9 +333,8 @@ public class Simulator extends Thread {
* @return String representation of click action
*/
public String clickActionName() {
// TODO : initially return "sheep" or "cell"
// depending on clickActionFlag
return "";
return clickActionFlag ? "Sheep" : "Cell";
}
}