johnmahugu

python game of life

Jun 3rd, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. import random
  2. import time
  3. import os
  4.  
  5. #---------------------------------------------------------------------------
  6.  
  7. def initGrid(cols, rows, array):
  8.     for i in range(rows):
  9.         arrayRow = []
  10.         for j in range(cols):
  11.             if (i == 0 or j == 0 or (i == rows - 1) or (j == cols - 1)):
  12.                 arrayRow += [-1]
  13.             else:
  14.                 ran = random.randint(0,3)
  15.                 if ran == 0:
  16.                     arrayRow += [1]
  17.                 else:
  18.                     arrayRow += [0]
  19.         array += [arrayRow]
  20.  
  21. #---------------------------------------------------------------------------
  22.    
  23. def printGen(cols, rows, array, genNo):
  24.     os.system("cls")
  25.  
  26.     print("Game of Life -- Generation " + str(genNo + 1))
  27.    
  28.     for i in range(rows):
  29.         for j in range(cols):
  30.             if array[i][j] == -1:
  31.                 print("#", end=" ")
  32.             elif array[i][j] == 1:
  33.                 print(".", end=" ")
  34.             else:
  35.                 print(" ", end=" ")
  36.         print("\n")
  37.  
  38. #---------------------------------------------------------------------------
  39.  
  40. def processNextGen(cols, rows, cur, nxt):
  41.     for i in range(1,rows-1):
  42.         for j in range(1,cols-1):
  43.             nxt[i][j] = processNeighbours(i, j, cur)
  44.  
  45. #---------------------------------------------------------------------------
  46.      
  47. def processNeighbours(x, y, array):
  48.     nCount = 0
  49.     for j in range(y-1,y+2):
  50.         for i in range(x-1,x+2):
  51.             if not(i == x and j == y):
  52.                 if array[i][j] != -1:
  53.                     nCount += array[i][j]
  54.     if array[x][y] == 1 and nCount < 2:
  55.         return 0
  56.     if array[x][y] == 1 and nCount > 3:
  57.         return 0
  58.     if array[x][y] == 0 and nCount == 3:
  59.         return 1
  60.     else:
  61.         return array[x][y]
  62.  
  63. #---------------------------------------------------------------------------
  64. ############################################################################
  65. #---------------------------------------------------------------------------
  66.  
  67. ROWS = 11
  68. COLS = 39
  69. GENERATIONS = 100
  70. DELAY = 0.2
  71.  
  72. thisGen = []
  73. nextGen = []
  74.  
  75. initGrid(COLS, ROWS, thisGen)
  76. initGrid(COLS, ROWS, nextGen)
  77.  
  78. for gens in range(GENERATIONS):
  79.     printGen(COLS, ROWS, thisGen, gens)
  80.     processNextGen(COLS, ROWS, thisGen, nextGen)
  81.     time.sleep(DELAY)
  82.     thisGen, nextGen = nextGen, thisGen
  83. input("Finished. Press <return> to quit.")# python game of life
Advertisement
Add Comment
Please, Sign In to add comment