Update et Opti 1
This commit is contained in:
parent
df53509911
commit
4cb26b2615
|
|
@ -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,70 +98,71 @@ 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);
|
||||
}
|
||||
evolveField();
|
||||
}
|
||||
// agent behaviors first
|
||||
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];
|
||||
for (int y = 0; y < LINE_NUM; y++) {
|
||||
for (int x = 0; x < COL_NUM; x++) {
|
||||
int neighbors = countAliveNeighbors(x, y);
|
||||
if (grid[y][x] == 1) { // Cell is alive
|
||||
if (fieldSurviveValues.contains(neighbors)) {
|
||||
newGrid[y][x] = 1;
|
||||
} else {
|
||||
newGrid[y][x] = 0;
|
||||
}
|
||||
} else { // Cell is dead
|
||||
if (fieldBirthValues.contains(neighbors)) {
|
||||
newGrid[y][x] = 1;
|
||||
} else {
|
||||
newGrid[y][x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
grid = newGrid;
|
||||
}
|
||||
|
||||
|
||||
private int countAliveNeighbors(int x, int y) {
|
||||
int count = 0;
|
||||
for (int i = -LIFE_AREA_RADIUS; i <= LIFE_AREA_RADIUS; i++) {
|
||||
for (int j = -LIFE_AREA_RADIUS; j <= LIFE_AREA_RADIUS; j++) {
|
||||
if (i == 0 && j == 0) continue;
|
||||
int nx = x + j;
|
||||
int ny = y + i;
|
||||
if (loopingBorder) {
|
||||
nx = (nx + COL_NUM) % COL_NUM;
|
||||
ny = (ny + LINE_NUM) % LINE_NUM;
|
||||
}
|
||||
if (nx >= 0 && nx < COL_NUM && ny >= 0 && ny < LINE_NUM) {
|
||||
if (grid[ny][nx] == 1) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
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;
|
||||
} else {
|
||||
newGrid[y][x] = 0;
|
||||
}
|
||||
} else { // Cell is dead
|
||||
if (fieldBirthValues.contains(neighbors)) {
|
||||
newGrid[y][x] = 1;
|
||||
} else {
|
||||
newGrid[y][x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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++) {
|
||||
for (int j = -LIFE_AREA_RADIUS; j <= LIFE_AREA_RADIUS; j++) {
|
||||
if (i == 0 && j == 0) continue;
|
||||
int nx = x + j;
|
||||
int ny = y + i;
|
||||
if (loopingBorder) {
|
||||
nx = (nx + COL_NUM) % COL_NUM;
|
||||
ny = (ny + LINE_NUM) % LINE_NUM;
|
||||
}
|
||||
if (nx >= 0 && nx < COL_NUM && ny >= 0 && ny < LINE_NUM) {
|
||||
if (grid[ny][nx] == 1) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//then evolution of the field
|
||||
|
|
@ -190,8 +191,16 @@ public class Simulator extends Thread {
|
|||
* leave this as is
|
||||
*/
|
||||
public void stopSimu() {
|
||||
stopFlag=true;
|
||||
worldLoaded = false;
|
||||
stopFlag = true;
|
||||
while (this.isAlive()) {
|
||||
try {
|
||||
Thread.sleep(loopDelay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
worldLoaded = false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -363,8 +372,8 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
clickActionFlag = !clickActionFlag;
|
||||
if (!this.isAlive() && worldLoaded) {
|
||||
clickActionFlag = !clickActionFlag;
|
||||
if (!this.isAlive() && worldLoaded) {
|
||||
startSimu();
|
||||
}
|
||||
}
|
||||
|
|
@ -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")) {
|
||||
agents.add(new Sheep(x, y));
|
||||
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,17 +460,36 @@ public class Simulator extends Thread {
|
|||
return clickActionFlag ? "sheep" : "cell";
|
||||
}
|
||||
public void startSimu() {
|
||||
if (!this.isAlive()) {
|
||||
stopFlag = false;
|
||||
this.start();
|
||||
} else {
|
||||
togglePause();
|
||||
if (worldLoaded) {
|
||||
if (!this.isAlive()) {
|
||||
stopFlag = false;
|
||||
this.start();
|
||||
} else {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue