Advertisement
Guest User

broken game of life

a guest
Jun 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. old_grid = [[0 for x in range(9)] for y in range(9)]
  5.  
  6. # for i in range(0, 9):
  7. #     for j in range(0, 9):
  8. #         old_grid[i][j] = random.randrange(0, 2)
  9. old_grid[5][5] = 1
  10. old_grid[5][6] = 1
  11. old_grid[5][7] = 1
  12. new_grid = old_grid.copy()
  13. print(old_grid)
  14. def wrapindex(i, i_max):
  15.     return (i_max + (i % i_max)) % i_max
  16.  
  17. while 1:
  18.     for x in range(0, 9):
  19.         for y in range(0, 9):
  20.             neighbors = old_grid[wrapindex(x-1, 9)][wrapindex(y+1, 9)] +       old_grid[wrapindex(x+1, 9)][wrapindex(y+1, 9)] + old_grid[wrapindex(x+1, 9)][wrapindex(y+ 1, 9)] + \
  21.                         old_grid[wrapindex(x-1, 9)][wrapindex(y, 9)]   +  0  + old_grid[wrapindex(x+1, 9)][wrapindex(y, 9)]   + \
  22.                         old_grid[wrapindex(x-1, 9)][wrapindex(y-1, 9)] +       old_grid[wrapindex(x+1, 9)][wrapindex(y-1, 9)] + old_grid[wrapindex(x+1, 9)][wrapindex(y-1, 9)]
  23.  
  24.             if neighbors == 3 and new_grid[x][y]== 0 :
  25.                 new_grid[x][y] = 1
  26.             elif neighbors == 2 and new_grid[x][y]==1 :
  27.                 new_grid[x][y] = 1
  28.             elif neighbors > 3:
  29.                 new_grid[x][y] = 0
  30.             elif neighbors < 2:
  31.                 new_grid[x][y] = 0
  32.             print(new_grid[x][y], end='')
  33.         time.sleep(0.25)
  34.         print()
  35.     temp = old_grid[x][y]
  36.     old_grid = new_grid.copy()
  37.     print('\n')
  38.     time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement