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"?>
<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>
<attribute name="module" value="true"/>
</attributes>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>OOP_D8_Project</name>
<name>OOP</name>
<comment></comment>
<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;

View File

@ -7,8 +7,9 @@ public class Simulator extends Thread {
private MyInterface mjf;
private final int COL_NUM = 10;
private final int LINE_NUM = 10;
//Size of the grid
private final int COL_NUM = 100;
private final int LINE_NUM = 100;
private final int LIFE_TYPE_NUM = 4;
private final int LIFE_AREA_RADIUS = 1;
private final int ANIMAL_AREA_RADIUS = 2;
@ -29,6 +30,9 @@ public class Simulator extends Thread {
private int height;
private Gride gride;
//Step put here to be reset when generate new field
private int stepCount=0;
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag=false;
@ -37,19 +41,20 @@ public class Simulator extends Thread {
clickActionFlag=false;
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.height=LINE_NUM;
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() {
int stepCount=0;
//int stepCount=0;
System.out.println("Step Count: "+ stepCount);
while(!stopFlag) {
stepCount++;
@ -122,6 +127,7 @@ public class Simulator extends Thread {
}
this.setCell(x, y, newCellValue);
} else {
//Apply sheep /agent here
return;
}
}
@ -151,11 +157,21 @@ public class Simulator extends Thread {
public void countAround(int x, int y) {
this.gride.countAround(x, y);
System.out.println("countAround");
}
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) {
@ -180,6 +196,9 @@ public class Simulator extends Thread {
public void generateRandom(float chanceOfLife) {
this.gride.setRandom(chanceOfLife);
//Reset the steps
stepCount = 0;
this.mjf.update(stepCount);
}
public boolean isLoopingBorder() {
@ -199,7 +218,28 @@ public class Simulator extends Thread {
}
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) {
@ -210,17 +250,26 @@ public class Simulator extends Thread {
String surviveLine = lines.get(0);
String birthLine = lines.get(1);
String[] surviveElements = surviveLine.split(";");
//Load the values to survive
for(int x=0; x<surviveElements.length;x++) {
String elem = surviveElements[x];
int value = Integer.parseInt(elem);
this.fieldSurviveValues.add(value);
}
//Load the values given birth
String[] birthElements = birthLine.split(";");
for(int x=0; x<birthElements.length;x++) {
String elem = birthElements[x];
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);
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;
} else {
newCellValue = 0;
}
} else {
if (count == 3) {
//Check that count is one of the value making generate a cell
if (this.fieldBirthValues.contains(count)) {
newCellValue = 1;
}
}
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;
@ -255,6 +306,12 @@ public class Simulator extends Thread {
public ArrayList<String> getAgentsSave() {
//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;
}
@ -270,7 +327,13 @@ public class Simulator extends Thread {
public String clickActionName() {
// TODO : initially return "sheep" or "cell"
// 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() {
this.instantiateSimu();
if(!mySimu.isAlive()) {
mySimu.start();
} else {
mySimu.togglePause();