Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Created November 15, 2013 20:29
Show Gist options
  • Select an option

  • Save Orpheon/7491038 to your computer and use it in GitHub Desktop.

Select an option

Save Orpheon/7491038 to your computer and use it in GitHub Desktop.
from __future__ import division, print_function
def is_alive(x, y, board):
neighbours = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
neighbours += board[(y+j)%8][(x+i)%8]
# The value of the point itself is not important, and should be removed
neighbours -= board[y][x]
if (2 <= neighbours <= 3 and board[y][x]) or neighbours == 3:
return 1
else:
return 0
def draw(board):
# Draw
print("\n\n --------")
for y in range(8):
tmp = ""
for x in range(8):
if board[y][x]:
tmp += "X"
else:
tmp += " "
print("|"+tmp+"|")
print(" --------")
raw_input();
# Main
new_board = [[0 for i in range(8)] for j in range(8)]
board = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]
draw(board)
while True:
for y in range(8):
for x in range(8):
new_board[y][x] = is_alive(x, y, board)
for y in range(8):
board[y] = new_board[y][:]
draw(board)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment