Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import time, os
  2.  
  3. w = 20
  4. h = 20
  5.  
  6. world = [ [0]*h for i in range(w) ]
  7.  
  8. world[0][1] = 1
  9. world[1][2] = 1
  10. world[2][2] = 1
  11. world[2][1] = 1
  12. world[2][0] = 1
  13.  
  14. def get_cell(world, w, h, i, j):
  15.     i %= w
  16.     j %= h
  17.     return world[i][j]
  18.  
  19. def draw(world, w):
  20.     os.system('cls')
  21.     for i in range(w):
  22.         print(''.join(str(v) for v in world[i]))
  23.    
  24. def step(world, w, h):
  25.     new_world = [ row[:] for row in world ]
  26.     draw(world, w)
  27.     time.sleep(1)
  28.     neighbours = [None]*8
  29.     for i in range(w):
  30.         for j in range(h):
  31.             neighbours[0] = get_cell(world, w, h, i - 1, j - 1)
  32.             neighbours[1] = get_cell(world, w, h, i, j - 1)
  33.             neighbours[2] = get_cell(world, w, h, i + 1, j - 1)
  34.             neighbours[3] = get_cell(world, w, h, i - 1, j)
  35.             neighbours[4] = get_cell(world, w, h, i + 1, j)
  36.             neighbours[5] = get_cell(world, w, h, i - 1, j + 1)
  37.             neighbours[6] = get_cell(world, w, h, i, j + 1)
  38.             neighbours[7] = get_cell(world, w, h, i + 1, j + 1)
  39.  
  40.             neighbours_count = sum(neighbours)
  41.  
  42.             if world[i][j] == 1 and (neighbours_count < 2 or neighbours_count > 3):
  43.                 new_world[i][j] = 0
  44.             elif world[i][j] == 0 and neighbours_count == 3:
  45.                 new_world[i][j] = 1
  46.     return new_world
  47.  
  48. while True:
  49.     world = step(world, w, h)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement