Alright so I’ve been working on the game of life and have run into a weird problem. There are squares deciding to draw alive for an unknown reason. I have used print to check what the state is at those steps and I know it is not the state being wrong.
`
public int gridSize = 50;
public float cellSize = 10;
public float spacing = 10;
public Cell[][] cells = new Cell[gridSize][gridSize];
public int startingCells = 3;
public boolean startPlaced = false;
public int timeBetweenTicks = 5;
public boolean isBetweenTicks = false;
public int startTime;
public int tickCount = 0;
public boolean gameActive = false;
void setup(){
background(19, 19, 19);
size(501, 600);
for(int col=0; col<gridSize; col++){
for(int row=0; row<gridSize; row++){
cells[col][row] = new Cell(col, row, 0);
}
}
}
void draw(){
clear();
fill(0, 200, 0);
text("Current Wait: ", 50, 550);
fill(0, 200, 0);
text("Ticks Since Start: ", 50, 570);
DrawSquares();
if(gameActive && !isBetweenTicks){
//cells[0][0].SetState(1);
if(!startPlaced){
SetStartingCells();
}
if(tickCount >1){
UpdateCells();
}
delay(1000);
startTime = second();
isBetweenTicks = true;
//delay(timeBetweenTicks * 1000);
}
HandleTime();
}
void UpdateCells(){
for(int col=0; col<gridSize; col++){
for(int row=0; row<gridSize; row++){
cells[col][row].UpdateState();
}
}
}
void SetStartingCells(){
for(int i=0; i<startingCells; i++){
int colum = (int)random(cells.length);
int row = (int)random(cells[0].length);
cells[colum][row].SetState(1);
}
startPlaced = true;
}
void HandleTime(){
if(isBetweenTicks){
int elapsedTime = second() - startTime;
//println(elapsedTime);
fill(0, 200, 0);
text(elapsedTime, 150, 550);
fill(0, 200, 0);
text(tickCount, 180, 570);
if(elapsedTime == timeBetweenTicks){
isBetweenTicks = false;
tickCount++;
elapsedTime = 0;
}else if(elapsedTime < 0){
startTime = second();
elapsedTime = second() - startTime;
}
}
}
void DrawSquares(){
for(int col=0; col<gridSize; col++){
for(int row=0; row<gridSize; row++){
cells[col][row].DrawSquare();
}
}
}
void keyReleased(){
if(key == ‘s’){
//println(“S has been Released”);
gameActive = true;
}else if(key == ‘p’){
gameActive = false;
}
}
class Cell{
//col, row
int gridX, gridY;
//1 = alive, 0 = dead
int cellState = 0;
int aliveNeighbours = 0;
Cell(int gridPosX, int gridPosY, int state){
gridX = gridPosX;
gridY = gridPosY;
cellState = state;
}
void DrawSquare(){
stroke(0, 255, 0);
if(cellState == 0){
//dead
fill(0);
}else if (cellState == 1){
//alive
fill(255);
}
rect(gridX * spacing, gridY * spacing, cellSize, cellSize);
}
void SetState(int newState){
cellState = newState;
}
int GetState(){
return cellState;
}
void UpdateState(){
if(gridX < gridSize-1 && gridY < gridSize-1){
if(gridX > 0 && gridY > 0){
ArrayList<Cell> neighbours = new ArrayList<Cell>();
//println(gridX + " , " + gridY);
//top row
neighbours.add(cells[gridX - 1][gridY + 1]);
neighbours.add(cells[gridX][gridY + 1]);
neighbours.add(cells[gridX + 1][gridY + 1]);
//middle row
neighbours.add(cells[gridX - 1][gridY]);
//Cell mid2 = cells[gridX][gridY]; Ignoring ourself
neighbours.add(cells[gridX + 1][gridY + 1]);
//bottom row
neighbours.add(cells[gridX - 1][gridY + 1]);
neighbours.add(cells[gridX][gridY + 1]);
neighbours.add(cells[gridX + 1][gridY + 1]);
for(int i=0; i<neighbours.size(); i++){
Cell curCell = neighbours.get(i);
if(curCell.GetState() == 1){
aliveNeighbours++;
}
}
boolean survived = false;
if(cellState == 1){
if(aliveNeighbours == 2 || aliveNeighbours == 3){
survived = true;
//println("survived");
}
}else{
if(aliveNeighbours == 3){
survived = true;
//println("survived");
}
}
if(survived){
SetState(1);
//println("state set to 1");
}else{
SetState(0);
}
//println(aliveNeighbours + ", State = " + cellState);
aliveNeighbours = 0;
}
}
}
}
`
Im a little lost and wouldn’t mind a helping hand