Instantly share code, notes, and snippets.

# robinovitch61/game_of_life.py

Created September 25, 2019 18:49
Show Gist options
• Save robinovitch61/8a094c15f8cc28ec9218a1baf6b211c6 to your computer and use it in GitHub Desktop.
First pair programming at RC - Game of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import numpy as np from scipy.ndimage import convolve import matplotlib.pyplot as plt import matplotlib import copy # Setup BOARD_WIDTH = 10 BOARD_HEIGHT = 10 BOARD = np.zeros((BOARD_HEIGHT, BOARD_WIDTH)) # Randomly initialize board MIN_ALIVE_START = 25 MAX_ALIVE_START = 50 num_alive_start = np.random.randint(MIN_ALIVE_START, MAX_ALIVE_START+1) print("Initializing {} alive squares".format(num_alive_start)) kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]) # Assign alive cells to board for ii in range(num_alive_start): pos_x = np.random.randint(0, BOARD_WIDTH) pos_y = np.random.randint(0, BOARD_HEIGHT) BOARD[pos_y, pos_x] = 1 print("This is the initial board. Please close to see next generation.") print("\n") plt.imshow(BOARD) matplotlib.pyplot.show() while True: old_board = copy.copy(BOARD) # Add up the neighbours on the board convolved_board = convolve(BOARD, kernel, mode="constant", cval=0.0) # Rule 1: die if less than 2 BOARD[convolved_board < 2] = 0 # Rule 2: die if more than 3 BOARD[convolved_board > 3] = 0 # Rule 3: alive if 3 BOARD[convolved_board == 3] = 1 plt.imshow(BOARD) matplotlib.pyplot.show() # If board hasn't changed or everything is dead, exit if np.all(BOARD == 0) or np.all(old_board == BOARD): break

### Eclairemoy commented Oct 1, 2019

thanks for posting this (and pairing with me)!

### robinovitch61 commented Oct 1, 2019

thanks for posting this (and pairing with me)!

It was a pleasure! :)