Project_V1

This commit is contained in:
Paul MATHY 2024-05-31 21:10:09 +02:00
parent f438d20c2c
commit ed239ade7e
5 changed files with 109 additions and 34 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
</attributes> </attributes>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>OOP_D8_Project</name> <name>OOP</name>
<comment></comment> <comment></comment>
<projects> <projects>
</projects> </projects>

View File

@ -1,3 +1,14 @@
/*
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("toto");
}
}*/
import windowInterface.MyInterface; import windowInterface.MyInterface;
@ -9,4 +20,4 @@ public class Main {
mjf.setVisible(true); mjf.setVisible(true);
} }
} }

View File

@ -7,8 +7,9 @@ public class Simulator extends Thread {
private MyInterface mjf; private MyInterface mjf;
private final int COL_NUM = 10; //Size of the grid
private final int LINE_NUM = 10; private final int COL_NUM = 100;
private final int LINE_NUM = 100;
private final int LIFE_TYPE_NUM = 4; private final int LIFE_TYPE_NUM = 4;
private final int LIFE_AREA_RADIUS = 1; private final int LIFE_AREA_RADIUS = 1;
private final int ANIMAL_AREA_RADIUS = 2; private final int ANIMAL_AREA_RADIUS = 2;
@ -29,6 +30,9 @@ public class Simulator extends Thread {
private int height; private int height;
private Gride gride; private Gride gride;
//Step put here to be reset when generate new field
private int stepCount=0;
public Simulator(MyInterface mjfParam) { public Simulator(MyInterface mjfParam) {
mjf = mjfParam; mjf = mjfParam;
stopFlag=false; stopFlag=false;
@ -37,19 +41,20 @@ public class Simulator extends Thread {
clickActionFlag=false; clickActionFlag=false;
agents = new ArrayList<Agent>(); agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>();
fieldSurviveValues = new ArrayList<Integer>(); fieldSurviveValues = new ArrayList<Integer>();
fieldBirthValues = new ArrayList<Integer>();
this.width=COL_NUM; this.width=COL_NUM;
this.height=LINE_NUM; this.height=LINE_NUM;
gride = new Gride(height, width, this); gride = new Gride(height, width, this);
// Conway rules use at initialization stage
this.fieldSurviveValues.add(2);
this.fieldSurviveValues.add(3);
this.fieldBirthValues.add(3);
for(int i =0; i<9; i++) {
fieldSurviveValues.add(i);
}
} }
@ -62,7 +67,7 @@ public class Simulator extends Thread {
} }
public void run() { public void run() {
int stepCount=0; //int stepCount=0;
System.out.println("Step Count: "+ stepCount); System.out.println("Step Count: "+ stepCount);
while(!stopFlag) { while(!stopFlag) {
stepCount++; stepCount++;
@ -84,23 +89,23 @@ public class Simulator extends Thread {
} }
public void makeStep() { public void makeStep() {
for(Agent agent : agents) { for(Agent agent : agents) {
ArrayList<Agent> neighbors = ArrayList<Agent> neighbors =
this.getNeighboringAnimals( this.getNeighboringAnimals(
agent.getX(), agent.getX(),
agent.getY(), agent.getY(),
ANIMAL_AREA_RADIUS); ANIMAL_AREA_RADIUS);
if(!agent.liveTurn( if(!agent.liveTurn(
neighbors, neighbors,
this)) { this)) {
agents.remove(agent); agents.remove(agent);
}
} }
this.applyStep();
} }
this.applyStep();
}
public void stopSimu() { public void stopSimu() {
stopFlag=true; stopFlag=true;
@ -122,6 +127,7 @@ public class Simulator extends Thread {
} }
this.setCell(x, y, newCellValue); this.setCell(x, y, newCellValue);
} else { } else {
//Apply sheep /agent here
return; return;
} }
} }
@ -151,11 +157,21 @@ public class Simulator extends Thread {
public void countAround(int x, int y) { public void countAround(int x, int y) {
this.gride.countAround(x, y); this.gride.countAround(x, y);
System.out.println("countAround");
} }
public ArrayList<String> getSaveState() { public ArrayList<String> getSaveState() {
return null; ArrayList<String> strArrayList = new ArrayList<>();
String[] strArrayLine = new String[this.width];
//Store the state of the GRID
for(int x=0 ;x<this.width; x++) {
for(int y=0 ;y<this.width; y++) {
strArrayLine[y] = Integer.toString(getCell(y, x));
}
System.out.println(String.join(";", strArrayLine));
strArrayList.add(String.join(";", strArrayLine));
}
return strArrayList;
} }
public void loadSaveState(ArrayList<String> lines) { public void loadSaveState(ArrayList<String> lines) {
@ -180,6 +196,9 @@ public class Simulator extends Thread {
public void generateRandom(float chanceOfLife) { public void generateRandom(float chanceOfLife) {
this.gride.setRandom(chanceOfLife); this.gride.setRandom(chanceOfLife);
//Reset the steps
stepCount = 0;
this.mjf.update(stepCount);
} }
public boolean isLoopingBorder() { public boolean isLoopingBorder() {
@ -198,8 +217,29 @@ public class Simulator extends Thread {
clickActionFlag = !clickActionFlag; clickActionFlag = !clickActionFlag;
} }
public ArrayList<String> getRule() { public ArrayList<String> getRule() {
return null;
ArrayList<String> strArrayList = new ArrayList<>();
String[] strArrayLine = new String[this.fieldSurviveValues.size()];
//Add survive values
for(int x=0; x<this.fieldSurviveValues.size();x++) {
strArrayLine[x] = Integer.toString(this.fieldSurviveValues.get(x));
}
strArrayList.add(String.join(";", strArrayLine));
//New array for Birth values
strArrayLine = new String[this.fieldBirthValues.size()];
//Add born values
for(int x=0; x<this.fieldBirthValues.size();x++) {
strArrayLine[x] = Integer.toString(this.fieldBirthValues.get(x));
}
strArrayList.add(String.join(";", strArrayLine));
return strArrayList;
} }
public void loadRule(ArrayList<String> lines) { public void loadRule(ArrayList<String> lines) {
@ -210,17 +250,26 @@ public class Simulator extends Thread {
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(";");
//Load the values to survive
for(int x=0; x<surviveElements.length;x++) { for(int x=0; x<surviveElements.length;x++) {
String elem = surviveElements[x]; String elem = surviveElements[x];
int value = Integer.parseInt(elem); int value = Integer.parseInt(elem);
this.fieldSurviveValues.add(value);
} }
//Load the values given birth
String[] birthElements = birthLine.split(";"); String[] birthElements = birthLine.split(";");
for(int x=0; x<birthElements.length;x++) { for(int x=0; x<birthElements.length;x++) {
String elem = birthElements[x]; String elem = birthElements[x];
int value = Integer.parseInt(elem); int value = Integer.parseInt(elem);
this.fieldBirthValues.add(value);
} }
} }
@ -235,18 +284,20 @@ public class Simulator extends Thread {
int count = this.gride.countAround(i, j); int count = this.gride.countAround(i, j);
if (cellValue == 1) { if (cellValue == 1) {
if (count == 2 || count == 3) { //Check that count is one of the value making survive the cell
if (this.fieldSurviveValues.contains(count)) {
newCellValue = 1; newCellValue = 1;
} else { } else {
newCellValue = 0; newCellValue = 0;
} }
} else { } else {
if (count == 3) { //Check that count is one of the value making generate a cell
if (this.fieldBirthValues.contains(count)) {
newCellValue = 1; newCellValue = 1;
} }
} }
newGrideUpdated.setCell(i, j, new Cell(newCellValue)); newGrideUpdated.setCell(i, j, new Cell(newCellValue));
System.out.println("applyStep called : " + newGrideUpdated.getCell(i, j).getValue() + " at " + i + " " + j); //System.out.println("applyStep called : " + newGrideUpdated.getCell(i, j).getValue() + " at " + i + " " + j);
} }
} }
gride = newGrideUpdated; gride = newGrideUpdated;
@ -255,6 +306,12 @@ public class Simulator extends Thread {
public ArrayList<String> getAgentsSave() { public ArrayList<String> getAgentsSave() {
//TODO : Same idea as the other save method, but for agents //TODO : Same idea as the other save method, but for agents
//Marche pas car pas d'agent :-(
System.out.println("AGENTS " + agents);
for( Agent agent : agents ){
System.out.println( agent.getX() + "," + agent.getY());
}
return null; return null;
} }
@ -270,7 +327,13 @@ public class Simulator extends Thread {
public String clickActionName() { public String clickActionName() {
// TODO : initially return "sheep" or "cell" // TODO : initially return "sheep" or "cell"
// depending on clickActionFlag // depending on clickActionFlag
return "";
if (clickActionFlag) {
return "cell";
} else {
return "sheep";
}
} }
} }

View File

@ -218,6 +218,7 @@ public class MyInterface extends JFrame {
public void clicButtonGo() { public void clicButtonGo() {
this.instantiateSimu(); this.instantiateSimu();
if(!mySimu.isAlive()) { if(!mySimu.isAlive()) {
mySimu.start(); mySimu.start();
} else { } else {
mySimu.togglePause(); mySimu.togglePause();