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

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