working first step
This commit is contained in:
parent
9a3912fc81
commit
21950423b8
|
|
@ -0,0 +1,32 @@
|
|||
package backend;
|
||||
|
||||
public class Cell {
|
||||
int posX;
|
||||
int posY;
|
||||
int state;
|
||||
|
||||
public Cell(int x, int y, int lifeState) {
|
||||
posX = x;
|
||||
posY = y;
|
||||
state = lifeState;
|
||||
}
|
||||
/*
|
||||
* Change the state of the cell rolling over it's different states
|
||||
*/
|
||||
public void toggleCell() {
|
||||
state++;
|
||||
if (state>5) {
|
||||
state =0;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Set a new state for the cell
|
||||
*/
|
||||
public void setState(int lifeState) {
|
||||
state = lifeState;
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package backend;
|
||||
|
||||
public class Grid {
|
||||
private Cell[][] table;
|
||||
private int width;
|
||||
private int height;
|
||||
public Grid(int sizeX, int sizeY) {
|
||||
width = sizeX;
|
||||
height = sizeY;
|
||||
table = new Cell[width][height];
|
||||
|
||||
for (int x=0; x < width;x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
table[x][y] = new Cell(x,y,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Cell getCell(int x, int y) {
|
||||
return table[x][y];
|
||||
}
|
||||
|
||||
public void setCell(int x, int y, int value) {
|
||||
table[x][y].setState(value);
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ public class Simulator extends Thread {
|
|||
private boolean loopingBorder;
|
||||
private boolean clickActionFlag;
|
||||
private int loopDelay = 150;
|
||||
private Grid grid;
|
||||
|
||||
//TODO : add missing attribute(s)
|
||||
|
||||
|
|
@ -37,7 +38,7 @@ public class Simulator extends Thread {
|
|||
agents = new ArrayList<Agent>();
|
||||
fieldBirthValues = new ArrayList<Integer>();
|
||||
fieldSurviveValues = new ArrayList<Integer>();
|
||||
|
||||
grid = new Grid(60, 60);
|
||||
//TODO : add missing attribute initialization
|
||||
|
||||
|
||||
|
|
@ -50,13 +51,12 @@ public class Simulator extends Thread {
|
|||
}
|
||||
|
||||
public int getWidth() {
|
||||
//TODO : replace with proper return
|
||||
return 0;
|
||||
return grid.getWidth();
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
//TODO : replace with proper return
|
||||
return 0;
|
||||
return grid.getHeight();
|
||||
}
|
||||
|
||||
//Should probably stay as is
|
||||
|
|
@ -119,10 +119,61 @@ public class Simulator extends Thread {
|
|||
* and the count is in the birth list,
|
||||
* then the cell becomes alive
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
Grid nextGrid = new Grid(60,60);
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
int actualState = getCell(x,y);
|
||||
int futureState =0;
|
||||
// high life
|
||||
if(actualState==0) {
|
||||
for (int i = 1; i<=5; i++) {
|
||||
if(checkNeighbors(x,y,i)==3||checkNeighbors(x,y,i)==6) {
|
||||
futureState = i;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(checkNeighbors(x,y,actualState)==3||checkNeighbors(x,y,actualState)==2) {
|
||||
futureState = actualState;
|
||||
}
|
||||
}
|
||||
|
||||
nextGrid.setCell(x,y,futureState);
|
||||
}
|
||||
}
|
||||
grid = nextGrid;
|
||||
}
|
||||
|
||||
private int checkNeighbors(int x, int y, int state) {
|
||||
int nbNeighbors = 0;
|
||||
|
||||
for (int i = x - 1; i <= x + 1; i++) {
|
||||
for (int j = y - 1; j <= y + 1; j++) {
|
||||
int x_looped = i;
|
||||
int y_looped = j;
|
||||
|
||||
if (loopingBorder) {
|
||||
if (x_looped < 0) {
|
||||
x_looped = getWidth() - 1;
|
||||
} else if (x_looped >= getWidth()) {
|
||||
x_looped = 0;
|
||||
}
|
||||
if (y_looped < 0) {
|
||||
y_looped = getHeight() - 1;
|
||||
} else if (y_looped >= getHeight()) {
|
||||
y_looped = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) {
|
||||
if (state == grid.getCell(x_looped, y_looped).getState()) {
|
||||
nbNeighbors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(nbNeighbors);
|
||||
return nbNeighbors;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -136,14 +187,14 @@ public class Simulator extends Thread {
|
|||
* method called when clicking pause button
|
||||
*/
|
||||
public void togglePause() {
|
||||
// TODO : actually toggle the corresponding flag
|
||||
pauseFlag = !pauseFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
* method called when clicking on a cell in the interface
|
||||
*/
|
||||
public void clickCell(int x, int y) {
|
||||
//TODO : complete method
|
||||
grid.getCell(x, y).toggleCell();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -153,8 +204,7 @@ public class Simulator extends Thread {
|
|||
* @return value of cell
|
||||
*/
|
||||
public int getCell(int x, int y) {
|
||||
//TODO : complete method with proper return
|
||||
return 0;
|
||||
return grid.getCell(x, y).getState();
|
||||
}
|
||||
/**
|
||||
*
|
||||
|
|
@ -188,7 +238,7 @@ public class Simulator extends Thread {
|
|||
* @param val to set in cell
|
||||
*/
|
||||
public void setCell(int x, int y, int val) {
|
||||
//TODO : complete method
|
||||
grid.getCell(x, y).setState(val);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -249,20 +299,27 @@ public class Simulator extends Thread {
|
|||
* maybe just make a constructor in there
|
||||
* and use it here
|
||||
*/
|
||||
for (int y = 0; y < getHeight(); y++) {
|
||||
for (int x = 0; x < getWidth(); x++) {
|
||||
if (Math.random() < chanceOfLife) {
|
||||
setCell(x, y, 1);
|
||||
} else {
|
||||
setCell(x, y, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLoopingBorder() {
|
||||
//TODO : complete method with proper return
|
||||
return false;
|
||||
return loopingBorder;
|
||||
}
|
||||
|
||||
public void toggleLoopingBorder() {
|
||||
//TODO : complete method
|
||||
|
||||
loopingBorder = !loopingBorder;
|
||||
}
|
||||
|
||||
public void setLoopDelay(int delay) {
|
||||
//TODO : complete method
|
||||
loopDelay = delay;
|
||||
}
|
||||
|
||||
public void toggleClickAction() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue