task 1 working (Load World etc)

This commit is contained in:
paull 2024-05-27 18:13:54 +02:00
parent a0807f700c
commit 434f252f43
1 changed files with 143 additions and 74 deletions

View File

@ -1,29 +1,31 @@
package backend;
import java.util.ArrayList;
import windowInterface.MyInterface;
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;
private final int LIFE_AREA_RADIUS = 1;
private final int ANIMAL_AREA_RADIUS = 2;
private ArrayList<Integer> fieldSurviveValues;
private ArrayList<Integer> fieldBirthValues;
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 ArrayList<Agent> agents;
private boolean stopFlag;
private boolean pauseFlag;
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
private boolean stopFlag;
private boolean pauseFlag;
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
private int[][] grid;
public int[][] grid;
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
@ -52,26 +54,33 @@ public class Simulator extends Thread {
return LINE_NUM;
}
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();
}
}
}
}
//Should probably stay as is
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();
}
}
}
}
/**
* method called at each step of the simulation
* makes all the actions to go from one step to the other
*/
public void makeStep() {
int[][] newGrid = new int[LINE_NUM][COL_NUM];
@ -99,6 +108,7 @@ public class Simulator extends Thread {
grid = newGrid;
}
private int countAliveNeighbors(int x, int y) {
int aliveCount = 0;
int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
@ -120,14 +130,24 @@ public class Simulator extends Thread {
return aliveCount;
}
/*
* leave this as is
*/
public void stopSimu() {
stopFlag=true;
}
/*
* method called when clicking pause button
*/
public void togglePause() {
pauseFlag = !pauseFlag;
}
/**
* method called when clicking on a cell in the interface
*/
public void clickCell(int x, int y) {
if (clickActionFlag) {
agents.add(new Sheep(x, y));
@ -137,6 +157,12 @@ public class Simulator extends Thread {
}
}
/**
* 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) {
if (x >= 0 && x < COL_NUM && y >= 0 && y < LINE_NUM) {
return grid[y][x];
@ -144,40 +170,21 @@ public class Simulator extends Thread {
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;
}
}
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;
}
}
}
/**
*
* @return list of Animals in simulated world
*/
public ArrayList<Agent> getAnimals() {
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) {
ArrayList<Agent> inArea = new ArrayList<Agent>();
for (Agent agent : agents) {
@ -188,6 +195,23 @@ public class Simulator extends Thread {
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() {
ArrayList<String> saveState = new ArrayList<String>();
for (int y = 0; y < LINE_NUM; y++) {
@ -203,6 +227,10 @@ public class Simulator extends Thread {
return saveState;
}
/**
*
* @param lines of file representing saved world state
*/
public void loadSaveState(ArrayList<String> lines) {
if (lines.size() != LINE_NUM) {
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() {
ArrayList<String> rules = new ArrayList<String>();
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() {
return clickActionFlag ? "sheep" : "cell";
}