Update et Opti 1

This commit is contained in:
elmirador 2024-05-31 22:36:20 +02:00
parent df53509911
commit 4cb26b2615
1 changed files with 112 additions and 75 deletions

View File

@ -1,6 +1,7 @@
package backend;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import windowInterface.MyInterface;
@ -11,7 +12,6 @@ public class Simulator extends Thread {
private final int COL_NUM = 100;
private final int LINE_NUM = 100;
private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1;
@ -98,31 +98,31 @@ public class Simulator extends Thread {
* method called at each step of the simulation
* makes all the actions to go from one step to the other
*/
public void makeStep() {
// agent behaviors first
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(
neighbors,
this)) {
agents.remove(agent);
Iterator<Agent> it = agents.iterator();
while (it.hasNext()) {
Agent agent = it.next();
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
if (!agent.liveTurn(neighbors, this)) {
it.remove();
}
}
// Then evolution of the field
evolveField();
System.out.println("makeStep called");
}
}
private void evolveField() {
int[][] newGrid = new int[LINE_NUM][COL_NUM];
System.out.println("evolveField called");
for (int y = 0; y < LINE_NUM; y++) {
for (int x = 0; x < COL_NUM; x++) {
int neighbors = countAliveNeighbors(x, y);
System.out.println("Cell (" + x + "," + y + ") has " + neighbors + " neighbors");
if (grid[y][x] == 1) { // Cell is alive
if (fieldSurviveValues.contains(neighbors)) {
newGrid[y][x] = 1;
@ -139,9 +139,9 @@ public class Simulator extends Thread {
}
}
grid = newGrid;
System.out.println("Grid updated");
}
private int countAliveNeighbors(int x, int y) {
int count = 0;
for (int i = -LIFE_AREA_RADIUS; i <= LIFE_AREA_RADIUS; i++) {
@ -164,6 +164,7 @@ public class Simulator extends Thread {
}
//then evolution of the field
// TODO : apply game rule to all cells of the field
@ -191,7 +192,15 @@ public class Simulator extends Thread {
*/
public void stopSimu() {
stopFlag = true;
while (this.isAlive()) {
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
worldLoaded = false;
}
@ -419,13 +428,22 @@ public class Simulator extends Thread {
public void loadAgents(ArrayList<String> stringArray) {
agents.clear();
for (String line : stringArray) {
String[] elements = line.split(";");
if (elements.length < 3) {
continue; // Skip invalid lines
}
int x = Integer.parseInt(elements[0]);
int y = Integer.parseInt(elements[1]);
String type = elements[2];
if (type.equals("Sheep")) {
switch (type) {
case "Sheep":
agents.add(new Sheep(x, y));
break;
// Add cases for other agent types if needed
default:
System.out.println("Unknown agent type: " + type);
}
}
}
@ -442,6 +460,7 @@ public class Simulator extends Thread {
return clickActionFlag ? "sheep" : "cell";
}
public void startSimu() {
if (worldLoaded) {
if (!this.isAlive()) {
stopFlag = false;
this.start();
@ -449,10 +468,28 @@ public class Simulator extends Thread {
togglePause();
}
}
}
public void initializeWorld() {
grid = new int[LINE_NUM][COL_NUM];
agents.clear();
worldLoaded = true;
}
public void restartSimu() {
stopFlag = true;
pauseFlag = false;
while (this.isAlive()) {
try {
Thread.sleep(loopDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
stopFlag = false;
this.start();
}
}