Advertisement
brandonmunda1

Conways

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