asweigart

conway.py

Jun 1st, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # Conway's Game of Life
  2. import random, time, copy
  3. WIDTH = 60
  4. HEIGHT = 20
  5.  
  6. # Create a list of list for the cells:
  7. nextCells = []
  8. for x in range(WIDTH):
  9.     column = [] # Create a new column.
  10.     for y in range(HEIGHT):
  11.         if random.randint(0, 1) == 0:
  12.             column.append('#') # Add a living cell.
  13.         else:
  14.             column.append(' ') # Add a dead cell.
  15.     nextCells.append(column) # nextCells is a list of column lists.
  16.  
  17. while True: # Main program loop.
  18.     print('\n\n\n\n\n') # Separate each step with newlines.
  19.     currentCells = copy.deepcopy(nextCells)
  20.     # Print currentCells on the screen:
  21.     for y in range(HEIGHT):
  22.         for x in range(WIDTH):
  23.             print(currentCells[x][y], end='') # Print the # or space.
  24.         print() # Print a newline at the end of the row.
  25.  
  26.     # Calculate the next step's cells based on current step's cells:
  27.     for x in range(WIDTH):
  28.         for y in range(HEIGHT):
  29.             # Get neighboring coordinates:
  30.             # `% WIDTH` ensures leftCoord is always between 0 and WIDTH - 1
  31.             leftCoord  = (x - 1) % WIDTH
  32.             rightCoord = (x + 1) % WIDTH
  33.             aboveCoord = (y - 1) % HEIGHT
  34.             belowCoord = (y + 1) % HEIGHT
  35.  
  36.             # Count number of living neighbors:
  37.             numNeighbors = 0
  38.             if currentCells[leftCoord][aboveCoord] == '#':
  39.                 numNeighbors += 1 # Top left neighbor is alive.
  40.             if currentCells[x][aboveCoord] == '#':
  41.                 numNeighbors += 1 # Top neighbor is alive.
  42.             if currentCells[rightCoord][aboveCoord] == '#':
  43.                 numNeighbors += 1 # Top right neighbor is alive.
  44.             if currentCells[leftCoord][y] == '#':
  45.                 numNeighbors += 1 # Left neighbor is alive.
  46.             if currentCells[rightCoord][y] == '#':
  47.                 numNeighbors += 1 # Right neighbor is alive.
  48.             if currentCells[leftCoord][belowCoord] == '#':
  49.                 numNeighbors += 1 # Bottom left neighbor is alive.
  50.             if currentCells[x][belowCoord] == '#':
  51.                 numNeighbors += 1 # Bottom neighbor is alive.
  52.             if currentCells[rightCoord][belowCoord] == '#':
  53.                 numNeighbors += 1 # Bottom right neighbor is alive.
  54.  
  55.             # Set cell based on Conway's Game of Life rules:
  56.             if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
  57.                 # Living cells with 2 or 3 neighbors stay alive:
  58.                 nextCells[x][y] = '#'
  59.             elif currentCells[x][y] == ' ' and numNeighbors == 3:
  60.                 # Dead cells with 3 neighbors become alive:
  61.                 nextCells[x][y] = '#'
  62.             else:
  63.                 # Everything else dies or stays dead:
  64.                 nextCells[x][y] = ' '
  65.     time.sleep(1) # Add a 1 second pause to reduce flickering.
Add Comment
Please, Sign In to add comment