rendered paste bodypublic class GenericRule implements GameRule {
private Colony next;
private boolean[][] neighbours;
private boolean[] live;
private boolean[] dead;
public GenericRule() {
neighbours = new boolean[3][3];
this.fillArray(neighbours);
live = new boolean[] {false,false,true,true,false,false,false,false,false};
dead = new boolean[] {false,false,false,true,false,false,false,false,false};
}
private void fillArray(boolean[][] arr) {
for (int i = 0; i<arr.length; i++) {
for (int j = 0; j<arr[0].length; j++) {
arr[i][j] = true;
}
}
}
@Override
public boolean nextState(Colony col, int y, int x) {
int n = this.countNeighbours(col, y, x);
boolean lives = col.getCell(y, x);
if (lives) {
return live[n];
} else {
return dead[n];
}
}
public int countNeighbours(Colony col, int y, int x) {
int n = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if ((isNeighbour(i, j))&&(col.getCell(i+y, j+x))) {
n++;
}
}
}
return n;
}
private boolean willLive(boolean alive, int liveNeigbours) {
if (alive && (liveNeigbours == 2 || liveNeigbours == 3)) {
return true;
} else if ((!alive) && (liveNeigbours == 3)) {
return true;
} else {
return false;
}
}
public void setSurvive(int liveNeighbours, boolean alive) {
live[liveNeighbours] = alive;
}
public void setBirth(int liveNeighbours, boolean alive) {
dead[liveNeighbours] = alive;
}
public void setNeighbour(int oppned, int venstrehoyre, boolean countThis) {
if ((oppned == 0)&&(venstrehoyre == 0)) return;
neighbours[1+oppned][1+venstrehoyre] = countThis;
}
public boolean isNeighbour(int on, int vh) {
if ((on == 0)&&(vh == 0)) return false;
return neighbours[1+on][1+vh];
}
}