I have recently been trying to create Conway's game of life algorithm within python using pygame and changing arrays. However, I have come across a problem where my 3d array will detect elements within the 3d array that become True, that should in reality be false. The issue seems to be somewhere within the 3 functions / loop listed below. If you test the code below the output should be a blinker that switches between a horizontal and vertical line repeatedly.
Full Code:
from pygame.locals import FULLSCREEN
from pygame.locals import DOUBLEBUF
import pygame
import random
import time
pygame.init()
grid_x = 20
grid_y = 20
BLACK = (0,0,0)
GREEN = (20,255,0)
root = pygame.display.set_mode((0,0), FULLSCREEN | DOUBLEBUF)
screen_width, screen_height = root.get_size()
pixelsize = screen_width/grid_x
life_array = [[[0 for x in range(grid_x)] for y in range(grid_y)] for i in range(2)]
life_array[0][3][3] = True
life_array[0][4][3] = True
life_array[0][5][3] = True
def get_adjacent_cells(y,x):
adjacent_cells = 0
if y == 0:
pass
elif x == 0:
pass
elif y == grid_y-1:
pass
elif x == grid_x-1:
pass
else:
if life_array[0][y-1][x-1] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print("-,-")
if life_array[0][y][x-1] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print(",-")
if life_array[0][y+1][x-1] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print("+,-")
if life_array[0][y+1][x] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print("+,")
if life_array[0][y+1][x+1] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print("+,+")
if life_array[0][y][x+1] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print(",+")
if life_array[0][y-1][x+1] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print("-,+")
if life_array[0][y-1][x] and adjacent_cells < 4:
adjacent_cells = adjacent_cells + 1
if x == 4 and y==3:
print("-,")
return adjacent_cells
#print(adjacent_cells)
'''
if x == 3 and y==3:
print(adjacent_cells, ",{},{}".format(x,y))
#print(x-1)
#print(life_array[0][y][x-1], " should be false")
'''
def change_cell(y,x,adjacent_cells):
if cell_alive:
if adjacent_cells == 2 or adjacent_cells == 3:
life_array[1][y][x] = True
print("STIll alive,{},{}".format(x,y))
else:
life_array[1][y][x] = 0
print("dies,{},{}".format(x,y))
else:
if adjacent_cells == 3:
life_array[1][y][x] = True
print("realive,{},{}".format(x,y))
else:
life_array[1][y][x] = 0
#print("still dead")
def check_colour(cell_alive):
if cell_alive:
colour = GREEN
else:
colour = BLACK
return colour
def check_cell(y,x):
adjacent_cells = 0
if life_array[0][y][x]:
cell_alive = True
else:
cell_alive = False
return cell_alive
'''
for y in range(grid_y):
for x in range(grid_x):
life_array[array][y][x] = bool(random.getrandbits(1))
'''
print(life_array[0])
while True:
pygame.display.update()
root.fill(0)
time.sleep(3)
for y in range(grid_y):
for x in range(grid_x):
colour = check_colour(life_array[0][y][x])
#print(colour)
y0 = (y*pixelsize)
x0 = (x*pixelsize)
y1 = ((y*pixelsize) + pixelsize)
x1 = ((x*pixelsize) + pixelsize)
pygame.draw.rect(root,colour,(x0,y0,x1,y1))
cell_alive = check_cell(y,x)
adjacent_cells = get_adjacent_cells(y,x)
change_cell(y,x,adjacent_cells)
life_array[0] = life_array[1]
print(life_array[0])
pygame.quit()
Any Help would be much appreciated!