{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. feladat\n", "\n", "Készítsük el a [Conway-féle életjáték](https://hu.wikipedia.org/wiki/%C3%89letj%C3%A1t%C3%A9k) objektumorientált változatát!" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "worldstr = '''\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", ".........oo.........\n", "........oo..........\n", ".........o..........\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "'''.strip()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", ".........oo.........\n", "........oo..........\n", ".........o..........\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "........ooo.........\n", "........o...........\n", "........oo..........\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "Interrupted by user", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 40\u001b[0m \u001b[0mdisplay_world\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mworld\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 41\u001b[0m \u001b[0mworld\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mupdate_world\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mworld\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 42\u001b[1;33m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 858\u001b[0m \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 859\u001b[0m )\n\u001b[1;32m--> 860\u001b[1;33m return self._input_request(str(prompt),\n\u001b[0m\u001b[0;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 902\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 903\u001b[0m \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 905\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user" ] } ], "source": [ "# kezdőállapot beolvasása listák listájába\n", "world = [list(row) for row in worldstr.split('\\n')]\n", "\n", "def display_world(world):\n", " for row in world:\n", " print(''.join(row))\n", " \n", "def count_neighbors(world, i, j):\n", " nb = 0 # szomszédok szám\n", " for di in [-1, 0, 1]: # végigmegyünk a szomszédos sorokon\n", " for dj in [-1, 0, 1]: # végigmegyünk a szomszédos oszlopokon\n", " if di == 0 and dj == 0: # önmagát ne számítsuk szomszédként\n", " continue\n", " if j + dj < 0 or i + di < 0: # negatív indext ne használjunk\n", " continue\n", " \n", " try:\n", " if world[i + di][j + dj] == 'o': # ha találtunk szomszédot\n", " nb += 1\n", " except IndexError:\n", " pass\n", " return nb\n", "\n", "import copy\n", "\n", "def update_world(world):\n", " nrows = len(world)\n", " ncols = len(world[0])\n", " new_world = copy.deepcopy(world) # másolat készítése\n", " for i in range(nrows):\n", " for j in range(ncols):\n", " nb = count_neighbors(world, i, j)\n", " if nb < 2 or nb > 3: # elpusztul a sejt\n", " new_world[i][j] = '.'\n", " elif nb == 3: # új sejt születik\n", " new_world[i][j] = 'o'\n", " return new_world\n", "\n", "while True:\n", " display_world(world)\n", " world = update_world(world)\n", " input()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import copy\n", "\n", "class Game:\n", " def __init__(self, worldstr):\n", " # kezdőállapot beolvasása listák listájába\n", " self.world = [list(row) for row in worldstr.split('\\n')]\n", " \n", " def run(self, nsteps=None):\n", " step = 0\n", " while nsteps == None or step < nsteps:\n", " self.display_world() # megjelenítés\n", " self.world = self.update_world() # léptetés\n", " input() # enter bekérése\n", " step += 1\n", " \n", " def update_world(self):\n", " nrows = len(self.world)\n", " ncols = len(self.world[0])\n", " new_world = copy.deepcopy(self.world) # másolat készítése\n", " for i in range(nrows):\n", " for j in range(ncols):\n", " nb = self.count_neighbors(i, j)\n", " if nb < 2 or nb > 3: # elpusztul a sejt\n", " new_world[i][j] = '.'\n", " elif nb == 3: # új sejt születik\n", " new_world[i][j] = 'o'\n", " return new_world\n", " \n", " def display_world(self):\n", " for row in self.world:\n", " print(''.join(row))\n", " \n", " def count_neighbors(self, i, j):\n", " nb = 0 # szomszédok száma\n", " for di in [-1, 0, 1]: # végigmegyünk a szomszédos sorokon\n", " for dj in [-1, 0, 1]: # végigmegyünk a szomszédos oszlopokon\n", " if di == 0 and dj == 0: # önmagát ne számítsuk szomszédként\n", " continue\n", " if j + dj < 0 or i + di < 0: # negatív indext ne használjunk\n", " continue\n", "\n", " try:\n", " if self.world[i + di][j + dj] == 'o': # ha találtunk szomszédot\n", " nb += 1\n", " except IndexError:\n", " pass\n", " return nb" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", ".........oo.........\n", "........oo..........\n", ".........o..........\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "........ooo.........\n", "........o...........\n", "........oo..........\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", ".........o..........\n", "........oo..........\n", ".......o..o.........\n", "........oo..........\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "....................\n", "\n" ] } ], "source": [ "Game(worldstr).run(3)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }