Skip to content

Instantly share code, notes, and snippets.

@farhanhubble
Created July 28, 2020 14:49
Show Gist options
  • Save farhanhubble/cadae1055c46d4fd735b428bb1772b22 to your computer and use it in GitHub Desktop.
Save farhanhubble/cadae1055c46d4fd735b428bb1772b22 to your computer and use it in GitHub Desktop.
Gol game
world = [['▢', '▣', '▢'],
['▢', '▣', '▢'],
['▢', '▣', '▢']]
## Code to update the world one step.
nb_rows = len(world)
nb_cols = len(world[0])
for rowid in range(nb_rows):
for colid in range(nb_cols):
print(world[rowid][colid]+' ', end='')
print('')
print('\n\n')
tmp_world = [x[:] for x in world]
for row in range(nb_rows):
for col in range(nb_cols):
# Count neighbors
neighbour_row_idx = [row-1, row, row+1]
neighbour_col_idx = [col-1, col, col+1]
neighbor_positions = [(r,c) for r in neighbour_row_idx for c in neighbour_col_idx if not (r==row and c==col) and 0 <= r < nb_rows and 0 <= c < nb_cols]
nb_alive_neighbors = 0
for row_neighbor,col_neighbor in neighbor_positions:
if world[row_neighbor][col_neighbor] == '▣':
nb_alive_neighbors += 1
if world[row][col] == '▢':
if nb_alive_neighbors == 3:
tmp_world[row][col] = '▣'
else:
if nb_alive_neighbors < 2 or nb_alive_neighbors > 3:
tmp_world[row][col] = '▢'
world = tmp_world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment