Stop at random

This commit is contained in:
mathisdebard 2024-05-14 17:28:56 +02:00
parent f802340d3c
commit ca0aa50170
1 changed files with 20 additions and 36 deletions

View File

@ -1,6 +1,6 @@
package backend;
import java.util.ArrayList;
import java.util.Random;
import windowInterface.MyInterface;
public class Simulator extends Thread {
@ -25,11 +25,9 @@ public class Simulator extends Thread {
private boolean clickActionFlag;
private int loopDelay = 150;
private int[][] board;
private int width;
private int height;
private int clickActionFlagValue;
//TODO : add missing attribute(s)
public Simulator(MyInterface mjfParam) {
@ -153,38 +151,10 @@ public class Simulator extends Thread {
// Method to handle different behaviors based on the clickActionFlagValue
public void clickCell(int x, int y) {
//TODO : complete method
switch (clickActionFlagValue) {
case 1: // Cell case (default): toggles the state of the cell at coordinates x,y
toggleCellState(x, y);
break;
case 2: // Agent case: create an Agent (Sheep provided for example) at provided coordinates
createAgent(x, y);
break;
// Add more cases as needed for different flag values
default:
// Default behavior
System.out.println("Invalid click action flag value.");
break;
}
board[x][y] = (board[x][y] == 0) ? 1 : 0;
}
// Method to toggle the state of the cell at coordinates x, y
private void toggleCellState(int x, int y) {
if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
// Toggle the state of the cell
board[x][y] = (board[x][y] == 0) ? 1 : 0; // Assuming 0 represents dead cell and 1 represents alive cell
} else {
// Handle out of bounds condition or throw an exception
System.out.println("Invalid coordinates: (" + x + ", " + y + ")");
// You can choose to throw an exception here instead
}
}
// Method to create an agent at the provided coordinates (example: Sheep)
private void createAgent(int x, int y) {
Agent agent = new Sheep(x, y);
}
/**
* get cell value in simulated world
* @param x coordinate of cell
@ -294,8 +264,22 @@ public class Simulator extends Thread {
* @param chanceOfLife the chance for each cell
* to be alive in new state
*/
public void generateRandom(float chanceOfLife) {
//TODO : complete method
Random random = new Random ();
for (int x = 0; x < COL_NUM; x++) {
for (int y = 0; y < LINE_NUM; y++) {
if (random.nextFloat() < chanceOfLife) {
setCell(x, y, 1);
} else {
setCell(x, y, 0);
}
}
}
}
/*
* Advice :
* as you should probably have a separate class
@ -303,7 +287,7 @@ public class Simulator extends Thread {
* maybe just make a constructor in there
* and use it here
*/
}
public boolean isLoopingBorder() {
//TODO : complete method with proper return
@ -324,7 +308,7 @@ public class Simulator extends Thread {
public void toggleClickAction() {
//TODO : complete method
clickActionFlagValue = (clickActionFlagValue == 1) ? 2 : 1;
clickActionFlag =! clickActionFlag;
}
/**