modif makestep et loadrule
This commit is contained in:
parent
9dbbfdcaeb
commit
cd00a26119
|
|
@ -25,18 +25,9 @@ public class Simulator extends Thread {
|
|||
private boolean loopingBorder;
|
||||
private boolean clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
int[][] cells = initializeMatrix(100, 100, 0);
|
||||
int[][] cells = Cells.initializeMatrix(100, 100, 0);
|
||||
//TODO : add missing attribute(s)
|
||||
|
||||
public static int[][] initializeMatrix(int rows, int cols, int initialValue) {
|
||||
int[][] matrix = new int[rows][cols];
|
||||
for (int i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < cols; j++) {
|
||||
matrix[i][j] = initialValue;
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
public Simulator(MyInterface mjfParam) {
|
||||
mjf = mjfParam;
|
||||
stopFlag=false;
|
||||
|
|
@ -53,9 +44,10 @@ public class Simulator extends Thread {
|
|||
|
||||
|
||||
//Default rule : Survive always, birth never
|
||||
for(int i =0; i<9; i++) {
|
||||
fieldSurviveValues.add(i);
|
||||
}
|
||||
for(int i =0; i<9; i++) {
|
||||
fieldSurviveValues.add(i);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -105,12 +97,45 @@ public class Simulator extends Thread {
|
|||
agent.getX(),
|
||||
agent.getY(),
|
||||
ANIMAL_AREA_RADIUS);
|
||||
if(!agent.liveTurn(
|
||||
neighbors,
|
||||
this)) {
|
||||
if(!agent.liveTurn(neighbors,this)) {
|
||||
agents.remove(agent);
|
||||
}
|
||||
}
|
||||
int[][] newCells = Cells.initializeMatrix(100, 100, 0); //matrix of the future states of the cells to not disturb the counter
|
||||
int nbNeighborAlive = 0; //counter for alive cells
|
||||
for (int j=0; j< LINE_NUM;j++) { // test for each cells
|
||||
for (int i=0; i< COL_NUM;i++) {
|
||||
for (int a=-1; a<2;a++) { // test the surrounding cells
|
||||
for(int b = -1 ; b<2; b++) {
|
||||
if (i+a >0 && j+b >0 && i+a <100 && j+b <100) {
|
||||
if(cells[i+a][j+b] == 1) {
|
||||
nbNeighborAlive += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int aliveNextIteration = 0;
|
||||
if (cells[i][j] == 1) {
|
||||
nbNeighborAlive +=-1; //removes the actual cell that got counted
|
||||
for(int k =0; k<fieldSurviveValues.size();k++ ) {
|
||||
if (nbNeighborAlive == fieldSurviveValues.get(k)) {
|
||||
aliveNextIteration = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cells[i][j] == 0) {
|
||||
for(int k =0; k<fieldBirthValues.size();k++ ) {
|
||||
if (nbNeighborAlive == fieldBirthValues.get(k)) {
|
||||
aliveNextIteration = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
newCells[i][j] = aliveNextIteration;
|
||||
}
|
||||
}
|
||||
cells = newCells;
|
||||
}
|
||||
//then evolution of the field
|
||||
// TODO : apply game rule to all cells of the field
|
||||
|
||||
|
|
@ -131,7 +156,7 @@ public class Simulator extends Thread {
|
|||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* leave this as is
|
||||
|
|
@ -153,14 +178,13 @@ public class Simulator extends Thread {
|
|||
* method called when clicking on a cell in the interface
|
||||
*/
|
||||
public void clickCell(int x, int y) { //NOT SURE AT ALL
|
||||
if (clickActionFlag == true) {
|
||||
if ( cells[x][y] == 0){
|
||||
cells[x][y] = 0;
|
||||
}
|
||||
else {
|
||||
if (cells[x][y] == 0){
|
||||
cells[x][y] = 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cells[x][y] = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
/**
|
||||
|
|
@ -206,7 +230,7 @@ public class Simulator extends Thread {
|
|||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
this.cells[x][y] = val;
|
||||
cells[x][y] = val;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -217,19 +241,15 @@ public class Simulator extends Thread {
|
|||
public ArrayList<String> getSaveState() {
|
||||
ArrayList<String> saveState = new ArrayList<>();
|
||||
|
||||
// Iterate in each row of the world then puts the values of the row in a string
|
||||
// takes from each row of the world then puts the values of the row in a string
|
||||
for (int i = 0; i < LINE_NUM; i++) {
|
||||
StringBuilder line = new StringBuilder();
|
||||
|
||||
// Append each cell value in the row, separated by semicolons
|
||||
for (int j = 0; j < COL_NUM; j++) {
|
||||
line.append(cells[i][j]);
|
||||
if (j < COL_NUM - 1) {
|
||||
line.append(";"); // Add semicolon separator for each value except the last one
|
||||
}
|
||||
}
|
||||
|
||||
// Add the line to the saveState list
|
||||
saveState.add(line.toString());
|
||||
}
|
||||
|
||||
|
|
@ -307,7 +327,7 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
clickActionFlag = !clickActionFlag;
|
||||
clickActionFlag =! clickActionFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -319,35 +339,38 @@ public class Simulator extends Thread {
|
|||
* @see loadRule for inverse process
|
||||
*/
|
||||
public ArrayList<String> getRule() {
|
||||
//TODO : complete method with proper return
|
||||
|
||||
return null;
|
||||
///FaLSE ArrayList<String> lines = new ArrayList<String>();
|
||||
fieldSurviveValues.add(csv.read)
|
||||
fieldSurviveValues = csv.read
|
||||
return lines
|
||||
}
|
||||
|
||||
public void loadRule(ArrayList<String> lines) {
|
||||
if(lines.size()<=0) {
|
||||
System.out.println("empty rule file");
|
||||
return;
|
||||
}
|
||||
//TODO : remove previous rule (=emptying lists)
|
||||
|
||||
|
||||
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];
|
||||
int value = Integer.parseInt(elem);
|
||||
//TODO : add value to possible survive values
|
||||
|
||||
}
|
||||
String[] birthElements = birthLine.split(";");
|
||||
for(int x=0; x<birthElements.length;x++) {
|
||||
String elem = birthElements[x];
|
||||
int value = Integer.parseInt(elem);
|
||||
//TODO : add value to possible birth values
|
||||
|
||||
}
|
||||
if (lines.size() <= 0) {
|
||||
fieldSurviveValues.clear();
|
||||
fieldBirthValues.clear();
|
||||
System.out.println("empty rule file");
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear previous rules
|
||||
fieldSurviveValues.clear();
|
||||
fieldBirthValues.clear();
|
||||
|
||||
// Process the survive line
|
||||
String surviveLine = lines.get(0);
|
||||
String[] surviveElements = surviveLine.split(";");
|
||||
for (String elem : surviveElements) {
|
||||
int value = Integer.parseInt(elem);
|
||||
fieldSurviveValues.add(value);
|
||||
}
|
||||
|
||||
// Process the birth line
|
||||
String birthLine = lines.get(1);
|
||||
String[] birthElements = birthLine.split(";");
|
||||
for (String elem : birthElements) {
|
||||
int value = Integer.parseInt(elem);
|
||||
fieldBirthValues.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<String> getAgentsSave() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue