package backend; import java.util.ArrayList; import java.util.ListIterator; import windowInterface.MyInterface; public class Simulator extends Thread { private MyInterface mjf; //Size of the grid 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; //TODO : add missing attribute(s) private double randomDansitySlider = 0.5; private int width; private int height; private Gride gride; //Step put here to be reset when generate new field private int stepCount=0; private Rules rules; public Simulator(MyInterface mjfParam) { mjf = mjfParam; stopFlag=false; pauseFlag=false; loopingBorder=false; clickActionFlag=true; agents = new ArrayList(); //fieldSurviveValues = new ArrayList(); //fieldBirthValues = new ArrayList(); this.width=COL_NUM; this.height=LINE_NUM; gride = new Gride(height, width, this); //Reset rules to Conway rules //this.resetRules(); rules = new Rules(); } public int getWidth() { return this.width; } public int getHeight() { return this.height; } public void run() { //int stepCount=0; System.out.println("Step Count: "+ stepCount); //Set label when starting mjf.setClickBanner("click : " + this.clickActionName()); mjf.setBorderBanner("border : " + (this.isLoopingBorder()?"loop":"closed")); 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() { ListIterator iter = agents.listIterator(); while(iter.hasNext()){ Agent agent = iter.next(); //System.out.println(agent.getX() + "," + agent.getY()); ArrayList neighbors = this.getNeighboringAnimals( agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); if(!agent.liveTurn( neighbors, this)) { iter.remove(); } } /* for(Agent agent : agents) { ArrayList neighbors = this.getNeighboringAnimals( agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); if(!agent.liveTurn( neighbors, this)) { agents.remove(agent); } } */ this.applyStep(); } public void stopSimu() { stopFlag=true; } public void togglePause() { pauseFlag = !pauseFlag; } public void clickCell(int x, int y) { int currentCellValue = getCell(x, y); if (clickActionFlag == true) { int newCellValue = 0; if (currentCellValue == 0) { newCellValue = 1; } else { newCellValue = 0; } this.setCell(x, y, newCellValue); } else { //Apply sheep /agent here this.agents.add(new Sheep(x, y)); } return; } public int getCell(int x, int y) { return this.gride.getCell(x,y).getValue(); } public ArrayList getAnimals(){ return agents; } public ArrayList getNeighboringAnimals(int x, int y, int radius){ ArrayList inArea = new ArrayList(); for(int i=0;i getSaveState() { ArrayList strArrayList = new ArrayList<>(); String[] strArrayLine = new String[this.width]; //Store the state of the GRID for(int x=0 ;x lines) { if(lines.size()<=0) { return; } String firstLine = lines.get(0); String[] firstLineElements = firstLine.split(";"); if(firstLineElements.length<=0) { return; } for(int y =0; y getRule() { ArrayList strArrayList = new ArrayList<>(); String[] strArrayLine = new String[rules.getSurviveValues().size()]; //Add survive values for(int x=0; x lines) { if(lines.size()<=0) { System.out.println("empty rule file"); return; } rules.resetRules(); String surviveLine = lines.get(0); String birthLine = lines.get(1); String[] surviveElements = surviveLine.split(";"); //Load the values to survive for(int x=0; x getAgentsSave() { //TODO : Same idea as the other save method, but for agents ArrayList strArrayList = new ArrayList<>(); String[] strArrayLine = new String[this.agents.size()]; for(int ii=0;ii stringArray) { if(stringArray.size()<=0) { System.out.println("empty rule file"); return; } //Get the first line assuming it contains coordinates under format: X1,Y1;X2,Y2;X3,Y3 String strFirstLine = stringArray.get(0); String[] strListCoordinates = strFirstLine.split(";"); //Loop on agent's coordinates and create one agent for each X,Y for(int ii=0; ii