This commit is contained in:
Guillaume LE CHARTIER 2024-05-29 14:30:06 +02:00
commit e501b7feb6
4 changed files with 77 additions and 56 deletions

View File

@ -38,7 +38,7 @@ public class Sheep extends Agent {
hunger++; hunger++;
} }
this.moveRandom(); this.moveRandom();
return hunger>10; return hunger<10; //condition to be alive
} }
private void moveRandom() { private void moveRandom() {

View File

@ -31,7 +31,7 @@ public class Simulator extends Thread {
private boolean stopFlag; private boolean stopFlag;
private boolean pauseFlag; private boolean pauseFlag;
private boolean loopingBorder; private boolean loopingBorder;
private boolean clickActionFlag; private int clickActionFlag;
private int loopDelay = 150; private int loopDelay = 150;
//TODO : add missing attribute(s) //TODO : add missing attribute(s)
@ -40,7 +40,9 @@ public class Simulator extends Thread {
private int height; private int height;
private boolean enableLogs; private boolean enableLogs;
private Table table; private Table table;
private boolean cellDensityToggle; private int lowestCellValue = 0;
private int highestCellValue = 0;
private int ruleArrayListLength = 0;
//Rules Arraylists //Rules Arraylists
private ArrayList<Rule> ruleArrayList = new ArrayList<Rule>(); private ArrayList<Rule> ruleArrayList = new ArrayList<Rule>();
@ -52,7 +54,7 @@ public class Simulator extends Thread {
stopFlag=false; stopFlag=false;
pauseFlag=false; pauseFlag=false;
loopingBorder=false; loopingBorder=false;
clickActionFlag=false; clickActionFlag=1; //1 for cell, 2 for sheep, 3 for wolf
agents = new ArrayList<Agent>(); agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>(); fieldBirthValues = new ArrayList<Integer>();
@ -64,12 +66,11 @@ public class Simulator extends Thread {
this.height=LINE_NUM; this.height=LINE_NUM;
enableLogs = true; // for debugging purposes enableLogs = true; // for debugging purposes
table = new Table(height, width, this); table = new Table(height, width, this);
cellDensityToggle=true;
//Default rule : Survive always, birth never //Default rule : Survive always, birth never
loadRule("ressources\\Rule\\conwayRule.json"); loadRule("ressources/Rule/conwayRule.json");
} }
@ -124,17 +125,19 @@ public class Simulator extends Thread {
// only modify if sure of what you do // only modify if sure of what you do
// to modify agent behavior, see liveTurn method // to modify agent behavior, see liveTurn method
// in agent classes // in agent classes
for(Agent agent : agents) { /*for(Agent agent : agents) {
ArrayList<Agent> neighbors = ArrayList<Agent> neighbors =
this.getNeighboringAnimals( this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
agent.getX(), if(!agent.liveTurn(neighbors,this)) {
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(
neighbors,
this)) {
agents.remove(agent); agents.remove(agent);
} }
}*/
for(int i=0; i<agents.size(); i++){
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agents.get(i).getX(), agents.get(i).getY(), ANIMAL_AREA_RADIUS);
if(!agents.get(i).liveTurn(neighbors,this)) {
agents.remove(i);
}
} }
//then evolution of the field //then evolution of the field
//TODO-INPROGRESS : apply game rule to all cells of the field //TODO-INPROGRESS : apply game rule to all cells of the field
@ -186,32 +189,42 @@ 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) { //ruleArrayList
if (ruleArrayListLength == 0 && highestCellValue == 0){
int ruleArrayListLength = ruleArrayList.size();
for (int i = 0; i < ruleArrayListLength; i++) {
if (ruleArrayList.get(i).getValue() > highestCellValue) {
highestCellValue = ruleArrayList.get(i).getValue();
}
}
int lowestCellValue = 0;
for (int i = 0; i < ruleArrayListLength; i++) {
if (ruleArrayList.get(i).getValue() < lowestCellValue) {
lowestCellValue = ruleArrayList.get(i).getValue();
}
}
System.out.println("ruleArrayListLength: " + ruleArrayListLength);
System.out.println("highestCellValue: " + highestCellValue);
System.out.println("lowestCellValue: " + lowestCellValue);
}
int currentCellValue = getCell(x, y); int currentCellValue = getCell(x, y);
//TODO : find highest value in ruleArrayList
int newCellValue = 0; int newCellValue = 0;
if(cellDensityToggle) { if(clickActionFlag == 1) { //cell
if (currentCellValue <6) { if (currentCellValue != highestCellValue) {
newCellValue = currentCellValue +1; newCellValue = currentCellValue +1;
} else {
newCellValue=-1;
}
} else {
if (currentCellValue == 0) {
newCellValue = 1;
} else {
newCellValue = 0;
}
}
if (enableLogs) {
System.out.println("clickCell Called, cell :" + x + "," + y + " is now" + newCellValue + "");
}
this.setCell(x, y, newCellValue);
} }
else { else {
newCellValue = lowestCellValue;
}
this.setCell(x, y, newCellValue);
}
else if(clickActionFlag == 2) { //sheep
int i=0; int i=0;
Agent agent = new Sheep(x,y); Agent agent = new Sheep(x,y);
@ -248,7 +261,13 @@ public class Simulator extends Thread {
System.out.println("clickAgent Called, Agent created at: " + x + "," + y + ""); System.out.println("clickAgent Called, Agent created at: " + x + "," + y + "");
} }
} }
}
else if(clickActionFlag == 3) { //wolf
//Make agent sheep/wolf
return;
}
if (enableLogs) {
System.out.println("clickCell Called, cell :" + x + "," + y + " is now" + newCellValue + "");
} }
} }
@ -423,13 +442,13 @@ public class Simulator extends Thread {
public void toggleClickAction() { public void toggleClickAction() {
//TODO-COMPLETE : complete method //TODO-COMPLETE : complete method
clickActionFlag = !clickActionFlag; if (clickActionFlag != 3) {
if (enableLogs) { clickActionFlag++;
if (clickActionFlag) {
System.out.println("toggleClickAction called, set clickActionFlag to true");
} else { } else {
System.out.println("toggleClickAction called, set clickActionFlag to false"); clickActionFlag = 1;
} }
if (enableLogs) {
System.out.println("toggleClickAction called, set clickActionFlag to : " + clickActionFlag);
} }
} }
@ -472,6 +491,9 @@ public class Simulator extends Thread {
} }
//DEBUG //DEBUG
//printRules(ruleArrayList); //printRules(ruleArrayList);
lowestCellValue = 0;
highestCellValue = 0;
ruleArrayListLength = 0;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -578,22 +600,19 @@ public class Simulator extends Thread {
* @return String representation of click action * @return String representation of click action
*/ */
public String clickActionName() { public String clickActionName() {
// TODO-COMPLETE : initially return "sheep" or "cell" if (clickActionFlag == 1){
// depending on clickActionFlag
if (clickActionFlag){
return "cell"; return "cell";
} }
else { else if (clickActionFlag == 2){
return "sheep"; return "sheep";
} }
else if (clickActionFlag == 3){
return "wolf";
}
else {
return "error";
}
} }
//debug print the list of rules //debug print the list of rules
public void printRules(ArrayList<Rule> ruleArrayList) { public void printRules(ArrayList<Rule> ruleArrayList) {

View File

@ -8,7 +8,7 @@ public class Table {
private ArrayList<ArrayList<Cell>> table; private ArrayList<ArrayList<Cell>> table;
private Simulator simulator; private Simulator simulator;
//TODO-INPROGRESS : create constructor //TODO-COMPLETE : create constructor
public Table(int height, int width, Simulator tempSimulator) { public Table(int height, int width, Simulator tempSimulator) {
this.height = height; this.height = height;
this.width = width; this.width = width;

View File

@ -210,6 +210,8 @@ public class MyInterface extends JFrame {
mySimu = new Simulator(this); mySimu = new Simulator(this);
panelDraw.setSimu(mySimu); panelDraw.setSimu(mySimu);
panelDraw.repaint(); panelDraw.repaint();
randSlider.setValue(50);
speedSlider.setValue(30);
} }
public void setStepBanner(String s) { public void setStepBanner(String s) {