{{ message }}

Instantly share code, notes, and snippets.

# jiffyclub/game_of_life.ipynb

Created Sep 24, 2012
Conway's Game of Life in an IPython Notebook
 { "metadata": { "name": "Game of Life" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conway's Game of Life\n", "\n", "For more on Conway's Game of Life: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\n", "\n", "Based on code originally sent to me by [@gvwilson](http://twitter.com/gvwilson)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from scipy.signal import convolve\n", "\n", "# used for counting the number of living neighbors each cell has\n", "FILTER = np.array([[1, 1, 1],\n", " [1, 100, 1],\n", " [1, 1, 1]], dtype=np.uint8)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "def evolve(length, generations):\n", " \"\"\"\n", " Run the Game of Life. Starting state is random.\n", "\n", " Parameters\n", " ----------\n", " length : int\n", " Universe will be `length` units per side.\n", " generations : int\n", " Number of generations to run simulation.\n", "\n", " \"\"\"\n", " current = np.random.randint(2, size=(length, length))\n", " next = np.empty_like(current)\n", " current[length/2, 1:(length-1)] = 1\n", " show_board(current)\n", " for _ in xrange(generations):\n", " advance(current, next)\n", " current, next = next, current\n", " show_board(current)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "def advance(current, next):\n", " \"\"\"\n", " Calculate the next iteration of the Game of Life.\n", "\n", " Parameters\n", " ----------\n", " current : 2D array\n", " Current state of universe.\n", " next : 2D array\n", " This array will be modified in place so that it contains the\n", " next step. Must be the same size as `current`.\n", "\n", " \"\"\"\n", " assert current.shape[0] == current.shape[1], \\\n", " 'Expected square universe'\n", " next[:] = 0\n", " count = convolve(current, FILTER, mode='same')\n", " next[(count == 3) | (count == 102) | (count == 103)] = 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import clear_output, display_html\n", "import time\n", "def show_board(board):\n", " \"\"\"\n", " Print the current Game of Life universe in an HTML table.\n", " Removes any existing output using `IPython.display.clear_output`\n", " to make an animation. This doesn't scale well beyond ~50x50 boards.\n", "\n", " Parameters\n", " ----------\n", " board : 2D array\n", " Array representing the current universe.\n", "\n", " \"\"\"\n", " clear_output()\n", " nx, ny = board.shape\n", " table = '\\n'\n", " for y in xrange(ny-1, -1, -1):\n", " table += '
'\n", " for x in xrange(0, nx):\n", " if board[x, y]:\n", " table += ''\n", " else:\n", " table += ''\n", " table += '\\n'\n", " table += ''\n", " display_html(table, raw=True)\n", " time.sleep(0.1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "evolve(40, 100)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "" ], "output_type": "display_data" } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }

### Kay1223 commented Sep 13, 2016

 Does not work... because of "xrange" is not defined, my notebook said.

### jmeier1963 commented Nov 10, 2018

 @Kay1223: It should work fine with Python 2.7

### snordhausen commented Apr 16, 2020

 To make this work in Python 3, replace all `xrange` with `range` (3 replacements) and also replace `length/2` with `length//2`. Then it works fine in Python 3.8.