Created
January 3, 2021 15:24
-
-
Save rifazn/a6cc24aac977d3c722ed3da22c4e9341 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Gen: 1\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"[0, 0, 0, 1, 1, 1, 0]\n", | |
"[0, 0, 1, 1, 1, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"Gen: 2\n", | |
"[0, 0, 0, 0, 1, 0, 0]\n", | |
"[0, 0, 1, 0, 0, 1, 0]\n", | |
"[0, 0, 1, 0, 0, 1, 0]\n", | |
"[0, 0, 0, 1, 0, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"Gen: 3\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"[0, 0, 0, 1, 1, 1, 0]\n", | |
"[0, 0, 1, 1, 1, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"Gen: 4\n", | |
"[0, 0, 0, 0, 1, 0, 0]\n", | |
"[0, 0, 1, 0, 0, 1, 0]\n", | |
"[0, 0, 1, 0, 0, 1, 0]\n", | |
"[0, 0, 0, 1, 0, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"Gen: 5\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"[0, 0, 0, 1, 1, 1, 0]\n", | |
"[0, 0, 1, 1, 1, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n", | |
"[0, 0, 0, 0, 0, 0, 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"from copy import deepcopy\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"NUMROWS = NUMCOLS = 5\n", | |
"\n", | |
"# game grid, but with a padding of zeroes on either side\n", | |
"# to make counting neighboring live cells earier\n", | |
"grid = [[0 for _ in range(NUMCOLS + 2)]] # upper padding\n", | |
"grid += [[0 for _ in range(NUMCOLS + 2)] for _ in range(NUMROWS)]\n", | |
"grid += [[0 for _ in range(NUMCOLS + 2)]] # lower padding\n", | |
"\n", | |
"future_grid = deepcopy(grid)\n", | |
"\n", | |
"def print_grid(do_print=True, do_draw=False):\n", | |
" if do_print == False and do_draw == False:\n", | |
" do_print = True\n", | |
" \n", | |
" if do_print:\n", | |
" for i in range(1, NUMROWS + 1):\n", | |
" print(future_grid[i])\n", | |
" if do_draw:\n", | |
" draw_grid(future_grid)\n", | |
"\n", | |
"def draw_grid(image_matrix):\n", | |
" \n", | |
" nrows = len(image_matrix)\n", | |
" ncols = len(image_matrix[0])\n", | |
" row_labels = range(nrows)\n", | |
" col_labels = range(ncols)\n", | |
" plt.matshow(image_matrix)\n", | |
" plt.xticks(range(ncols), col_labels)\n", | |
" plt.yticks(range(nrows), row_labels)\n", | |
" plt.show()\n", | |
"\n", | |
"def count_live_neighbors(i, j):\n", | |
" upper_row = i-1\n", | |
" bottom_row = i+1\n", | |
" left = j-1\n", | |
" right = j+1\n", | |
"\n", | |
" \"\"\"if upper_row == 0 or bottom_row == NUMROWS + 1 \\\n", | |
" or left == 0 or right == NUMCOLS + 1:\n", | |
" print(upper_row, bottom_row, left, right)\n", | |
" raise Exception('Yo. Out of bounds.')\"\"\"\n", | |
"\n", | |
" return sum(\n", | |
" grid[ upper_row][left : right + 1] +\n", | |
" [grid[i][left]] + [grid[i][right]] +\n", | |
" grid[bottom_row][left : right + 1]\n", | |
" )\n", | |
"\n", | |
"def set_live_cells(live_positions: '2d tuple'):\n", | |
" for [x, y] in live_positions:\n", | |
" grid[x][y] = 1\n", | |
"\n", | |
"def conway_step(i, j):\n", | |
" live_count = count_live_neighbors(i, j)\n", | |
"\n", | |
" if grid[i][j] == 1: # cell is 'live'\n", | |
" if live_count < 2 or live_count > 3:\n", | |
" future_grid[i][j] = 0\n", | |
" else:\n", | |
" if live_count == 3:\n", | |
" future_grid[i][j] = 1\n", | |
"\n", | |
"def conway(num_generations = 5):\n", | |
" global grid, future_grid\n", | |
" for g in range(num_generations):\n", | |
" future_grid = deepcopy(grid)\n", | |
" print(f'Gen: {g + 1}')\n", | |
" print_grid()\n", | |
" for i in range(1, NUMROWS + 1):\n", | |
" for j in range(1, NUMCOLS+1):\n", | |
" conway_step(i, j)\n", | |
" grid = deepcopy(future_grid)\n", | |
"\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" live_cells = [\n", | |
" [2, 3], [2, 4], [2, 5],\n", | |
" [3, 2], [3, 3], [3, 4]\n", | |
" ]\n", | |
" set_live_cells(live_cells)\n", | |
" conway()\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment