|
|
|
|
@ -1,76 +1,87 @@
|
|
|
|
|
package backend;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
// Importing windowInterface for graphical user interface
|
|
|
|
|
import windowInterface.MyInterface;
|
|
|
|
|
|
|
|
|
|
// Class for simulation
|
|
|
|
|
public class Simulator extends Thread {
|
|
|
|
|
|
|
|
|
|
// Interface object
|
|
|
|
|
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;
|
|
|
|
|
// Constants for grid dimensions and other parameters
|
|
|
|
|
private final int COL_NUM = 100; // Number of columns in the grid
|
|
|
|
|
private final int LINE_NUM = 100; // Number of lines in the grid
|
|
|
|
|
private final int LIFE_TYPE_NUM = 4; // Number of life types
|
|
|
|
|
private final int LIFE_AREA_RADIUS = 1; // Radius for life area
|
|
|
|
|
private final int ANIMAL_AREA_RADIUS = 2; // Radius for animal area
|
|
|
|
|
private ArrayList<Integer> fieldSurviveValues; // Array for survival values
|
|
|
|
|
private ArrayList<Integer> fieldBirthValues; // Array for birth values
|
|
|
|
|
|
|
|
|
|
private ArrayList<Agent> agents;
|
|
|
|
|
private ArrayList<Agent> agents; // List of agents
|
|
|
|
|
|
|
|
|
|
private boolean stopFlag;
|
|
|
|
|
private boolean pauseFlag;
|
|
|
|
|
private boolean loopingBorder;
|
|
|
|
|
private boolean clickActionFlag;
|
|
|
|
|
private int loopDelay = 150;
|
|
|
|
|
// Flags for simulation control
|
|
|
|
|
private boolean stopFlag; // Flag to stop simulation
|
|
|
|
|
private boolean pauseFlag; // Flag to pause simulation
|
|
|
|
|
private boolean loopingBorder; // Flag for looping border behavior
|
|
|
|
|
private boolean clickActionFlag; // Flag for click action
|
|
|
|
|
private int loopDelay = 150; // Delay between simulation steps
|
|
|
|
|
|
|
|
|
|
//TODO : add missing attribute(s)
|
|
|
|
|
// Additional attribute(s)
|
|
|
|
|
private double randomDansitySlider = 0.5; // Random density slider value
|
|
|
|
|
private int width; // Width of the grid
|
|
|
|
|
private int height; // Height of the grid
|
|
|
|
|
private Gride gride; // Grid object
|
|
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
|
public Simulator(MyInterface mjfParam) {
|
|
|
|
|
mjf = mjfParam;
|
|
|
|
|
stopFlag=false;
|
|
|
|
|
pauseFlag=false;
|
|
|
|
|
loopingBorder=false;
|
|
|
|
|
clickActionFlag=false;
|
|
|
|
|
// Initializing interface object and flags
|
|
|
|
|
mjf = mjfParam; // Interface object
|
|
|
|
|
stopFlag=false; // Initializing stop flag
|
|
|
|
|
pauseFlag=false; // Initializing pause flag
|
|
|
|
|
loopingBorder=false; // Initializing looping border flag
|
|
|
|
|
clickActionFlag=false; // Initializing click action flag
|
|
|
|
|
|
|
|
|
|
agents = new ArrayList<Agent>();
|
|
|
|
|
fieldBirthValues = new ArrayList<Integer>();
|
|
|
|
|
fieldSurviveValues = new ArrayList<Integer>();
|
|
|
|
|
agents = new ArrayList<Agent>(); // Initializing agents list
|
|
|
|
|
fieldBirthValues = new ArrayList<Integer>(); // Initializing birth values array
|
|
|
|
|
fieldSurviveValues = new ArrayList<Integer>(); // Initializing survival values array
|
|
|
|
|
|
|
|
|
|
//TODO : add missing attribute initialization
|
|
|
|
|
this.width=COL_NUM; // Setting width of the grid
|
|
|
|
|
this.height=LINE_NUM; // Setting height of the grid
|
|
|
|
|
gride = new Gride(height, width, this); // Creating grid object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Default rule : Survive always, birth never
|
|
|
|
|
// Initializing survival values array
|
|
|
|
|
for(int i =0; i<9; i++) {
|
|
|
|
|
fieldSurviveValues.add(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to get width of the grid
|
|
|
|
|
public int getWidth() {
|
|
|
|
|
//TODO : replace with proper return
|
|
|
|
|
return 0;
|
|
|
|
|
return this.width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to get height of the grid
|
|
|
|
|
public int getHeight() {
|
|
|
|
|
//TODO : replace with proper return
|
|
|
|
|
return 0;
|
|
|
|
|
return this.height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Should probably stay as is
|
|
|
|
|
// Method to run the simulation
|
|
|
|
|
public void run() {
|
|
|
|
|
// Initializing step count
|
|
|
|
|
int stepCount=0;
|
|
|
|
|
// Running simulation until stop flag is set
|
|
|
|
|
while(!stopFlag) {
|
|
|
|
|
stepCount++;
|
|
|
|
|
makeStep();
|
|
|
|
|
mjf.update(stepCount);
|
|
|
|
|
mjf.update(stepCount); // Updating interface
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(loopDelay);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
// Pausing simulation if pause flag is set
|
|
|
|
|
while(pauseFlag && !stopFlag) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(loopDelay);
|
|
|
|
|
@ -79,18 +90,11 @@ 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
|
|
|
|
|
*/
|
|
|
|
|
// Method to perform one step of simulation
|
|
|
|
|
public void makeStep() {
|
|
|
|
|
// agent behaviors first
|
|
|
|
|
// only modify if sure of what you do
|
|
|
|
|
// to modify agent behavior, see liveTurn method
|
|
|
|
|
// in agent classes
|
|
|
|
|
// Iterating through agents
|
|
|
|
|
for(Agent agent : agents) {
|
|
|
|
|
ArrayList<Agent> neighbors =
|
|
|
|
|
this.getNeighboringAnimals(
|
|
|
|
|
@ -103,73 +107,49 @@ public class Simulator extends Thread {
|
|
|
|
|
agents.remove(agent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//then evolution of the field
|
|
|
|
|
// TODO : apply game rule to all cells of the field
|
|
|
|
|
|
|
|
|
|
/* you should distribute this action in methods/classes
|
|
|
|
|
* don't write everything here !
|
|
|
|
|
*
|
|
|
|
|
* the idea is first to get the surrounding values
|
|
|
|
|
* then count how many are alive
|
|
|
|
|
* then check if that number is in the lists of rules
|
|
|
|
|
* if the cell is alive
|
|
|
|
|
* and the count is in the survive list,
|
|
|
|
|
* then the cell stays alive
|
|
|
|
|
* if the cell is not alive
|
|
|
|
|
* and the count is in the birth list,
|
|
|
|
|
* then the cell becomes alive
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.applyStep(); // Applying simulation step
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* leave this as is
|
|
|
|
|
*/
|
|
|
|
|
// Method to stop simulation
|
|
|
|
|
public void stopSimu() {
|
|
|
|
|
stopFlag=true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* method called when clicking pause button
|
|
|
|
|
*/
|
|
|
|
|
// Method to toggle pause
|
|
|
|
|
public void togglePause() {
|
|
|
|
|
// TODO : actually toggle the corresponding flag
|
|
|
|
|
pauseFlag = !pauseFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* method called when clicking on a cell in the interface
|
|
|
|
|
*/
|
|
|
|
|
// Method to handle click on cell
|
|
|
|
|
public void clickCell(int x, int y) {
|
|
|
|
|
//TODO : complete method
|
|
|
|
|
// Checking if click action flag is true
|
|
|
|
|
if (clickActionFlag == true) {
|
|
|
|
|
int currentCellValue = getCell(x, y);
|
|
|
|
|
int newCellValue = 0;
|
|
|
|
|
|
|
|
|
|
// Toggling cell value
|
|
|
|
|
if (currentCellValue == 0) {
|
|
|
|
|
newCellValue = 1;
|
|
|
|
|
} else {
|
|
|
|
|
newCellValue = 0;
|
|
|
|
|
}
|
|
|
|
|
this.setCell(x, y, newCellValue);
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get cell value in simulated world
|
|
|
|
|
* @param x coordinate of cell
|
|
|
|
|
* @param y coordinate of cell
|
|
|
|
|
* @return value of cell
|
|
|
|
|
*/
|
|
|
|
|
// Method to get cell value
|
|
|
|
|
public int getCell(int x, int y) {
|
|
|
|
|
//TODO : complete method with proper return
|
|
|
|
|
return 0;
|
|
|
|
|
return this.gride.getCell(x,y).getValue();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @return list of Animals in simulated world
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Method to get list of animals
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Method to get neighboring animals
|
|
|
|
|
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
|
|
|
|
|
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
|
|
|
|
for(int i=0;i<agents.size();i++) {
|
|
|
|
|
@ -181,36 +161,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
|
|
|
|
|
*/
|
|
|
|
|
// Method to set cell value
|
|
|
|
|
public void setCell(int x, int y, int val) {
|
|
|
|
|
//TODO : complete method
|
|
|
|
|
this.gride.getCell(x, y).setValue(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @return lines of file representing
|
|
|
|
|
* the simulated world in its present state
|
|
|
|
|
*/
|
|
|
|
|
// Method to count surrounding cells
|
|
|
|
|
public void countAround(int x, int y) {
|
|
|
|
|
this.gride.countAround(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to get save state
|
|
|
|
|
public ArrayList<String> getSaveState() {
|
|
|
|
|
//TODO : complete method with proper return
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param lines of file representing saved world state
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Method to load save state
|
|
|
|
|
public void loadSaveState(ArrayList<String> lines) {
|
|
|
|
|
/*
|
|
|
|
|
* First some checks that the file is usable
|
|
|
|
|
* We call early returns in conditions like this
|
|
|
|
|
* "Guard clauses", as they guard the method
|
|
|
|
|
* against unwanted inputs
|
|
|
|
|
*/
|
|
|
|
|
if(lines.size()<=0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@ -219,10 +186,6 @@ public class Simulator extends Thread {
|
|
|
|
|
if(firstLineElements.length<=0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* now we fill in the world
|
|
|
|
|
* with the content of the file
|
|
|
|
|
*/
|
|
|
|
|
for(int y =0; y<lines.size();y++) {
|
|
|
|
|
String line = lines.get(y);
|
|
|
|
|
String[] lineElements = line.split(";");
|
|
|
|
|
@ -234,99 +197,102 @@ 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
|
|
|
|
|
*/
|
|
|
|
|
// Method to generate random grid
|
|
|
|
|
public void generateRandom(float chanceOfLife) {
|
|
|
|
|
//TODO : complete method
|
|
|
|
|
/*
|
|
|
|
|
* Advice :
|
|
|
|
|
* as you should probably have a separate class
|
|
|
|
|
* representing the field of cells...
|
|
|
|
|
* maybe just make a constructor in there
|
|
|
|
|
* and use it here
|
|
|
|
|
*/
|
|
|
|
|
this.gride.setRandom(chanceOfLife);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to check looping border
|
|
|
|
|
public boolean isLoopingBorder() {
|
|
|
|
|
//TODO : complete method with proper return
|
|
|
|
|
return false;
|
|
|
|
|
return loopingBorder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to toggle looping border
|
|
|
|
|
public void toggleLoopingBorder() {
|
|
|
|
|
//TODO : complete method
|
|
|
|
|
|
|
|
|
|
loopingBorder = !loopingBorder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to set loop delay
|
|
|
|
|
public void setLoopDelay(int delay) {
|
|
|
|
|
//TODO : complete method
|
|
|
|
|
loopDelay = delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void toggleClickAction() {
|
|
|
|
|
//TODO : complete method
|
|
|
|
|
// Method to toggle click action
|
|
|
|
|
public void toggleClickAction() {
|
|
|
|
|
clickActionFlag = !clickActionFlag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to get rule
|
|
|
|
|
public ArrayList<String> getRule() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to load rule
|
|
|
|
|
public void loadRule(ArrayList<String> lines) {
|
|
|
|
|
if(lines.size()<=0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
|
|
|
|
//TODO : complete method with proper return
|
|
|
|
|
String surviveLine = lines.get(0);
|
|
|
|
|
String birthLine = lines.get(1);
|
|
|
|
|
String[] surviveElements = surviveLine.split(";");
|
|
|
|
|
for(int x=0; x<surviveElements.length;x++) {
|
|
|
|
|
String elem = surviveElements[x];
|
|
|
|
|
int value = Integer.parseInt(elem);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
String[] birthElements = birthLine.split(";");
|
|
|
|
|
for(int x=0; x<birthElements.length;x++) {
|
|
|
|
|
String elem = birthElements[x];
|
|
|
|
|
int value = Integer.parseInt(elem);
|
|
|
|
|
|
|
|
|
|
public void loadRule(ArrayList<String> lines) {
|
|
|
|
|
if(lines.size()<=0) {
|
|
|
|
|
System.out.println("empty rule file");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//TODO : remove previous rule (=emptying lists)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to apply simulation step
|
|
|
|
|
public void applyStep() {
|
|
|
|
|
Gride newGrideUpdated = new Gride(this.height, this.width, this);
|
|
|
|
|
for (int i = 0; i < this.height; i++) {
|
|
|
|
|
for (int j = 0; j < this.width; j++) {
|
|
|
|
|
int cellValue = this.gride.getCell(i, j).getValue();
|
|
|
|
|
|
|
|
|
|
String surviveLine = lines.get(0);
|
|
|
|
|
String birthLine = lines.get(1);
|
|
|
|
|
String[] surviveElements = surviveLine.split(";");
|
|
|
|
|
for(int x=0; x<surviveElements.length;x++) {
|
|
|
|
|
String elem = surviveElements[x];
|
|
|
|
|
int value = Integer.parseInt(elem);
|
|
|
|
|
//TODO : add value to possible survive values
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
String[] birthElements = birthLine.split(";");
|
|
|
|
|
for(int x=0; x<birthElements.length;x++) {
|
|
|
|
|
String elem = birthElements[x];
|
|
|
|
|
int value = Integer.parseInt(elem);
|
|
|
|
|
//TODO : add value to possible birth values
|
|
|
|
|
int newCellValue = cellValue;
|
|
|
|
|
int count = this.gride.countAround(i, j);
|
|
|
|
|
|
|
|
|
|
if (cellValue == 1) {
|
|
|
|
|
if (count == 2 || count == 3) {
|
|
|
|
|
newCellValue = 1;
|
|
|
|
|
} else {
|
|
|
|
|
newCellValue = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (count == 3) {
|
|
|
|
|
newCellValue = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
newGrideUpdated.setCell(i, j, new Cell(newCellValue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
gride = newGrideUpdated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ArrayList<String> getAgentsSave() {
|
|
|
|
|
//TODO : Same idea as the other save method, but for agents
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadAgents(ArrayList<String> stringArray) {
|
|
|
|
|
//TODO : Same idea as other load methods, but for agent list
|
|
|
|
|
// Method to get agents save
|
|
|
|
|
public ArrayList<String> getAgentsSave() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* used by label in interface to show the active click action
|
|
|
|
|
* @return String representation of click action
|
|
|
|
|
*/
|
|
|
|
|
public String clickActionName() {
|
|
|
|
|
// TODO : initially return "sheep" or "cell"
|
|
|
|
|
// depending on clickActionFlag
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
// Method to load agents
|
|
|
|
|
public void loadAgents(ArrayList<String> stringArray) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used by label in interface to show the active click action
|
|
|
|
|
* @return String representation of click action
|
|
|
|
|
*/
|
|
|
|
|
public String clickActionName() {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|