game of life master
authorGeorgios Atheridis <atheridis@tutamail.com>
Thu, 7 Jul 2022 06:29:25 +0000 (09:29 +0300)
committerGeorgios Atheridis <atheridis@tutamail.com>
Thu, 7 Jul 2022 06:29:25 +0000 (09:29 +0300)
13 files changed:
.flake8 [new file with mode: 0644]
.gitignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
README.md [new file with mode: 0644]
game_of_life/__init__.py [new file with mode: 0644]
game_of_life/board.py [new file with mode: 0755]
game_of_life/cell.py [new file with mode: 0755]
game_of_life/data/save.json [new file with mode: 0644]
game_of_life/data/settings.json [new file with mode: 0644]
game_of_life/game_of_life.py [new file with mode: 0644]
game_of_life/main.py [new file with mode: 0644]
requirements.txt [new file with mode: 0644]
setup.py [new file with mode: 0644]

diff --git a/.flake8 b/.flake8
new file mode 100644 (file)
index 0000000..ae11a13
--- /dev/null
+++ b/.flake8
@@ -0,0 +1,7 @@
+[flake8]
+ignore = E203, E266, E501, W503
+# line length is intentionally set to 80 here because black uses Bugbear
+# See https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-length for more details
+max-line-length = 80
+max-complexity = 18
+select = B,C,E,F,W,T4,B9
diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..b6e4761
--- /dev/null
@@ -0,0 +1,129 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+pip-wheel-metadata/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+.python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..8aa2645
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) [year] [fullname]
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..1aed34d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,50 @@
+<h1 align="center">Conway's Game of Life - In Python's Pygame</h1>
+
+<p align="center">
+    <img width="600"
+        alt="Conway's Game of Life - In Python's Pygame"
+        src="https://i.imgur.com/mHcbvKw.png">
+</p>
+
+## Installation
+
+To install the latest version just type `$ pip install git+https://github.com/atheridis/game-of-life.git`
+in your terminal. To run the program type `$ gameoflife`.
+
+You may also clone this repository and install it from there.
+```
+$ git clone https://github.com/atheridis/game-of-life.git
+$ cd game-of-life
+$ pip install .
+```
+
+## Settings
+
+You will find settings in `~/.config/game-of-life`. Currently Settings are changed through
+editing a json file. You can choose the game resolution, the width of the cells in pixels,
+the colour of dead and alive cells, and the maximum amount of frames that get computed
+each second.
+
+You will need to run the game at least once before the directory becomes available.n your terminal.
+To run the program type `$ gameoflife`.
+
+## How to use
+
+### Loading and Saving
+
+Your numrow allow you to load saved states. Some saved states already exist for you to try.
+You may press the Function Keys, F1, F2, ..., F10 to save to the corresponding slot (where F10 is 0 on the numrow).
+If a state is already saved in one of those keys it will be overwritten **without warning**.
+
+### Keybindings
+
+* Use the Up and Down arrow keys to speed up or slow down the game respectively.
+* Use the Spacebar to pause and resume the game
+* Press [c] to clear the board
+* Press [r] to randomize the board
+* Press [ESC] to quit.
+
+## OLD PROJECT
+
+This is one of many of my older projects which I never used git with.
+I have decided to turn it into a package and upload it to github.
diff --git a/game_of_life/__init__.py b/game_of_life/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/game_of_life/board.py b/game_of_life/board.py
new file mode 100755 (executable)
index 0000000..27b66e8
--- /dev/null
@@ -0,0 +1,77 @@
+from itertools import product\r
+\r
+from .cell import Cell\r
+\r
+\r
+class Board:\r
+    def __init__(self, size):\r
+        """\r
+        :param size:\r
+        """\r
+        self.size = size\r
+        self._cells = []\r
+        self.living_cells = []\r
+        self.grid = []\r
+        self._altered = set()\r
+\r
+    def create(self, state=None):\r
+        """\r
+        :param start_state:\r
+        """\r
+        state = [] if state is None else state\r
+\r
+        self.grid = []\r
+        self._cells = []\r
+        self.living_cells = []\r
+        self._altered = set()\r
+\r
+        for i in range(self.size[0]):\r
+            self.grid.append([])\r
+            for j in range(self.size[1]):\r
+                if [i, j] in state:\r
+                    c = Cell((i, j), 1)\r
+                    self.living_cells.append(c)\r
+                else:\r
+                    c = Cell((i, j), 0)\r
+                self._cells.append(c)\r
+                self.grid[i].append(c)\r
+\r
+        self._assign_neighbours()\r
+\r
+    def update_grid(self):\r
+        self._altered = set()\r
+        for living_cell in self.living_cells:\r
+            altered_cells = living_cell.update_neighbours()\r
+            self._altered.update(altered_cells)\r
+\r
+        self.living_cells = []\r
+\r
+        for altered_cell in self._altered:\r
+            altered_cell.update_status()\r
+            if altered_cell:\r
+                self.living_cells.append(altered_cell)\r
+\r
+    def change_cell_status(self, pos):\r
+        self.grid[pos[0]][pos[1]].change_status()\r
+\r
+        if self.grid[pos[0]][pos[1]]:\r
+            self.living_cells.append(self.grid[pos[0]][pos[1]])\r
+        else:\r
+            self.living_cells.remove(self.grid[pos[0]][pos[1]])\r
+\r
+    def randomise_grid(self, alive_chance):\r
+        self.living_cells = []\r
+        for c in self._cells:\r
+            c.randomise_status(alive_chance)\r
+            if c:\r
+                self.living_cells.append(c)\r
+\r
+    def _assign_neighbours(self):\r
+        for (n, m) in product(range(self.size[0]), range(self.size[1])):\r
+            for (i, j) in product(range(3), range(3)):\r
+                if (\r
+                    0 <= n - i + 1 < self.size[0]\r
+                    and 0 <= m - j + 1 < self.size[1]\r
+                    and (i, j) != (1, 1)\r
+                ):\r
+                    self.grid[n][m].neighbours.append(self.grid[n - i + 1][m - j + 1])\r
diff --git a/game_of_life/cell.py b/game_of_life/cell.py
new file mode 100755 (executable)
index 0000000..fe66e70
--- /dev/null
@@ -0,0 +1,55 @@
+import random\r
+\r
+\r
+class Cell:\r
+    def __init__(self, pos, status=0):\r
+        """\r
+        :param pos:\r
+        :param status:\r
+        """\r
+        self.pos = pos\r
+        self.neighbours = []\r
+        self.status = status\r
+        self._living_neighbours = 0\r
+\r
+    def change_status(self):\r
+        if self.status == 0:\r
+            self.status = 1\r
+        else:\r
+            self.status = 0\r
+\r
+    def update_status(self):\r
+        if self:\r
+            if self._living_neighbours > 3:\r
+                self.status = 0\r
+            elif self._living_neighbours < 2:\r
+                self.status = 0\r
+        elif self._living_neighbours == 3:\r
+            self.status = 1\r
+\r
+        self._living_neighbours = 0\r
+\r
+    def neighbour_count(self):\r
+        self._living_neighbours += 1\r
+\r
+    def update_neighbours(self):\r
+        updated = [self]\r
+        if self:\r
+            for neighbour in self.neighbours:\r
+                neighbour.neighbour_count()\r
+                updated.append(neighbour)\r
+\r
+        return updated\r
+\r
+    def randomise_status(self, alive_chance):\r
+        rand = random.random()\r
+        if rand <= alive_chance:\r
+            self.status = 1\r
+        else:\r
+            self.status = 0\r
+\r
+    def __bool__(self):\r
+        if self.status == 0:\r
+            return False\r
+\r
+        return True\r
diff --git a/game_of_life/data/save.json b/game_of_life/data/save.json
new file mode 100644 (file)
index 0000000..68404d0
--- /dev/null
@@ -0,0 +1,1078 @@
+{\r
+  "starting_position": [\r
+    [\r
+      5,\r
+      2\r
+    ],\r
+    [\r
+      5,\r
+      3\r
+    ],\r
+    [\r
+      6,\r
+      3\r
+    ],\r
+    [\r
+      6,\r
+      2\r
+    ],\r
+    [\r
+      5,\r
+      12\r
+    ],\r
+    [\r
+      6,\r
+      12\r
+    ],\r
+    [\r
+      7,\r
+      12\r
+    ],\r
+    [\r
+      4,\r
+      13\r
+    ],\r
+    [\r
+      3,\r
+      14\r
+    ],\r
+    [\r
+      8,\r
+      13\r
+    ],\r
+    [\r
+      9,\r
+      14\r
+    ],\r
+    [\r
+      9,\r
+      15\r
+    ],\r
+    [\r
+      3,\r
+      15\r
+    ],\r
+    [\r
+      8,\r
+      17\r
+    ],\r
+    [\r
+      7,\r
+      18\r
+    ],\r
+    [\r
+      6,\r
+      18\r
+    ],\r
+    [\r
+      5,\r
+      18\r
+    ],\r
+    [\r
+      4,\r
+      17\r
+    ],\r
+    [\r
+      6,\r
+      19\r
+    ],\r
+    [\r
+      6,\r
+      16\r
+    ],\r
+    [\r
+      5,\r
+      22\r
+    ],\r
+    [\r
+      4,\r
+      22\r
+    ],\r
+    [\r
+      3,\r
+      22\r
+    ],\r
+    [\r
+      3,\r
+      23\r
+    ],\r
+    [\r
+      4,\r
+      23\r
+    ],\r
+    [\r
+      5,\r
+      23\r
+    ],\r
+    [\r
+      2,\r
+      24\r
+    ],\r
+    [\r
+      6,\r
+      24\r
+    ],\r
+    [\r
+      1,\r
+      26\r
+    ],\r
+    [\r
+      2,\r
+      26\r
+    ],\r
+    [\r
+      6,\r
+      26\r
+    ],\r
+    [\r
+      7,\r
+      26\r
+    ],\r
+    [\r
+      3,\r
+      36\r
+    ],\r
+    [\r
+      4,\r
+      36\r
+    ],\r
+    [\r
+      4,\r
+      37\r
+    ],\r
+    [\r
+      3,\r
+      37\r
+    ]\r
+  ],\r
+  "save_1": [\r
+    [\r
+      11,\r
+      31\r
+    ],\r
+    [\r
+      11,\r
+      32\r
+    ],\r
+    [\r
+      11,\r
+      36\r
+    ],\r
+    [\r
+      11,\r
+      37\r
+    ],\r
+    [\r
+      11,\r
+      41\r
+    ],\r
+    [\r
+      11,\r
+      42\r
+    ],\r
+    [\r
+      11,\r
+      46\r
+    ],\r
+    [\r
+      11,\r
+      47\r
+    ],\r
+    [\r
+      11,\r
+      51\r
+    ],\r
+    [\r
+      11,\r
+      52\r
+    ],\r
+    [\r
+      16,\r
+      51\r
+    ],\r
+    [\r
+      16,\r
+      52\r
+    ],\r
+    [\r
+      8,\r
+      37\r
+    ],\r
+    [\r
+      8,\r
+      36\r
+    ],\r
+    [\r
+      18,\r
+      20\r
+    ],\r
+    [\r
+      8,\r
+      32\r
+    ],\r
+    [\r
+      22,\r
+      45\r
+    ],\r
+    [\r
+      22,\r
+      46\r
+    ],\r
+    [\r
+      8,\r
+      31\r
+    ],\r
+    [\r
+      8,\r
+      52\r
+    ],\r
+    [\r
+      21,\r
+      45\r
+    ],\r
+    [\r
+      8,\r
+      51\r
+    ],\r
+    [\r
+      21,\r
+      47\r
+    ],\r
+    [\r
+      18,\r
+      50\r
+    ],\r
+    [\r
+      8,\r
+      27\r
+    ],\r
+    [\r
+      18,\r
+      18\r
+    ],\r
+    [\r
+      8,\r
+      26\r
+    ],\r
+    [\r
+      8,\r
+      47\r
+    ],\r
+    [\r
+      15,\r
+      53\r
+    ],\r
+    [\r
+      8,\r
+      46\r
+    ],\r
+    [\r
+      15,\r
+      51\r
+    ],\r
+    [\r
+      8,\r
+      22\r
+    ],\r
+    [\r
+      8,\r
+      21\r
+    ],\r
+    [\r
+      8,\r
+      42\r
+    ],\r
+    [\r
+      18,\r
+      48\r
+    ],\r
+    [\r
+      8,\r
+      41\r
+    ],\r
+    [\r
+      10,\r
+      28\r
+    ],\r
+    [\r
+      20,\r
+      22\r
+    ],\r
+    [\r
+      10,\r
+      30\r
+    ],\r
+    [\r
+      20,\r
+      21\r
+    ],\r
+    [\r
+      10,\r
+      33\r
+    ],\r
+    [\r
+      10,\r
+      35\r
+    ],\r
+    [\r
+      10,\r
+      38\r
+    ],\r
+    [\r
+      10,\r
+      40\r
+    ],\r
+    [\r
+      14,\r
+      15\r
+    ],\r
+    [\r
+      14,\r
+      16\r
+    ],\r
+    [\r
+      10,\r
+      43\r
+    ],\r
+    [\r
+      10,\r
+      45\r
+    ],\r
+    [\r
+      10,\r
+      48\r
+    ],\r
+    [\r
+      10,\r
+      50\r
+    ],\r
+    [\r
+      10,\r
+      53\r
+    ],\r
+    [\r
+      21,\r
+      21\r
+    ],\r
+    [\r
+      21,\r
+      23\r
+    ],\r
+    [\r
+      9,\r
+      23\r
+    ],\r
+    [\r
+      9,\r
+      25\r
+    ],\r
+    [\r
+      9,\r
+      28\r
+    ],\r
+    [\r
+      9,\r
+      30\r
+    ],\r
+    [\r
+      9,\r
+      33\r
+    ],\r
+    [\r
+      9,\r
+      35\r
+    ],\r
+    [\r
+      9,\r
+      38\r
+    ],\r
+    [\r
+      9,\r
+      40\r
+    ],\r
+    [\r
+      9,\r
+      43\r
+    ],\r
+    [\r
+      9,\r
+      45\r
+    ],\r
+    [\r
+      9,\r
+      48\r
+    ],\r
+    [\r
+      9,\r
+      50\r
+    ],\r
+    [\r
+      9,\r
+      53\r
+    ],\r
+    [\r
+      22,\r
+      22\r
+    ],\r
+    [\r
+      22,\r
+      23\r
+    ],\r
+    [\r
+      19,\r
+      20\r
+    ],\r
+    [\r
+      8,\r
+      17\r
+    ],\r
+    [\r
+      8,\r
+      16\r
+    ],\r
+    [\r
+      17,\r
+      18\r
+    ],\r
+    [\r
+      17,\r
+      19\r
+    ],\r
+    [\r
+      19,\r
+      48\r
+    ],\r
+    [\r
+      11,\r
+      16\r
+    ],\r
+    [\r
+      19,\r
+      49\r
+    ],\r
+    [\r
+      11,\r
+      17\r
+    ],\r
+    [\r
+      11,\r
+      21\r
+    ],\r
+    [\r
+      11,\r
+      22\r
+    ],\r
+    [\r
+      17,\r
+      49\r
+    ],\r
+    [\r
+      17,\r
+      50\r
+    ],\r
+    [\r
+      11,\r
+      26\r
+    ],\r
+    [\r
+      11,\r
+      27\r
+    ],\r
+    [\r
+      15,\r
+      17\r
+    ],\r
+    [\r
+      15,\r
+      15\r
+    ],\r
+    [\r
+      9,\r
+      15\r
+    ],\r
+    [\r
+      20,\r
+      47\r
+    ],\r
+    [\r
+      9,\r
+      18\r
+    ],\r
+    [\r
+      20,\r
+      46\r
+    ],\r
+    [\r
+      9,\r
+      20\r
+    ],\r
+    [\r
+      19,\r
+      19\r
+    ],\r
+    [\r
+      16,\r
+      16\r
+    ],\r
+    [\r
+      16,\r
+      17\r
+    ],\r
+    [\r
+      14,\r
+      52\r
+    ],\r
+    [\r
+      14,\r
+      53\r
+    ],\r
+    [\r
+      10,\r
+      15\r
+    ],\r
+    [\r
+      10,\r
+      18\r
+    ],\r
+    [\r
+      10,\r
+      20\r
+    ],\r
+    [\r
+      10,\r
+      23\r
+    ],\r
+    [\r
+      10,\r
+      25\r
+    ],\r
+    [\r
+      20,\r
+      34\r
+    ],\r
+    [\r
+      20,\r
+      35\r
+    ],\r
+    [\r
+      21,\r
+      35\r
+    ],\r
+    [\r
+      22,\r
+      35\r
+    ],\r
+    [\r
+      22,\r
+      34\r
+    ],\r
+    [\r
+      22,\r
+      33\r
+    ],\r
+    [\r
+      21,\r
+      33\r
+    ],\r
+    [\r
+      20,\r
+      33\r
+    ],\r
+    [\r
+      19,\r
+      34\r
+    ],\r
+    [\r
+      23,\r
+      34\r
+    ]\r
+  ],\r
+  "save_3": [\r
+    [\r
+      3,\r
+      5\r
+    ],\r
+    [\r
+      3,\r
+      8\r
+    ],\r
+    [\r
+      5,\r
+      5\r
+    ],\r
+    [\r
+      6,\r
+      6\r
+    ],\r
+    [\r
+      6,\r
+      7\r
+    ],\r
+    [\r
+      6,\r
+      8\r
+    ],\r
+    [\r
+      6,\r
+      9\r
+    ],\r
+    [\r
+      5,\r
+      9\r
+    ],\r
+    [\r
+      4,\r
+      9\r
+    ],\r
+    [\r
+      9,\r
+      5\r
+    ],\r
+    [\r
+      11,\r
+      5\r
+    ],\r
+    [\r
+      9,\r
+      8\r
+    ],\r
+    [\r
+      10,\r
+      9\r
+    ],\r
+    [\r
+      11,\r
+      9\r
+    ],\r
+    [\r
+      12,\r
+      9\r
+    ],\r
+    [\r
+      12,\r
+      8\r
+    ],\r
+    [\r
+      12,\r
+      7\r
+    ],\r
+    [\r
+      12,\r
+      6\r
+    ],\r
+    [\r
+      15,\r
+      5\r
+    ],\r
+    [\r
+      17,\r
+      5\r
+    ],\r
+    [\r
+      18,\r
+      6\r
+    ],\r
+    [\r
+      18,\r
+      7\r
+    ],\r
+    [\r
+      18,\r
+      8\r
+    ],\r
+    [\r
+      15,\r
+      8\r
+    ],\r
+    [\r
+      18,\r
+      9\r
+    ],\r
+    [\r
+      17,\r
+      9\r
+    ],\r
+    [\r
+      16,\r
+      9\r
+    ],\r
+    [\r
+      21,\r
+      5\r
+    ],\r
+    [\r
+      23,\r
+      5\r
+    ],\r
+    [\r
+      24,\r
+      6\r
+    ],\r
+    [\r
+      24,\r
+      7\r
+    ],\r
+    [\r
+      24,\r
+      8\r
+    ],\r
+    [\r
+      24,\r
+      9\r
+    ],\r
+    [\r
+      23,\r
+      9\r
+    ],\r
+    [\r
+      22,\r
+      9\r
+    ],\r
+    [\r
+      21,\r
+      8\r
+    ],\r
+    [\r
+      27,\r
+      5\r
+    ],\r
+    [\r
+      29,\r
+      5\r
+    ],\r
+    [\r
+      30,\r
+      6\r
+    ],\r
+    [\r
+      30,\r
+      7\r
+    ],\r
+    [\r
+      30,\r
+      8\r
+    ],\r
+    [\r
+      30,\r
+      9\r
+    ],\r
+    [\r
+      29,\r
+      9\r
+    ],\r
+    [\r
+      28,\r
+      9\r
+    ],\r
+    [\r
+      27,\r
+      8\r
+    ]\r
+  ],\r
+  "save_4": [\r
+    [\r
+      3,\r
+      6\r
+    ],\r
+    [\r
+      4,\r
+      6\r
+    ],\r
+    [\r
+      5,\r
+      7\r
+    ],\r
+    [\r
+      6,\r
+      9\r
+    ],\r
+    [\r
+      6,\r
+      10\r
+    ],\r
+    [\r
+      7,\r
+      11\r
+    ],\r
+    [\r
+      8,\r
+      12\r
+    ],\r
+    [\r
+      9,\r
+      13\r
+    ],\r
+    [\r
+      8,\r
+      14\r
+    ],\r
+    [\r
+      10,\r
+      13\r
+    ],\r
+    [\r
+      11,\r
+      12\r
+    ],\r
+    [\r
+      12,\r
+      11\r
+    ],\r
+    [\r
+      13,\r
+      10\r
+    ],\r
+    [\r
+      13,\r
+      9\r
+    ],\r
+    [\r
+      14,\r
+      7\r
+    ],\r
+    [\r
+      15,\r
+      6\r
+    ],\r
+    [\r
+      16,\r
+      6\r
+    ],\r
+    [\r
+      17,\r
+      6\r
+    ],\r
+    [\r
+      17,\r
+      7\r
+    ],\r
+    [\r
+      17,\r
+      8\r
+    ],\r
+    [\r
+      17,\r
+      9\r
+    ],\r
+    [\r
+      17,\r
+      10\r
+    ],\r
+    [\r
+      16,\r
+      11\r
+    ],\r
+    [\r
+      3,\r
+      11\r
+    ],\r
+    [\r
+      2,\r
+      10\r
+    ],\r
+    [\r
+      2,\r
+      9\r
+    ],\r
+    [\r
+      2,\r
+      8\r
+    ],\r
+    [\r
+      2,\r
+      7\r
+    ],\r
+    [\r
+      2,\r
+      6\r
+    ],\r
+    [\r
+      11,\r
+      14\r
+    ],\r
+    [\r
+      6,\r
+      14\r
+    ],\r
+    [\r
+      6,\r
+      15\r
+    ],\r
+    [\r
+      7,\r
+      16\r
+    ],\r
+    [\r
+      8,\r
+      16\r
+    ],\r
+    [\r
+      13,\r
+      14\r
+    ],\r
+    [\r
+      13,\r
+      15\r
+    ],\r
+    [\r
+      12,\r
+      16\r
+    ],\r
+    [\r
+      11,\r
+      16\r
+    ],\r
+    [\r
+      6,\r
+      17\r
+    ],\r
+    [\r
+      6,\r
+      18\r
+    ],\r
+    [\r
+      5,\r
+      18\r
+    ],\r
+    [\r
+      5,\r
+      17\r
+    ],\r
+    [\r
+      4,\r
+      18\r
+    ],\r
+    [\r
+      4,\r
+      19\r
+    ],\r
+    [\r
+      3,\r
+      19\r
+    ],\r
+    [\r
+      3,\r
+      20\r
+    ],\r
+    [\r
+      5,\r
+      20\r
+    ],\r
+    [\r
+      5,\r
+      21\r
+    ],\r
+    [\r
+      4,\r
+      21\r
+    ],\r
+    [\r
+      6,\r
+      21\r
+    ],\r
+    [\r
+      6,\r
+      22\r
+    ],\r
+    [\r
+      5,\r
+      22\r
+    ],\r
+    [\r
+      4,\r
+      22\r
+    ],\r
+    [\r
+      4,\r
+      23\r
+    ],\r
+    [\r
+      5,\r
+      23\r
+    ],\r
+    [\r
+      7,\r
+      19\r
+    ],\r
+    [\r
+      8,\r
+      18\r
+    ],\r
+    [\r
+      11,\r
+      18\r
+    ],\r
+    [\r
+      12,\r
+      19\r
+    ],\r
+    [\r
+      13,\r
+      18\r
+    ],\r
+    [\r
+      13,\r
+      17\r
+    ],\r
+    [\r
+      14,\r
+      17\r
+    ],\r
+    [\r
+      14,\r
+      18\r
+    ],\r
+    [\r
+      15,\r
+      18\r
+    ],\r
+    [\r
+      15,\r
+      19\r
+    ],\r
+    [\r
+      16,\r
+      19\r
+    ],\r
+    [\r
+      16,\r
+      20\r
+    ],\r
+    [\r
+      14,\r
+      20\r
+    ],\r
+    [\r
+      14,\r
+      21\r
+    ],\r
+    [\r
+      13,\r
+      21\r
+    ],\r
+    [\r
+      15,\r
+      21\r
+    ],\r
+    [\r
+      15,\r
+      22\r
+    ],\r
+    [\r
+      15,\r
+      23\r
+    ],\r
+    [\r
+      14,\r
+      23\r
+    ],\r
+    [\r
+      14,\r
+      22\r
+    ],\r
+    [\r
+      13,\r
+      22\r
+    ]\r
+  ]\r
+}\r
diff --git a/game_of_life/data/settings.json b/game_of_life/data/settings.json
new file mode 100644 (file)
index 0000000..015f3dd
--- /dev/null
@@ -0,0 +1,22 @@
+{\r
+  "settings": {\r
+    "resolution": [\r
+      1600,\r
+      900\r
+    ],\r
+    "cell_width": 15,\r
+    "colour": {\r
+      "dead": [\r
+        0,\r
+        0,\r
+        0\r
+      ],\r
+      "alive": [\r
+        0,\r
+        255,\r
+        255\r
+      ]\r
+    },\r
+    "frame_limit": 75\r
+  }\r
+}
\ No newline at end of file
diff --git a/game_of_life/game_of_life.py b/game_of_life/game_of_life.py
new file mode 100644 (file)
index 0000000..d7f209d
--- /dev/null
@@ -0,0 +1,236 @@
+import json
+import sys
+from math import floor
+
+import pygame
+
+from .board import Board
+
+pygame.init()
+
+
+class GameOfLife:
+    def __init__(
+        self, resolution, cell_width, dead_colour, alive_colour, frame_limit, save_path
+    ):
+        self._resolution = resolution
+        self._cell_width = cell_width
+        self._dead_colour = dead_colour
+        self._alive_colour = alive_colour
+        self._frame_limit = frame_limit
+        self._save_path = save_path
+
+        # Required due to MOUSEBUTTONDOWN sometimes activating after get_pressed
+        self._changing = False
+
+        self.rows = int(round(self._resolution[1] / self._cell_width))
+        self.cols = int(round(self._resolution[0] / self._cell_width))
+        self.cell_width = self._resolution[0] / self.cols
+
+        self._save_game_keys = (
+            pygame.K_F1,
+            pygame.K_F2,
+            pygame.K_F3,
+            pygame.K_F4,
+            pygame.K_F5,
+            pygame.K_F6,
+            pygame.K_F7,
+            pygame.K_F8,
+            pygame.K_F9,
+        )
+
+        self._load_game_keys = (
+            pygame.K_1,
+            pygame.K_2,
+            pygame.K_3,
+            pygame.K_4,
+            pygame.K_5,
+            pygame.K_6,
+            pygame.K_7,
+            pygame.K_8,
+            pygame.K_9,
+            pygame.K_0,
+        )
+
+        self._screen = pygame.display.set_mode(self._resolution)
+        pygame.display.set_caption("Game of Life")
+        self.fpsClock = pygame.time.Clock()
+
+        self.board = Board((self.rows, self.cols))
+        self.board.create()
+        self.load()
+
+        self.paused = False
+        self.changing = False
+        self.lock_x = False
+        self.lock_y = False
+
+    @classmethod
+    def fromjsonfile(cls, settings_path, save_path):
+        with open(settings_path) as f:
+            file = json.loads(f.read())["settings"]
+        return cls.fromdict(file, save_path)
+
+    @classmethod
+    def fromdict(cls, dict, save_path):
+        resolution = dict["resolution"]
+        cell_width = dict["cell_width"]
+        dead_colour = dict["colour"]["dead"]
+        alive_colour = dict["colour"]["alive"]
+        frame_limit = dict["frame_limit"]
+        return cls(
+            resolution, cell_width, dead_colour, alive_colour, frame_limit, save_path
+        )
+
+    def save(self, code):
+        save = []
+        for cell in self.board.living_cells:
+            save.append(cell.pos)
+
+        with open(self._save_path) as rf:
+            try:
+                save_data = json.loads(rf.read())
+            except ValueError:
+                save_data = {}
+                print("Save File Empty")
+
+        print(self._save_game_keys.index(code) + 1)
+        save_data[f"save_{self._save_game_keys.index(code) + 1}"] = save
+        with open(self._save_path, "w") as wf:
+            wf.write(json.dumps(save_data))
+
+    def load(self, code="0"):
+        try:
+            if code == "0":
+                with open(self._save_path) as f:
+                    save = json.loads(f.read())["starting_position"]
+            else:
+                with open(self._save_path) as f:
+                    save = json.loads(f.read())[f"save_{code}"]
+        except KeyError:
+            print("save not found")
+        else:
+            self.board.create(save)
+
+    def _quit(self):
+        pygame.quit()
+        sys.exit()
+
+    def _handle_key(self, event):
+        if event.key == pygame.K_ESCAPE:
+            self._quit()
+        elif event.key == pygame.K_SPACE:
+            self.paused = not self.paused
+        elif event.key in self._save_game_keys:
+            self.save(event.key)
+        elif event.key in self._load_game_keys:
+            self.load(event.unicode)
+        elif event.key == pygame.K_r:
+            self.board.randomise_grid(0.1)
+        elif event.key == pygame.K_c:
+            self.board.create()
+        elif self.paused and event.key == pygame.K_RIGHT:
+            self.board.update_grid()
+        elif event.key == pygame.K_UP and self._frame_limit < 1000:
+            self._frame_limit += 10
+        elif event.key == pygame.K_DOWN:
+            if self._frame_limit > 10:
+                self._frame_limit -= 10
+            else:
+                self._frame_limit = 1
+        elif pygame.KMOD_SHIFT:
+            self.original_posx = int(floor(pygame.mouse.get_pos()[1] / self.cell_width))
+            self.original_posy = int(floor(pygame.mouse.get_pos()[0] / self.cell_width))
+
+    def _handle_events(self):
+        for event in pygame.event.get():
+            if event.type == pygame.QUIT:
+                self._quit()
+            elif event.type == pygame.KEYDOWN:
+                self._handle_key(event)
+            elif event.type == pygame.MOUSEBUTTONDOWN:
+                self._changing = True
+                self.original_posx = int(
+                    floor(pygame.mouse.get_pos()[1] / self.cell_width)
+                )
+                self.original_posy = int(
+                    floor(pygame.mouse.get_pos()[0] / self.cell_width)
+                )
+                self.original_cell_status = self.board.grid[self.original_posx][
+                    self.original_posy
+                ].status
+                self.board.change_cell_status((self.original_posx, self.original_posy))
+            elif pygame.mouse.get_pressed()[0] & self._changing:
+                # if event.type == pygame.MOUSEBUTTONDOWN:
+                #     self._changing = True
+                #     self.original_posx = int(
+                #         floor(pygame.mouse.get_pos()[1] / self.cell_width)
+                #     )
+                #     self.original_posy = int(
+                #         floor(pygame.mouse.get_pos()[0] / self.cell_width)
+                #     )
+                #     self.original_cell_status = self.board.grid[self.original_posx][
+                #         self.original_posy
+                #     ].status
+                #     self.board.change_cell_status(
+                #         (self.original_posx, self.original_posy)
+                #     )
+                self.posx = int(floor(pygame.mouse.get_pos()[1] / self.cell_width))
+                self.posy = int(floor(pygame.mouse.get_pos()[0] / self.cell_width))
+                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
+                    if self.posx != self.original_posx and not self.lock_x:
+                        self.lock_y = True
+                        if (
+                            self.board.grid[self.posx][self.original_posy].status
+                            == self.original_cell_status
+                        ):
+                            self.board.change_cell_status(
+                                (self.posx, self.original_posy)
+                            )
+                    if self.posy != self.original_posy and not self.lock_y:
+                        self.lock_x = True
+                        if (
+                            self.board.grid[self.original_posx][self.posy].status
+                            == self.original_cell_status
+                        ):
+                            self.board.change_cell_status(
+                                (self.original_posx, self.posy)
+                            )
+                elif (
+                    self.board.grid[self.posx][self.posy].status
+                    == self.original_cell_status
+                ):
+                    self.board.change_cell_status((self.posx, self.posy))
+            elif event.type == pygame.MOUSEBUTTONUP:
+                self._changing = False
+                self.lock_x = False
+                self.lock_y = False
+
+    def _draw_screen(self):
+        self._empty_space()
+
+        for cell in self.board.living_cells:
+            if cell:
+                pygame.draw.rect(
+                    self._screen,
+                    self._alive_colour,
+                    (
+                        cell.pos[1] * self.cell_width,
+                        cell.pos[0] * self.cell_width,
+                        self.cell_width,
+                        self.cell_width,
+                    ),
+                )
+
+        pygame.display.flip()
+
+    def _empty_space(self):
+        self._screen.fill(self._dead_colour)
+
+    def run(self):
+        while True:
+            self._draw_screen()
+            self._handle_events()
+            if not self.paused:
+                self.board.update_grid()
+                self.fpsClock.tick(self._frame_limit)
diff --git a/game_of_life/main.py b/game_of_life/main.py
new file mode 100644 (file)
index 0000000..150fded
--- /dev/null
@@ -0,0 +1,50 @@
+from .game_of_life import GameOfLife
+import os
+import shutil
+import sys
+
+
+def configs():
+    if os.name == "posix":
+        if "XDG_CONFIG_HOME" in os.environ:
+            CONFIG_PATH = os.path.join(os.environ["XDG_CONFIG_HOME"], "game-of-life")
+        else:
+            CONFIG_PATH = os.path.join(os.environ["HOME"], ".config/game-of-life")
+    elif os.name == "nt":
+        if "APPDATA" in os.environ:
+            CONFIG_PATH = os.path.join(os.environ["APPDATA"], "game-of-life")
+        else:
+            print(
+                "APPDATA is not set, something must be very wrong.",
+                file=sys.stderr,
+            )
+            sys.exit(1)
+    else:
+        print("Your OS is not supported", file=sys.stderr)
+
+    os.makedirs(CONFIG_PATH, exist_ok=True)
+    SAVE_PATH = os.path.join(CONFIG_PATH, "save.json")
+    SETTINGS_PATH = os.path.join(CONFIG_PATH, "settings.json")
+
+    if not os.path.exists(os.path.join(CONFIG_PATH, "save.json")):
+        shutil.copyfile(
+            os.path.join(os.path.dirname(os.path.realpath(__file__)), "data/save.json"),
+            SAVE_PATH,
+        )
+    if not os.path.exists(os.path.join(CONFIG_PATH, "settings.json")):
+        shutil.copyfile(
+            os.path.join(
+                os.path.dirname(os.path.realpath(__file__)), "data/settings.json"
+            ),
+            SETTINGS_PATH,
+        )
+    return SAVE_PATH, SETTINGS_PATH
+
+
+def main():
+    save_path, settings_path = configs()
+    GameOfLife.fromjsonfile(settings_path, save_path).run()
+
+
+if __name__ == "__main__":
+    main()
diff --git a/requirements.txt b/requirements.txt
new file mode 100644 (file)
index 0000000..7906c60
--- /dev/null
@@ -0,0 +1,11 @@
+black==22.6.0
+click==8.1.3
+flake8==4.0.1
+mccabe==0.6.1
+mypy-extensions==0.4.3
+pathspec==0.9.0
+platformdirs==2.5.2
+pycodestyle==2.8.0
+pyflakes==2.4.0
+pygame==2.1.2
+tomli==2.0.1
diff --git a/setup.py b/setup.py
new file mode 100644 (file)
index 0000000..ed9ecaf
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,20 @@
+from setuptools import setup, find_packages
+
+setup(
+    name="game_of_life",
+    version="0.0.1",
+    author="Georgios Atheridis",
+    author_email="atheridis@tutamail.com",
+    packages=find_packages(),
+    entry_points={
+        "console_scripts": [
+            "gameoflife=game_of_life.main:main",
+        ],
+    },
+    package_data={
+        "game_of_life": ["data/*.json"],
+    },
+    install_requires=[
+        "pygame",
+    ],
+)