public static void step(){
int x,y,offsetX,offsetY,row,colm;
int red=0 ,green = 0;
Color tempCol;// = Color.gray;
cell [][] newGrid = new cell[size][size]; //temporary array of cells
for(x = 0; x < size;x++){//row in grid being examined
for(y = 0; y < size; y++){//column in grid being examined
for(offsetX=-1; offsetX<2;offsetX++ ){//offsets are -1 to 1 so in the case of x, left centre and right
for(offsetY=-1; offsetY<2; offsetY++){
if (!(offsetX==0 && offsetY==0)){//not the square being examined
row = x + offsetX;//row is the row of the surrounding cell being examined
if (row == -1){//catch array out of bounds
row = 39;
} else if (row == 40) {
row = 0;
}
colm = y + offsetY;//colm is the column of the surrounding cell being examined
if (colm == -1){//catch array out of bounds
colm = 39;
} else if (colm == 40) {
colm = 0;
}
tempCol = grid[row][colm].getBackground(); //tempCol refers to the colour of the surrounding cell
if(tempCol == Color.red){//if it is red increment the amout of reds
red++;
}
if(tempCol == Color.green){//if it is green increment the greens
green++;
}
if(red>green){//of there are more reds
switch(red){//how many reds
case 2 : //if tehre are 2 keep the colour the same
newGrid[x][y] = new cell(grid[x][y].getBackground());
break;
case 3 ://if there are 3 create a cell
newGrid[x][y] = new cell(Color.red);
default: //else cell dies and is gray
newGrid[x][y] = new cell(Color.gray);
break;
}
}else if (green>red){//if there are more greens
switch(green){
case 2 :
newGrid[x][y] = new cell(grid[x][y].getBackground());
break;
case 3 :
newGrid[x][y] = new cell(Color.green);
default:
newGrid[x][y] = new cell(Color.gray);
break;
}
}else{//if tehre are equal amounts keep the cell the same colour
newGrid[x][y] = new cell(grid[x][y].getBackground());
}
}
}
}
}
}//update the grid
for(x = 0; x < size;x++){
for(y = 0; y < size; y++){
grid[x][y].setBackground(newGrid[x][y].getBackground());
}
}
}