Update et Opti 1
This commit is contained in:
parent
df53509911
commit
4cb26b2615
|
|
@ -1,6 +1,7 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
@ -11,7 +12,6 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
private final int COL_NUM = 100;
|
private final int COL_NUM = 100;
|
||||||
private final int LINE_NUM = 100;
|
private final int LINE_NUM = 100;
|
||||||
private final int LIFE_TYPE_NUM = 4;
|
|
||||||
|
|
||||||
//Conway Radius : 1
|
//Conway Radius : 1
|
||||||
private final int LIFE_AREA_RADIUS = 1;
|
private final int LIFE_AREA_RADIUS = 1;
|
||||||
|
|
@ -98,71 +98,72 @@ public class Simulator extends Thread {
|
||||||
* method called at each step of the simulation
|
* method called at each step of the simulation
|
||||||
* makes all the actions to go from one step to the other
|
* makes all the actions to go from one step to the other
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void makeStep() {
|
public void makeStep() {
|
||||||
// agent behaviors first
|
// agent behaviors first
|
||||||
// only modify if sure of what you do
|
Iterator<Agent> it = agents.iterator();
|
||||||
// to modify agent behavior, see liveTurn method
|
while (it.hasNext()) {
|
||||||
// in agent classes
|
Agent agent = it.next();
|
||||||
for(Agent agent : agents) {
|
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
|
||||||
ArrayList<Agent> neighbors =
|
if (!agent.liveTurn(neighbors, this)) {
|
||||||
this.getNeighboringAnimals(
|
it.remove();
|
||||||
agent.getX(),
|
}
|
||||||
agent.getY(),
|
}
|
||||||
ANIMAL_AREA_RADIUS);
|
// Then evolution of the field
|
||||||
if(!agent.liveTurn(
|
evolveField();
|
||||||
neighbors,
|
System.out.println("makeStep called");
|
||||||
this)) {
|
}
|
||||||
agents.remove(agent);
|
|
||||||
}
|
|
||||||
evolveField();
|
|
||||||
}
|
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 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 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
|
//then evolution of the field
|
||||||
// TODO : apply game rule to all cells of the field
|
// TODO : apply game rule to all cells of the field
|
||||||
|
|
@ -190,8 +191,16 @@ public class Simulator extends Thread {
|
||||||
* leave this as is
|
* leave this as is
|
||||||
*/
|
*/
|
||||||
public void stopSimu() {
|
public void stopSimu() {
|
||||||
stopFlag=true;
|
stopFlag = true;
|
||||||
worldLoaded = false;
|
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() {
|
public void toggleClickAction() {
|
||||||
clickActionFlag = !clickActionFlag;
|
clickActionFlag = !clickActionFlag;
|
||||||
if (!this.isAlive() && worldLoaded) {
|
if (!this.isAlive() && worldLoaded) {
|
||||||
startSimu();
|
startSimu();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -419,13 +428,22 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
public void loadAgents(ArrayList<String> stringArray) {
|
||||||
|
agents.clear();
|
||||||
for (String line : stringArray) {
|
for (String line : stringArray) {
|
||||||
String[] elements = line.split(";");
|
String[] elements = line.split(";");
|
||||||
|
if (elements.length < 3) {
|
||||||
|
continue; // Skip invalid lines
|
||||||
|
}
|
||||||
int x = Integer.parseInt(elements[0]);
|
int x = Integer.parseInt(elements[0]);
|
||||||
int y = Integer.parseInt(elements[1]);
|
int y = Integer.parseInt(elements[1]);
|
||||||
String type = elements[2];
|
String type = elements[2];
|
||||||
if (type.equals("Sheep")) {
|
switch (type) {
|
||||||
agents.add(new Sheep(x, y));
|
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";
|
return clickActionFlag ? "sheep" : "cell";
|
||||||
}
|
}
|
||||||
public void startSimu() {
|
public void startSimu() {
|
||||||
if (!this.isAlive()) {
|
if (worldLoaded) {
|
||||||
stopFlag = false;
|
if (!this.isAlive()) {
|
||||||
this.start();
|
stopFlag = false;
|
||||||
} else {
|
this.start();
|
||||||
togglePause();
|
} else {
|
||||||
|
togglePause();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeWorld() {
|
public void initializeWorld() {
|
||||||
grid = new int[LINE_NUM][COL_NUM];
|
grid = new int[LINE_NUM][COL_NUM];
|
||||||
agents.clear();
|
agents.clear();
|
||||||
worldLoaded = true;
|
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