From 434f252f439b365c0de58366a6f04f232becfa45 Mon Sep 17 00:00:00 2001 From: paull Date: Mon, 27 May 2024 18:13:54 +0200 Subject: [PATCH] task 1 working (Load World etc) --- src/backend/Simulator.java | 217 ++++++++++++++++++++++++------------- 1 file changed, 143 insertions(+), 74 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index ec5660e..73a5abc 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -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; + //Conway Radius : 1 + private final int LIFE_AREA_RADIUS = 1; + //Animal Neighborhood Radius : 5 + private final int ANIMAL_AREA_RADIUS = 2; + private ArrayList fieldSurviveValues; + private ArrayList fieldBirthValues; + + private ArrayList agents; + + private boolean stopFlag; + private boolean pauseFlag; + private boolean loopingBorder; + private boolean clickActionFlag; + private int loopDelay = 150; - 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 fieldSurviveValues; - private ArrayList fieldBirthValues; - - private ArrayList agents; - - 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; @@ -47,32 +49,39 @@ public class Simulator extends Thread { public int getWidth() { return COL_NUM; } - + public int getHeight() { 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]; @@ -98,6 +107,7 @@ public class Simulator extends Thread { grid = newGrid; } + private int countAliveNeighbors(int x, int y) { int aliveCount = 0; @@ -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)); @@ -136,7 +156,13 @@ public class Simulator extends Thread { 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) { 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 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 getNeighboringAnimals(int x, int y, int radius) { ArrayList inArea = new ArrayList(); for (Agent agent : agents) { @@ -187,7 +194,24 @@ 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 getSaveState() { ArrayList saveState = new ArrayList(); 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 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 getRule() { ArrayList rules = new ArrayList(); 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"; }