finally working correctly, however not optimized at all yet
This commit is contained in:
parent
64a3e59afe
commit
778ae63f4e
|
|
@ -2,8 +2,11 @@ package backend;
|
|||
|
||||
public class Cell {
|
||||
int alive;
|
||||
private Simulator mySimu;
|
||||
//private Simulator mySimu;
|
||||
|
||||
public Cell() {
|
||||
|
||||
}
|
||||
public Cell(int alive) {
|
||||
this.alive = alive;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@ package backend;
|
|||
import java.util.ArrayList;
|
||||
|
||||
import windowInterface.MyInterface;
|
||||
//import backend.Rules;
|
||||
|
||||
public class Simulator extends Thread {
|
||||
|
||||
private MyInterface mjf;
|
||||
|
||||
//private Rules rules;
|
||||
|
||||
private final int COL_NUM = 100;
|
||||
private final int LINE_NUM = 100;
|
||||
private final int LIFE_TYPE_NUM = 4;
|
||||
//Conway Radius : 1
|
||||
private final int LIFE_AREA_RADIUS = 1;
|
||||
public final int LIFE_AREA_RADIUS = 1;
|
||||
//Animal Neighborhood Radius : 2
|
||||
private final int ANIMAL_AREA_RADIUS = 2;
|
||||
private ArrayList<Integer> fieldSurviveValues;
|
||||
|
|
@ -19,6 +22,8 @@ public class Simulator extends Thread {
|
|||
|
||||
private ArrayList<Agent> agents;
|
||||
private ArrayList<ArrayList<Cell>> cells;
|
||||
private ArrayList<ArrayList<Integer>> newCells;
|
||||
|
||||
private boolean stopFlag;
|
||||
private boolean pauseFlag;
|
||||
private boolean loopingBorder;
|
||||
|
|
@ -34,6 +39,8 @@ public class Simulator extends Thread {
|
|||
loopingBorder=false;
|
||||
clickActionFlag=false;
|
||||
cells = new ArrayList<ArrayList<Cell>>();
|
||||
newCells = new ArrayList<ArrayList<Integer>>();
|
||||
|
||||
agents = new ArrayList<Agent>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
fieldSurviveValues = new ArrayList<Integer>();
|
||||
|
|
@ -42,7 +49,7 @@ public class Simulator extends Thread {
|
|||
|
||||
//initialize grid with dead cells
|
||||
for(int x=0; x <= getWidth();x++) {
|
||||
ArrayList<Cell> arrayCell = new ArrayList<Cell>(); //initialise first dimension with ArrayLists
|
||||
ArrayList<Cell> arrayCell = new ArrayList<Cell>(); //initialize first dimension with ArrayLists
|
||||
cells.add(arrayCell);
|
||||
for(int y=0; y <= getHeight(); y++) {
|
||||
Cell cell = new Cell(0); //create a dead cell
|
||||
|
|
@ -50,6 +57,14 @@ public class Simulator extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
for(int x=0; x <= getWidth();x++) {
|
||||
ArrayList<Integer> arrayNewCell = new ArrayList<Integer>(); //initialize first dimension with ArrayLists
|
||||
newCells.add(arrayNewCell);
|
||||
for(int y=0; y <= getHeight(); y++) {
|
||||
newCells.get(x).add(0); //assign dead cell to its position in second dimension array
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Default rule : Survive always, birth never
|
||||
for(int i =0; i<9; i++) {
|
||||
|
|
@ -91,6 +106,45 @@ public class Simulator extends Thread {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//i put everything here for now
|
||||
public void cellDies(int x, int y) {
|
||||
int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS);
|
||||
|
||||
if(aliveNeighbors < 2 || aliveNeighbors > 3) {
|
||||
//setNewCell(x, y, 0);
|
||||
newCells.get(x).set(y, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void cellBorns(int x, int y) {
|
||||
int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS);
|
||||
|
||||
if(aliveNeighbors == 3) {
|
||||
//setNewCell(x, y, 1);
|
||||
newCells.get(x).set(y, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public int getAliveNeighbors(int x, int y, int radius) {
|
||||
int aliveNeighbors = -1;
|
||||
if(getCell(x,y) == 0) {
|
||||
aliveNeighbors++;
|
||||
}
|
||||
|
||||
|
||||
//for each neighbor
|
||||
for(int i = x-radius; i <= x+radius; i++) {
|
||||
for(int j = y-radius; j <= y+radius; j++) {
|
||||
if(getCell(i,j) == 1) { //if alive, add 1 to counter
|
||||
aliveNeighbors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return aliveNeighbors;
|
||||
}
|
||||
/**
|
||||
* method called at each step of the simulation
|
||||
* makes all the actions to go from one step to the other
|
||||
|
|
@ -129,6 +183,40 @@ public class Simulator extends Thread {
|
|||
* then the cell becomes alive
|
||||
*/
|
||||
|
||||
//calculate and set newCells based on cells
|
||||
//i started from 1 and ended < to ignore the borders
|
||||
for(int x=1; x < getWidth();x++) {
|
||||
for(int y=1; y < getHeight(); y++) {
|
||||
int status = getCell(x,y);
|
||||
|
||||
if(status == 1) {
|
||||
//cellDies(x, y);
|
||||
int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS);
|
||||
|
||||
if(aliveNeighbors < 2 || aliveNeighbors > 3) {
|
||||
//setNewCell(x, y, 0);
|
||||
newCells.get(x).set(y, 0);
|
||||
}else{newCells.get(x).set(y, status);}
|
||||
}else if(status == 0) {
|
||||
//cellBorns(x, y);
|
||||
int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS);
|
||||
|
||||
if(aliveNeighbors == 3) {
|
||||
//setNewCell(x, y, 1);
|
||||
newCells.get(x).set(y, 1);
|
||||
}else{newCells.get(x).set(y, status);}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//update cells
|
||||
for(int x=1; x < getWidth();x++) {
|
||||
for(int y=1; y < getHeight(); y++) {
|
||||
int status = getNewCell(x,y);
|
||||
setCell(x, y, status);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -162,6 +250,7 @@ public class Simulator extends Thread {
|
|||
if(clickActionFlag) {
|
||||
|
||||
}else {
|
||||
//System.out.println(getAliveNeighbors(x, y, LIFE_AREA_RADIUS)); //print nb of neighbours
|
||||
if(getCell(x, y)==0) {
|
||||
setCell(x, y, 1);
|
||||
}else {setCell(x, y, 0);}
|
||||
|
|
@ -180,6 +269,15 @@ public class Simulator extends Thread {
|
|||
int status = cells.get(x).get(y).getAlive();
|
||||
return status;
|
||||
}
|
||||
|
||||
public int getNewCell(int x, int y) {
|
||||
//TODO : complete method with proper return
|
||||
//int status = newCells.get(x).get(y).getAlive();
|
||||
int status = newCells.get(x).get(y);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return list of Animals in simulated world
|
||||
|
|
@ -212,10 +310,11 @@ public class Simulator extends Thread {
|
|||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int status) {
|
||||
//TODO : complete method
|
||||
cells.get(x).get(y).setAlive(status);
|
||||
|
||||
}
|
||||
/*public void setNewCell(int x, int y, int status) {
|
||||
newCells.get(x).get(y).setAlive(status);
|
||||
}*/
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue