388 lines
9.4 KiB
Java
388 lines
9.4 KiB
Java
package backend;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
|
|
import windowInterface.MyInterface;
|
|
|
|
public class Simulator extends Thread {
|
|
|
|
private MyInterface mjf;
|
|
|
|
private final int COL_NUM = 200;
|
|
private final int LINE_NUM = 100;
|
|
private int[][] simuGrid = new int[COL_NUM][LINE_NUM];
|
|
private final int LIFE_TYPE_NUM = 4;
|
|
//Conway Radius : 1
|
|
private final int LIFE_AREA_RADIUS = 1;
|
|
private ArrayList<Integer> fieldSurviveValues;
|
|
private ArrayList<Integer> fieldBirthValues;
|
|
|
|
private ArrayList<Agent> agents;
|
|
|
|
private boolean stopFlag;
|
|
private boolean pauseFlag;
|
|
private boolean loopingBorder;
|
|
private int clickActionFlag; // bool --> int to have 3 different actions (cell, sheep, wolf)
|
|
private int loopDelay = 150;
|
|
|
|
public Simulator(MyInterface mjfParam) {
|
|
mjf = mjfParam;
|
|
stopFlag=false;
|
|
pauseFlag=false;
|
|
loopingBorder=false;
|
|
clickActionFlag=0;
|
|
|
|
agents = new ArrayList<Agent>();
|
|
fieldBirthValues = new ArrayList<Integer>();
|
|
fieldSurviveValues = new ArrayList<Integer>();
|
|
|
|
for(int x=0; x<COL_NUM; x++) {
|
|
for(int y=0; y<LINE_NUM; y++) {
|
|
this.setCell(x, y, 0);
|
|
}
|
|
}
|
|
|
|
//Default rule : Survive always, birth never
|
|
|
|
for(int i =0; i<9; i++) {
|
|
fieldSurviveValues.add(i);
|
|
}
|
|
}
|
|
|
|
public int getWidth() {
|
|
return this.COL_NUM;
|
|
}
|
|
|
|
public int getHeight() {
|
|
return this.LINE_NUM;
|
|
}
|
|
|
|
public int getAliveNeighboorCells(int x, int y) {
|
|
int aliveNumber = 0;
|
|
|
|
if(loopingBorder == false) {
|
|
for(int i=x-1; i<=x+1; i++) {
|
|
for(int j=y-1; j<=y+1; j++) {
|
|
|
|
if(i<0 || i>=COL_NUM || j<0 || j>=LINE_NUM || (i == x && j == y)) {
|
|
continue;
|
|
}
|
|
if(this.getCell(i, j) == 1) {
|
|
aliveNumber ++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(loopingBorder == true) {
|
|
for(int i=x-LIFE_AREA_RADIUS; i<=x+LIFE_AREA_RADIUS; i++) {
|
|
for(int j=y-LIFE_AREA_RADIUS; j<=y+LIFE_AREA_RADIUS; j++) {
|
|
|
|
if(i == x && j == y) {
|
|
continue;
|
|
}
|
|
|
|
if(this.getCell((i+COL_NUM)%COL_NUM, (j+LINE_NUM)%LINE_NUM) == 1) {
|
|
aliveNumber ++;
|
|
}
|
|
}
|
|
}
|
|
System.out.println("");
|
|
}
|
|
return aliveNumber;
|
|
}
|
|
|
|
public void run() {
|
|
|
|
int stepCount=0;
|
|
while(!stopFlag) {
|
|
stepCount++;
|
|
makeStep();
|
|
mjf.update(stepCount);
|
|
try {
|
|
Thread.sleep(loopDelay);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
while(pauseFlag && !stopFlag) {
|
|
try {
|
|
Thread.sleep(loopDelay);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void makeStep() {
|
|
|
|
// evolution of the animals
|
|
ArrayList<Agent> morgue = new ArrayList<Agent>(); // temporary list containing all the dead entities :'(
|
|
|
|
/*for(Agent agent : agents) {
|
|
ArrayList<Agent> neighbors =
|
|
this.getNeighboringAnimals(
|
|
agent.getX(),
|
|
agent.getY(),
|
|
ANIMAL_AREA_RADIUS);
|
|
if(!agent.liveTurn(
|
|
neighbors,
|
|
this)) {
|
|
agents.remove(agent);
|
|
}
|
|
}*/
|
|
|
|
// ConcurrentModificationException ?? We cannot iterate a list's modification ?
|
|
// The following piece of code fixes this by directly using an iterator
|
|
|
|
for (Iterator<Agent> iterator = agents.iterator(); iterator.hasNext();) {
|
|
Agent agent = iterator.next();
|
|
|
|
ArrayList<Agent> neighbors =
|
|
this.getNeighboringAnimals(
|
|
agent.getX(),
|
|
agent.getY(),
|
|
agent.triggerRadius);
|
|
|
|
if(agent.liveTurn(
|
|
neighbors,
|
|
this)) {
|
|
morgue.add(agent);
|
|
}
|
|
}
|
|
// clean the morgue and modify the agents list OUTSIDE the iteration
|
|
agents.removeAll(morgue);
|
|
|
|
// evolution of the field
|
|
int n;
|
|
int[][] stepGrid = new int[COL_NUM][LINE_NUM];
|
|
|
|
for(int x=0; x<COL_NUM; x++) {
|
|
for(int y=0; y<LINE_NUM; y++) {
|
|
|
|
// the idea is first to get the surrounding values
|
|
// then count how many are alive
|
|
n = getAliveNeighboorCells(x, y);
|
|
|
|
stepGrid[x][y] = 0;
|
|
// then check if that number is in the lists of rules
|
|
// if cell not alive & survive list
|
|
for(int element : fieldSurviveValues) {
|
|
if(n == element && this.getCell(x, y) == 1) {
|
|
stepGrid[x][y] = 1;
|
|
continue;
|
|
}
|
|
}
|
|
// if cell is not alive & birth list
|
|
for(int element : fieldBirthValues) {
|
|
if(n == element && this.getCell(x, y) == 0) {
|
|
stepGrid[x][y] = 1;
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// apply changes to actual grid (simuGrid)
|
|
for(int x=0; x<COL_NUM; x++) {
|
|
for(int y=0; y<LINE_NUM; y++) {
|
|
this.setCell(x, y, stepGrid[x][y]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void stopSimu() {
|
|
stopFlag = true;
|
|
}
|
|
|
|
public void togglePause() {
|
|
pauseFlag = !pauseFlag;
|
|
}
|
|
|
|
public void clickCell(int x, int y) {
|
|
if(clickActionFlag%3 == 0) { // place cell
|
|
this.setCell(x, y, Math.abs(this.getCell(x, y) - 1));
|
|
}
|
|
if(clickActionFlag%3 == 1) { // place sheep
|
|
Sheep mySheep = new Sheep(x, y);
|
|
agents.add(mySheep);
|
|
}
|
|
if(clickActionFlag%3 == 2) { // place wolf
|
|
Wolf myWolf = new Wolf(x, y);
|
|
agents.add(myWolf);
|
|
}
|
|
}
|
|
|
|
public int getCell(int x, int y) {
|
|
return this.simuGrid[x][y];
|
|
}
|
|
|
|
public ArrayList<Agent> getAnimals(){
|
|
return agents;
|
|
}
|
|
|
|
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
|
|
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
|
for(int i=0;i<agents.size();i++) {
|
|
Agent agent = agents.get(i);
|
|
if(agent.isInArea(x,y,radius)) {
|
|
inArea.add(agent);
|
|
}
|
|
}
|
|
return inArea;
|
|
}
|
|
|
|
public void setCell(int x, int y, int val) {
|
|
this.simuGrid[x][y] = val;
|
|
}
|
|
|
|
public ArrayList<String> getSaveState() {
|
|
|
|
ArrayList<String> lines = new ArrayList<String>(LINE_NUM);
|
|
for(int x=0; x<LINE_NUM; x++) {
|
|
String line = "";
|
|
|
|
for(int y=0; y<COL_NUM; y++) {
|
|
|
|
line += String.valueOf(this.getCell(y, x)) + ";";
|
|
}
|
|
lines.add(line);
|
|
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
public void loadSaveState(ArrayList<String> lines) {
|
|
|
|
if(lines.size()<=0) {
|
|
return;
|
|
}
|
|
String firstLine = lines.get(0);
|
|
String[] firstLineElements = firstLine.split(";");
|
|
if(firstLineElements.length<=0) {
|
|
return;
|
|
}
|
|
/*
|
|
* now we fill in the world
|
|
* with the content of the file
|
|
*/
|
|
for(int y =0; y<lines.size();y++) {
|
|
String line = lines.get(y);
|
|
String[] lineElements = line.split(";");
|
|
for(int x=0; x<lineElements.length;x++) {
|
|
String elem = lineElements[x];
|
|
int value = Integer.parseInt(elem);
|
|
setCell(x, y, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void generateRandom(float chanceOfLife) {
|
|
|
|
for(int x=0; x<COL_NUM; x++) {
|
|
for(int y=0; y<LINE_NUM; y++) {
|
|
setCell(x, y, 0);
|
|
if((Math.random() * (chanceOfLife + 1)) >= 1) {
|
|
setCell(x, y, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean isLoopingBorder() {
|
|
return loopingBorder;
|
|
}
|
|
|
|
public void toggleLoopingBorder() {
|
|
loopingBorder = !loopingBorder;
|
|
}
|
|
|
|
public void setLoopDelay(int delay) {
|
|
this.loopDelay = delay;
|
|
}
|
|
|
|
public void toggleClickAction() {
|
|
clickActionFlag ++;
|
|
}
|
|
|
|
public ArrayList<String> getRule() {
|
|
|
|
ArrayList<String> lines = new ArrayList<String>(2);
|
|
for(int elem : fieldSurviveValues) {
|
|
String line = "";
|
|
line += String.valueOf(elem) + ";";
|
|
lines.add(line);
|
|
}
|
|
for(int elem : fieldBirthValues) {
|
|
String line = "";
|
|
line += String.valueOf(elem) + ";";
|
|
lines.add(line);
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
public void loadRule(ArrayList<String> lines) {
|
|
|
|
// agent file empty?
|
|
if(lines.size()<=0) {
|
|
System.out.println("empty rule file");
|
|
return;
|
|
}
|
|
// (1) remove previous rule (= emptying lists)
|
|
this.fieldSurviveValues.clear();
|
|
this.fieldBirthValues.clear();
|
|
|
|
// (2) read the content of the selected rule file
|
|
String surviveLine = lines.get(0);
|
|
String birthLine = lines.get(1);
|
|
String[] surviveElements = surviveLine.split(";");
|
|
for(int x=0; x<surviveElements.length;x++) { // (3) convert each cell value into a rule
|
|
String elem = surviveElements[x];
|
|
int value = Integer.parseInt(elem);
|
|
this.fieldSurviveValues.add(value);
|
|
|
|
}
|
|
String[] birthElements = birthLine.split(";"); // (3) convert each cell value into a rule
|
|
for(int x=0; x<birthElements.length;x++) {
|
|
String elem = birthElements[x];
|
|
int value = Integer.parseInt(elem);
|
|
this.fieldBirthValues.add(value);
|
|
|
|
}
|
|
}
|
|
|
|
public ArrayList<String> getAgentsSave() {
|
|
//TODO : Same idea as the other save method, but for agents
|
|
return null;
|
|
}
|
|
|
|
public void loadAgents(ArrayList<String> stringArray) {
|
|
|
|
// agent file empty?
|
|
if(stringArray.size()<=0) {
|
|
System.out.println("empty agent file");
|
|
return;
|
|
}
|
|
// (1) remove previous agent(s) (= emptying lists)
|
|
this.agents.clear();
|
|
|
|
// (2) read the content of the selected agent file
|
|
String line = stringArray.get(0);
|
|
String[] agentCoord = line.split(";");
|
|
|
|
for(int i=0; i<agentCoord.length;i++) { // (3) convert each cell value into an agent coord. list
|
|
String coordList = agentCoord[i];
|
|
String[] coords = coordList.split(",");
|
|
|
|
int x = Integer.parseInt(coords[0]); // (4) read and convert the coord. of each list
|
|
int y = Integer.parseInt(coords[1]); // into integers, then create a new agent for each cell
|
|
Sheep mySheep = new Sheep(x, y);
|
|
agents.add(mySheep);
|
|
}
|
|
}
|
|
|
|
public String clickActionName() {
|
|
if(clickActionFlag%3 == 1) {return "sheep";}
|
|
if(clickActionFlag%3 == 2) {return "wolf";}
|
|
return "cell";
|
|
}
|
|
} |