comment on simulator class

This commit is contained in:
virgi 2024-05-31 15:17:29 +02:00
parent 0a35b031e2
commit 6b8adf7278
1 changed files with 34 additions and 43 deletions

View File

@ -40,21 +40,17 @@ public class Simulator extends Thread {
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>();
//TODO : add missing attribute initialization
//Default rule : Survive always, birth never
for(int i =0; i<9; i++) {
fieldSurviveValues.add(i);
}
}
//method to get the width of the grid
public int getWidth() {
return COL_NUM;
}
// method to get the height of the grid
public int getHeight() {
return LINE_NUM;
}
@ -106,16 +102,20 @@ public class Simulator extends Thread {
}
ArrayList<String>frame= new ArrayList<String>();
String frameLine = "";
//This loop is to go through each row of the grid
for (int i=0;i<getWidth();i++) {
frameLine = "";
for (int j=0;j<getHeight();j++) {
// Check the cell's value and compare it to our rule
if (getCell(j,i) == 0 && fieldBirthValues.contains(currentWorld.getLiving(j, i, LIFE_AREA_RADIUS, loopingBorder))) {
// If the cell is dead and meets the birth conditions, set it to alive
frameLine += ("1;");
}
// If the cell is alive and meets the survival conditions, keep it alive
else if (getCell(j,i) == 1 && fieldSurviveValues.contains(currentWorld.getLiving(j, i, LIFE_AREA_RADIUS, loopingBorder))) {
frameLine += ("1;");
}
// Otherwise, the cell remains dead
else {
frameLine += ("0;");
}
@ -123,26 +123,6 @@ public class Simulator extends Thread {
frame.add(frameLine);
}
loadSaveState(frame);
//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
*/
}
/*
@ -167,18 +147,21 @@ public class Simulator extends Thread {
* method called when clicking on a cell in the interface
*/
public void clickCell(int x, int y) {
// Check the click action flag to know the the action to do
if (clickActionFlag ==0) {
if (getCell(x,y)==0) {
// manually set the state of the cell
if (getCell(x,y)==0) {// set alive
setCell(x,y,1);
}
else {
setCell(x,y,0);
setCell(x,y,0);// set dead
}
}
else if (clickActionFlag ==1) {
// To place engineer agent
agents.add(new Engineer(x,y));
}
// to place sheep on the grid
else if (clickActionFlag ==2) {
agents.add(new Sheep(x,y));
}
@ -235,8 +218,10 @@ public class Simulator extends Thread {
* the simulated world in its present state
*/
public ArrayList<String> getSaveState() {
// Initialize the ArrayList to store the grid's state
ArrayList<String> outputSaveState = new ArrayList<String>();
for (int i=0; i<getWidth(); i++) {
// Initialize an empty string to store the state of the current row
String lineSaveState = "";
for(int j=0; j<getHeight(); j++) {
lineSaveState += (getCell(i,j)+";");
@ -307,7 +292,7 @@ public class Simulator extends Thread {
public void setLoopDelay(int delay) {
loopDelay = delay;
}
// method to toggle the action to click between the different mode
public void toggleClickAction() {
if (clickActionFlag==2) {
clickActionFlag = 0;
@ -325,31 +310,36 @@ public class Simulator extends Thread {
* @see loadRule for inverse process
*/
public ArrayList<String> getRule() {
// Method to store a new rule
//first we deal with the rule setting
ArrayList<String> outputRule = new ArrayList<String>();
String rule = "";
for (int i:fieldBirthValues) {
rule += (i+";");
}
outputRule.add(rule);
outputRule.add(rule); // store the new rule setting
// know we need how a cell survive
rule = "";
for (int i:fieldSurviveValues) {
rule += (i+";");
}
outputRule.add(rule);
return outputRule;
outputRule.add(rule); // store the survival rule
return outputRule;// return the complete rule
}
//method to load a rule
public void loadRule(ArrayList<String> lines) {
if(lines.size()<=0) {
System.out.println("empty rule file");
return;
}
//clear the existang rule
fieldSurviveValues.clear();
fieldBirthValues.clear();
// Extract the survival and birth condition from the given lines
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];
@ -364,8 +354,9 @@ public class Simulator extends Thread {
fieldBirthValues.add(value);
}
}
//Method to save an agent
public ArrayList<String> getAgentsSave() {
//
ArrayList<String> agentSave = new ArrayList<String>();
for (Agent i:getAnimals()) {
agentSave.add(i.x+";"+i.y+";"+i.toString());
@ -374,7 +365,7 @@ public class Simulator extends Thread {
}
public void loadAgents(ArrayList<String> stringArray) {
//collect the information(type, coordate) of each agent
//collect the information(type, coordinate) of each agent
for(int i =0; i<stringArray.size();i++) {
String agentLine = stringArray.get(i);
String[] agentElements = agentLine.split(";");
@ -400,6 +391,7 @@ public class Simulator extends Thread {
* used by label in interface to show the active click action
* @return String representation of click action
*/
// TODO : initially return "sheep" or "cell"
public String clickActionName() {
if (clickActionFlag == 1) {
return "engineer";
@ -410,7 +402,6 @@ public class Simulator extends Thread {
else {
return "sheep";
}
// TODO : initially return "sheep" or "cell"
}
}