task 1 working (Load World etc)
This commit is contained in:
parent
a0807f700c
commit
434f252f43
|
|
@ -1,29 +1,31 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
public class Simulator extends Thread {
|
public class Simulator extends Thread {
|
||||||
|
|
||||||
private MyInterface mjf;
|
private MyInterface mjf;
|
||||||
|
|
||||||
|
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;
|
||||||
|
//Animal Neighborhood Radius : 5
|
||||||
|
private final int ANIMAL_AREA_RADIUS = 2;
|
||||||
|
private ArrayList<Integer> fieldSurviveValues;
|
||||||
|
private ArrayList<Integer> fieldBirthValues;
|
||||||
|
|
||||||
|
private ArrayList<Agent> agents;
|
||||||
|
|
||||||
|
private boolean stopFlag;
|
||||||
|
private boolean pauseFlag;
|
||||||
|
private boolean loopingBorder;
|
||||||
|
private boolean clickActionFlag;
|
||||||
|
private int loopDelay = 150;
|
||||||
|
|
||||||
private final int COL_NUM = 100;
|
public int[][] grid;
|
||||||
private final int LINE_NUM = 100;
|
|
||||||
private final int LIFE_TYPE_NUM = 4;
|
|
||||||
private final int LIFE_AREA_RADIUS = 1;
|
|
||||||
private final int ANIMAL_AREA_RADIUS = 2;
|
|
||||||
private ArrayList<Integer> fieldSurviveValues;
|
|
||||||
private ArrayList<Integer> fieldBirthValues;
|
|
||||||
|
|
||||||
private ArrayList<Agent> agents;
|
|
||||||
|
|
||||||
private boolean stopFlag;
|
|
||||||
private boolean pauseFlag;
|
|
||||||
private boolean loopingBorder;
|
|
||||||
private boolean clickActionFlag;
|
|
||||||
private int loopDelay = 150;
|
|
||||||
|
|
||||||
private int[][] grid;
|
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
mjf = mjfParam;
|
mjf = mjfParam;
|
||||||
|
|
@ -47,32 +49,39 @@ public class Simulator extends Thread {
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return COL_NUM;
|
return COL_NUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
return LINE_NUM;
|
return LINE_NUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
//Should probably stay as is
|
||||||
int stepCount = 0;
|
public void run() {
|
||||||
while (!stopFlag) {
|
int stepCount=0;
|
||||||
stepCount++;
|
while(!stopFlag) {
|
||||||
makeStep();
|
stepCount++;
|
||||||
mjf.update(stepCount);
|
makeStep();
|
||||||
try {
|
mjf.update(stepCount);
|
||||||
Thread.sleep(loopDelay);
|
try {
|
||||||
} catch (InterruptedException e) {
|
Thread.sleep(loopDelay);
|
||||||
e.printStackTrace();
|
} catch (InterruptedException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
while (pauseFlag && !stopFlag) {
|
}
|
||||||
try {
|
while(pauseFlag && !stopFlag) {
|
||||||
Thread.sleep(loopDelay);
|
try {
|
||||||
} catch (InterruptedException e) {
|
Thread.sleep(loopDelay);
|
||||||
e.printStackTrace();
|
} catch (InterruptedException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method called at each step of the simulation
|
||||||
|
* makes all the actions to go from one step to the other
|
||||||
|
*/
|
||||||
|
|
||||||
public void makeStep() {
|
public void makeStep() {
|
||||||
int[][] newGrid = new int[LINE_NUM][COL_NUM];
|
int[][] newGrid = new int[LINE_NUM][COL_NUM];
|
||||||
|
|
||||||
|
|
@ -98,6 +107,7 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
grid = newGrid;
|
grid = newGrid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int countAliveNeighbors(int x, int y) {
|
private int countAliveNeighbors(int x, int y) {
|
||||||
int aliveCount = 0;
|
int aliveCount = 0;
|
||||||
|
|
@ -120,14 +130,24 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
return aliveCount;
|
return aliveCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* leave this as is
|
||||||
|
*/
|
||||||
public void stopSimu() {
|
public void stopSimu() {
|
||||||
stopFlag=true;
|
stopFlag=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* method called when clicking pause button
|
||||||
|
*/
|
||||||
public void togglePause() {
|
public void togglePause() {
|
||||||
pauseFlag = !pauseFlag;
|
pauseFlag = !pauseFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method called when clicking on a cell in the interface
|
||||||
|
*/
|
||||||
public void clickCell(int x, int y) {
|
public void clickCell(int x, int y) {
|
||||||
if (clickActionFlag) {
|
if (clickActionFlag) {
|
||||||
agents.add(new Sheep(x, y));
|
agents.add(new Sheep(x, y));
|
||||||
|
|
@ -136,7 +156,13 @@ public class Simulator extends Thread {
|
||||||
setCell(x, y, currentState == 0 ? 1 : 0);
|
setCell(x, y, currentState == 0 ? 1 : 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get cell value in simulated world
|
||||||
|
* @param x coordinate of cell
|
||||||
|
* @param y coordinate of cell
|
||||||
|
* @return value of cell
|
||||||
|
*/
|
||||||
public int getCell(int x, int y) {
|
public int getCell(int x, int y) {
|
||||||
if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) {
|
if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) {
|
||||||
return grid[y][x];
|
return grid[y][x];
|
||||||
|
|
@ -144,40 +170,21 @@ public class Simulator extends Thread {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCell(int x, int y, int val) {
|
/**
|
||||||
if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) {
|
*
|
||||||
grid[y][x] = val;
|
* @return list of Animals in simulated world
|
||||||
}
|
*/
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleLoopingBorder() {
|
|
||||||
loopingBorder = !loopingBorder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
|
||||||
return loopingBorder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
|
||||||
loopDelay = delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toggleClickAction() {
|
|
||||||
clickActionFlag = !clickActionFlag;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateRandom(float chanceOfLife) {
|
|
||||||
for (int y = 0; y < LINE_NUM; y++) {
|
|
||||||
for (int x = 0; x < COL_NUM; x++) {
|
|
||||||
grid[y][x] = Math.random() < chanceOfLife ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Agent> getAnimals() {
|
public ArrayList<Agent> getAnimals() {
|
||||||
return agents;
|
return agents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* selects Animals in a circular area of simulated world
|
||||||
|
* @param x center
|
||||||
|
* @param y center
|
||||||
|
* @param radius
|
||||||
|
* @return list of agents in area
|
||||||
|
*/
|
||||||
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius) {
|
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius) {
|
||||||
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
||||||
for (Agent agent : agents) {
|
for (Agent agent : agents) {
|
||||||
|
|
@ -187,7 +194,24 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
return inArea;
|
return inArea;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set value of cell
|
||||||
|
* @param x coord of cell
|
||||||
|
* @param y coord of cell
|
||||||
|
* @param val to set in cell
|
||||||
|
*/
|
||||||
|
public void setCell(int x, int y, int val) {
|
||||||
|
if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) {
|
||||||
|
grid[y][x] = val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return lines of file representing
|
||||||
|
* the simulated world in its present state
|
||||||
|
*/
|
||||||
public ArrayList<String> getSaveState() {
|
public ArrayList<String> getSaveState() {
|
||||||
ArrayList<String> saveState = new ArrayList<String>();
|
ArrayList<String> saveState = new ArrayList<String>();
|
||||||
for (int y = 0; y < LINE_NUM; y++) {
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
|
@ -203,6 +227,10 @@ public class Simulator extends Thread {
|
||||||
return saveState;
|
return saveState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param lines of file representing saved world state
|
||||||
|
*/
|
||||||
public void loadSaveState(ArrayList<String> lines) {
|
public void loadSaveState(ArrayList<String> lines) {
|
||||||
if (lines.size() != LINE_NUM) {
|
if (lines.size() != LINE_NUM) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -218,6 +246,44 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* called by button, with slider providing the argument
|
||||||
|
* makes a new world state with random cell states
|
||||||
|
* @param chanceOfLife the chance for each cell
|
||||||
|
* to be alive in new state
|
||||||
|
*/
|
||||||
|
public void generateRandom(float chanceOfLife) {
|
||||||
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
grid[y][x] = Math.random() < chanceOfLife ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLoopingBorder() {
|
||||||
|
return loopingBorder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toggleLoopingBorder() {
|
||||||
|
loopingBorder = !loopingBorder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoopDelay(int delay) {
|
||||||
|
loopDelay = delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toggleClickAction() {
|
||||||
|
clickActionFlag = !clickActionFlag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prepare the content of a file saving present ruleSet
|
||||||
|
* as you might want to save a state,
|
||||||
|
* initialy written in this class constructor
|
||||||
|
* as a file for future use
|
||||||
|
* @return File content as an ArrayList of Lines (String)
|
||||||
|
* @see loadRule for inverse process
|
||||||
|
*/
|
||||||
public ArrayList<String> getRule() {
|
public ArrayList<String> getRule() {
|
||||||
ArrayList<String> rules = new ArrayList<String>();
|
ArrayList<String> rules = new ArrayList<String>();
|
||||||
StringBuilder surviveBuilder = new StringBuilder();
|
StringBuilder surviveBuilder = new StringBuilder();
|
||||||
|
|
@ -277,7 +343,10 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* used by label in interface to show the active click action
|
||||||
|
* @return String representation of click action
|
||||||
|
*/
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
return clickActionFlag ? "sheep" : "cell";
|
return clickActionFlag ? "sheep" : "cell";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue