Skip to content

Instantly share code, notes, and snippets.

@jmccardle
Created December 26, 2021 04:40
Show Gist options
  • Save jmccardle/699a22865da1a42e0b8b516abd3d82ed to your computer and use it in GitHub Desktop.
Save jmccardle/699a22865da1a42e0b8b516abd3d82ed to your computer and use it in GitHub Desktop.
import random, time, copy
def new_grid(width, height):
grid = []
for x in range(width):
column= []
for y in range(height):
column.append('#' if random.randint (0,1) else ' ')
grid.append(column)
return grid
def test_grid1():
return [[' ', ' ', ' '],
[' ', '#', ' '],
[' ', ' ', ' ']
]
def test_grid2():
return [[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', '#', ' ', ' '],
[' ', '#', ' ', '#', ' ', ' '],
[' ', ' ', '#', '#', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' '],
]
def display(grid):
"""Print a grid on screen with asterisk border"""
print("*" * (len(grid[0])+2))
for row in grid:
print("*", end='')
print(''.join(row), end='')
print("*")
print("*" * (len(grid[0])+2))
def display_neighbors(grid):
"""Show the neighbor count of each position"""
print("*" * (len(grid[0])+2))
g = copy.deepcopy(grid)
for y in range(len(grid)):
for x in range(len(grid[0])):
g[y][x] = str(count_neighbours(grid, x ,y))
for row in g:
print("*", end='')
print(''.join(row), end='')
print("*")
print("*" * (len(grid[0])+2))
def display_changes(grid):
"""Show 'A' for dead cells becoming alive, 'd' for live cells dying, and '#'/' ' for unchanged"""
g2 = step(grid)
print("*" * (len(grid[0])+2))
for y in range(len(grid)):
print('*', end='')
for x in range(len(grid[0])):
if grid[y][x] == g2[y][x]: print(grid[y][x], end='')
elif grid[y][x] == '#': print('d', end='')
else: print('A', end='')
print('*')
print("*" * (len(grid[0])+2))
def count_neighbours(grid, x,y):
neighbors = 0
for i in range(-1,1+1):
for j in range(-1,1+1):
nx = (x + i) % len(grid)
ny = (y + j) % len(grid[0])
#print(f"i: {i} j: {j} nx: {nx} ny: {ny} cell value: {grid[ny][nx]}")
if i == 0 and j == 0:
continue
if grid[ny][nx] == '#':
neighbors +=1
#print(f"neighbors: {neighbors}")
return neighbors
def game_of_life_rules(cell_value, neighbors):
##Any live cell with two or three live neighbours survives.
##Any dead cell with three live neighbours becomes a live cell.
##All other live cells die in the next generation. Similarly, all other dead cells stay dead.
if cell_value == '#' and neighbors in (2, 3): return '#'
elif cell_value == ' ' and neighbors == 3: return '#'
else: return ' '
def step(grid):
"""Return the successor grid according to Conway's Game of Life"""
g = copy.deepcopy(grid) # don't modify the list reference in place
for y in range(len(grid)):
for x in range(len(grid[0])):
# read from input array, "grid"
neighbors = count_neighbours(grid, x, y)
# write to output array, "g"
g[y][x] = game_of_life_rules(grid[y][x], neighbors)
return g
if __name__ == '__main__':
game_grid = test_grid2()
for i in range(20):
print(f"Generation {i}")
display(game_grid)
game_grid = step(game_grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment