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,6 +1,6 @@
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 {
@ -10,7 +10,9 @@ 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; private final int LIFE_TYPE_NUM = 4;
//Conway Radius : 1
private final int LIFE_AREA_RADIUS = 1; private final int LIFE_AREA_RADIUS = 1;
//Animal Neighborhood Radius : 5
private final int ANIMAL_AREA_RADIUS = 2; private final int ANIMAL_AREA_RADIUS = 2;
private ArrayList<Integer> fieldSurviveValues; private ArrayList<Integer> fieldSurviveValues;
private ArrayList<Integer> fieldBirthValues; private ArrayList<Integer> fieldBirthValues;
@ -23,7 +25,7 @@ public class Simulator extends Thread {
private boolean clickActionFlag; private boolean clickActionFlag;
private int loopDelay = 150; private int loopDelay = 150;
private int[][] grid; public int[][] grid;
public Simulator(MyInterface mjfParam) { public Simulator(MyInterface mjfParam) {
mjf = mjfParam; mjf = mjfParam;
@ -52,9 +54,10 @@ public class Simulator extends Thread {
return LINE_NUM; return LINE_NUM;
} }
//Should probably stay as is
public void run() { public void run() {
int stepCount = 0; int stepCount=0;
while (!stopFlag) { while(!stopFlag) {
stepCount++; stepCount++;
makeStep(); makeStep();
mjf.update(stepCount); mjf.update(stepCount);
@ -63,7 +66,7 @@ public class Simulator extends Thread {
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
while (pauseFlag && !stopFlag) { while(pauseFlag && !stopFlag) {
try { try {
Thread.sleep(loopDelay); Thread.sleep(loopDelay);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -71,8 +74,14 @@ 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() { public void makeStep() {
int[][] newGrid = new int[LINE_NUM][COL_NUM]; int[][] newGrid = new int[LINE_NUM][COL_NUM];
@ -99,6 +108,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;
int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1}; int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
@ -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));
@ -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) { 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) {
@ -188,6 +195,23 @@ 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";
} }