Advertisement
DMG

Conway's Game of Life

DMG
May 28th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import os, time, copy
  2.  
  3. def printBoard():
  4.     time.sleep(1)
  5.     os.system('cls')
  6.     for i in xrange(n):
  7.         for j in xrange(n):
  8.             if board[i][j] == 0:
  9.                 print " ",
  10.             else:
  11.                 print "X",
  12.         print
  13.  
  14. def countLiveNeighbours(x, y):
  15.     s = 0
  16.     if board[x % n][(y + 1) % n] == 1:
  17.         s = s + 1
  18.     if board[x % n][(y - 1) % n] == 1:
  19.         s = s + 1
  20.     if board[(x - 1) % n][(y + 1) % n] == 1:
  21.         s = s + 1
  22.     if board[(x - 1) % n][y % n] == 1:
  23.         s = s + 1
  24.     if board[(x - 1) % n][(y - 1) % n] == 1:
  25.         s = s + 1
  26.     if board[(x + 1) % n][(y + 1) % n] == 1:
  27.         s = s + 1
  28.     if board[(x + 1) % n][y % n] == 1:
  29.         s = s + 1
  30.     if board[(x + 1) % n][(y - 1) % n] == 1:
  31.         s = s + 1
  32.        
  33.     return s
  34.    
  35.  
  36. n = int(raw_input(" Unesite dimenzije table: "))
  37.  
  38. board = [[0 for x in xrange(n)] for x in xrange(n)]
  39. copyBoard = copy.deepcopy(board)
  40.  
  41. while True:
  42.     i = int(raw_input(" x: "))
  43.     if i == -1:
  44.         break
  45.     j = int(raw_input(" y: "))
  46.     board[i][j] = 1
  47.     copyBoard[i][j] = 1
  48.  
  49. printBoard()
  50.  
  51. while True:
  52.     for i in xrange(n):
  53.         for j in xrange(n):
  54.             # print countLiveNeighbours(i, j)
  55.             if countLiveNeighbours(i, j) < 2:
  56.                 copyBoard[i][j] = 0
  57.             elif countLiveNeighbours(i, j) > 3:
  58.                 copyBoard[i][j] = 0
  59.             elif (countLiveNeighbours(i, j) == 3 and board[i][j] == 0):
  60.                 copyBoard[i][j] = 1
  61.            
  62.     board = copy.deepcopy(copyBoard)
  63.            
  64.     printBoard()
  65.     #break 
  66.  
  67. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement