Advertisement
Guest User

conway.py

a guest
Jul 29th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. from time import sleep
  2. from random import random
  3.  
  4. def main():
  5.     printIntro()
  6.     rows, cols, gens = getInputs()
  7.     thisGen = initGame(rows, cols)
  8.     nextGen = initGame(rows, cols)
  9.     for gen in range(gens): #I meant to have it clear the screen each time
  10.         printGen(rows, cols, thisGen) # but I couldn't get os.system('cls')
  11.         calcNext(rows, cols, thisGen, nextGen) #to work right.  
  12.         thisGen = nextGen[:]
  13.         sleep(.1)
  14.         print()
  15.     if input("Type 'again' to try again.  <return> to close. ") == "again":
  16.         main()
  17.    
  18.  
  19. def printIntro():
  20.     print("Conway's Game of Life")
  21.     print()
  22.     print("Conway's Game of Life is a cellular automaton.")
  23.     print("There's a grid of cells that act according to rules.")
  24.     print("If a cell is alive and has 2 or 3 neighbors (in the surrounding 8) it survives.")
  25.     print("If a cell is dead and has 3 neighbors it lives.")
  26.     print("All other cells are dead or die.")
  27.     print()
  28.  
  29. def getInputs():
  30.     try:
  31.         rows = eval(input("How many rows tall? 'return' for 20 "))
  32.     except SyntaxError:
  33.         rows = 20
  34.     try:
  35.         cols = eval(input("How many columns wide? 'return' for 40 "))
  36.     except SyntaxError:
  37.         cols = 40
  38.     try:
  39.         gens = eval(input("How many generations would you like to simulate? 'return' for 100 "))
  40.     except SyntaxError:
  41.         gens = 100
  42.     return rows, cols, gens
  43.  
  44. def initGame(rows, cols): # makes a randomized grid
  45.     array = []
  46.     for r in range(rows):
  47.         row = []
  48.         for c in range(cols):
  49.             if (r == 0 or c == 0 or r == rows - 1 or c == cols - 1):
  50.                 row += [-1] #establish a boundary that won't change
  51.             else:
  52.                 rand = random()
  53.                 if rand < .33:
  54.                     row += [1]
  55.                 else:
  56.                     row += [0]
  57.         array.append(row)
  58.     return array
  59.  
  60. def printGen(rows, cols, thisGen):
  61.    
  62.     for r in range(rows):
  63.         for c in range(cols):
  64.             if r ==(0 or rows - 1):
  65.                 print("-", end="") # top and bottom boundary
  66.             elif c ==(0):
  67.                 print("|", end="") # left boundary
  68.             elif c == (cols - 1):
  69.                 print("|") #different line so it'd end with \n
  70.             elif thisGen[r][c] == 1:
  71.                 print("█", end="")
  72.             else:
  73.                 print("░", end="") # because I wanted more than just a " "
  74.  
  75.  
  76. def calcNext(rows, cols, thisGen, nextGen):
  77.     for r in range(1, rows - 1):
  78.         for c in range(1, cols-1):
  79.             nextGen[r][c] = getState(r, c, thisGen)
  80.  
  81. def getState(x, y, gen):
  82.     count = 0 #number of neighbors
  83.     for r in range(x-1, x+2):
  84.         for c in range(y-1, y + 2):
  85.             if gen[r][c] != -1:
  86.                 count += gen[r][c]
  87.     if count == 3:
  88.         return 1
  89.     elif count == 4:
  90.         return gen[x][y]    
  91.     else:
  92.         return 0
  93.     # I got this idea from the wikipedia page for Conway's Game of Life, under "algorithms"
  94.                
  95.        
  96.  
  97.  
  98. if __name__ == "__main__":
  99.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement