Skip to content

Instantly share code, notes, and snippets.

@Karthik-d-k
Created May 7, 2022 07:54
Show Gist options
  • Save Karthik-d-k/246f63f037ae8613ab70e340d0878444 to your computer and use it in GitHub Desktop.
Save Karthik-d-k/246f63f037ae8613ab70e340d0878444 to your computer and use it in GitHub Desktop.
Conways game of life in Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conways game of life in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These rules, which compare the behavior of the automaton to real life, can be condensed into the following:\n",
"\n",
"- Any live cell with two or three live neighbours survives.\n",
"- Any dead cell with three live neighbours becomes a live cell.\n",
"- All other live cells die in the next generation. Similarly, all other dead cells stay dead."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LIVE = πŸ–€\n",
"DEAD = 🀍\n"
]
}
],
"source": [
"LIVE = \"\\U0001F5A4\"\n",
"DEAD = \"\\U0001F90D\"\n",
"print(f\"LIVE = {LIVE}\\nDEAD = {DEAD}\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class CELL():\n",
" def __init__(self, cur_state, xy, next_state = None):\n",
" self.cur_state = cur_state\n",
" self.next_state = next_state\n",
" self.x = xy[0]\n",
" self.y = xy[1]\n",
"\n",
" def __repr__(self):\n",
" return f\"{self.cur_state}\"\n",
" \n",
" def neighbors(self):\n",
" \"\"\"\n",
" Return neighbors for the given (x, y) cell\n",
" \"\"\"\n",
" rmin = (0) if ((self.x - 1) < 0) else (self.x - 1)\n",
" rmax = (n-1) if ((self.x + 1) > n-1) else (self.x + 1)\n",
" cmin = (0) if ((self.y - 1) < 0) else (self.y - 1)\n",
" cmax = (n-1) if ((self.y + 1) > n-1) else (self.y + 1)\n",
"\n",
" neibs = [(r, c) for c in range(cmin, cmax+1) for r in range(rmin, rmax+1) if (r, c) != (self.x, self.y)]\n",
" return neibs\n",
" \n",
" def transition(self):\n",
" \"\"\"\n",
" Update `next_state` value to `cur_state`\n",
" \"\"\"\n",
" if self.next_state != None:\n",
" self.cur_state = self.next_state\n",
" else :\n",
" raise ValueError"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"n = 6\n",
"cells = [[] for i in range(n)]\n",
"def init_game(cells):\n",
" # Initial seed\n",
" cells[0].append(CELL(DEAD, (0, 0)))\n",
" cells[0].append(CELL(DEAD, (0, 1)))\n",
" cells[0].append(CELL(DEAD, (0, 2)))\n",
" cells[0].append(CELL(DEAD, (0, 3)))\n",
" cells[0].append(CELL(DEAD, (0, 4)))\n",
" cells[0].append(CELL(DEAD, (0, 5)))\n",
" cells[1].append(CELL(DEAD, (1, 0)))\n",
" cells[1].append(CELL(DEAD, (1, 1)))\n",
" cells[1].append(CELL(DEAD, (1, 2)))\n",
" cells[1].append(CELL(DEAD, (1, 3)))\n",
" cells[1].append(CELL(DEAD, (1, 4)))\n",
" cells[1].append(CELL(DEAD, (1, 5)))\n",
" cells[2].append(CELL(DEAD, (2, 0)))\n",
" cells[2].append(CELL(DEAD, (2, 1)))\n",
" cells[2].append(CELL(LIVE, (2, 2)))\n",
" cells[2].append(CELL(LIVE, (2, 3)))\n",
" cells[2].append(CELL(LIVE, (2, 4)))\n",
" cells[2].append(CELL(DEAD, (2, 5)))\n",
" cells[3].append(CELL(DEAD, (3, 0)))\n",
" cells[3].append(CELL(LIVE, (3, 1)))\n",
" cells[3].append(CELL(LIVE, (3, 2)))\n",
" cells[3].append(CELL(LIVE, (3, 3)))\n",
" cells[3].append(CELL(DEAD, (3, 4)))\n",
" cells[3].append(CELL(DEAD, (3, 5)))\n",
" cells[4].append(CELL(DEAD, (4, 0)))\n",
" cells[4].append(CELL(DEAD, (4, 1)))\n",
" cells[4].append(CELL(DEAD, (4, 2)))\n",
" cells[4].append(CELL(DEAD, (4, 3)))\n",
" cells[4].append(CELL(DEAD, (4, 4)))\n",
" cells[4].append(CELL(DEAD, (4, 5)))\n",
" cells[5].append(CELL(DEAD, (5, 0)))\n",
" cells[5].append(CELL(DEAD, (5, 1)))\n",
" cells[5].append(CELL(DEAD, (5, 2)))\n",
" cells[5].append(CELL(DEAD, (5, 3)))\n",
" cells[5].append(CELL(DEAD, (5, 4)))\n",
" cells[5].append(CELL(DEAD, (5, 5)))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def print_cells(cells):\n",
" for i in range(0, n):\n",
" for j in range(0, n):\n",
" print(cells[i][j], end=\" \")\n",
" print()\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n",
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n",
"🀍 🀍 πŸ–€ πŸ–€ πŸ–€ 🀍 \n",
"\n",
"🀍 πŸ–€ πŸ–€ πŸ–€ 🀍 🀍 \n",
"\n",
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n",
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n"
]
}
],
"source": [
"init_game(cells)\n",
"print_cells(cells)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def set_next_state(cells):\n",
" \"\"\"\n",
" Set `next_state` as per Conway's rules\n",
" \"\"\"\n",
" rc = [(r, c) for r in range(n) for c in range(n)]\n",
" for r, c in (rc):\n",
" n_live = 0\n",
" n_live = sum([1 if (cells[i][j].cur_state == LIVE) else 0 for i, j in cells[r][c].neighbors()])\n",
" if (cells[r][c].cur_state == LIVE):\n",
" if not(n_live == 2 or n_live == 3):\n",
" cells[r][c].next_state = DEAD\n",
" else :\n",
" cells[r][c].next_state = LIVE\n",
" if (cells[r][c].cur_state == DEAD):\n",
" if (n_live == 3):\n",
" cells[r][c].next_state = LIVE\n",
" else :\n",
" cells[r][c].next_state = DEAD"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def call_transition(cells):\n",
" rc = [(r, c) for r in range(n) for c in range(n)]\n",
" for r, c in (rc):\n",
" cells[r][c].transition()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n",
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n",
"🀍 🀍 πŸ–€ πŸ–€ πŸ–€ 🀍 \n",
"\n",
"🀍 πŸ–€ πŸ–€ πŸ–€ 🀍 🀍 \n",
"\n",
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n",
"🀍 🀍 🀍 🀍 🀍 🀍 \n",
"\n"
]
}
],
"source": [
"from time import sleep\n",
"from IPython.display import clear_output\n",
"\n",
"for _ in range(20):\n",
" clear_output(wait=False)\n",
" set_next_state(cells)\n",
" call_transition(cells)\n",
" print_cells(cells)\n",
" sleep(0.5)"
]
},
{
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment