Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time, os
- w = 20
- h = 20
- world = [ [0]*h for i in range(w) ]
- world[0][1] = 1
- world[1][2] = 1
- world[2][2] = 1
- world[2][1] = 1
- world[2][0] = 1
- def get_cell(world, w, h, i, j):
- i %= w
- j %= h
- return world[i][j]
- def draw(world, w):
- os.system('cls')
- for i in range(w):
- print(''.join(str(v) for v in world[i]))
- def step(world, w, h):
- new_world = [ row[:] for row in world ]
- draw(world, w)
- time.sleep(1)
- neighbours = [None]*8
- for i in range(w):
- for j in range(h):
- neighbours[0] = get_cell(world, w, h, i - 1, j - 1)
- neighbours[1] = get_cell(world, w, h, i, j - 1)
- neighbours[2] = get_cell(world, w, h, i + 1, j - 1)
- neighbours[3] = get_cell(world, w, h, i - 1, j)
- neighbours[4] = get_cell(world, w, h, i + 1, j)
- neighbours[5] = get_cell(world, w, h, i - 1, j + 1)
- neighbours[6] = get_cell(world, w, h, i, j + 1)
- neighbours[7] = get_cell(world, w, h, i + 1, j + 1)
- neighbours_count = sum(neighbours)
- if world[i][j] == 1 and (neighbours_count < 2 or neighbours_count > 3):
- new_world[i][j] = 0
- elif world[i][j] == 0 and neighbours_count == 3:
- new_world[i][j] = 1
- return new_world
- while True:
- world = step(world, w, h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement