pebbling a chessboard
authorGeorgios Atheridis <atheridis@tutamail.com>
Thu, 14 Jul 2022 09:09:22 +0000 (12:09 +0300)
committerGeorgios Atheridis <atheridis@tutamail.com>
Thu, 14 Jul 2022 09:09:22 +0000 (12:09 +0300)
14 files changed:
.flake8 [new file with mode: 0644]
.idea/.gitignore [new file with mode: 0644]
.idea/Math-Puzzles.iml [new file with mode: 0644]
.idea/inspectionProfiles/profiles_settings.xml [new file with mode: 0644]
.idea/misc.xml [new file with mode: 0644]
.idea/modules.xml [new file with mode: 0644]
.idea/vcs.xml [new file with mode: 0644]
LICENSE [new file with mode: 0644]
pebbling_a_chessboard/__init__.py [new file with mode: 0644]
pebbling_a_chessboard/board.py [new file with mode: 0644]
pebbling_a_chessboard/game.py [new file with mode: 0644]
pebbling_a_chessboard/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..541e00a
--- /dev/null
+++ b/.flake8
@@ -0,0 +1,3 @@
+[flake8]
+ignore = E203, W503
+max-line-length = 88
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644 (file)
index 0000000..eaf91e2
--- /dev/null
@@ -0,0 +1,3 @@
+# Default ignored files\r
+/shelf/\r
+/workspace.xml\r
diff --git a/.idea/Math-Puzzles.iml b/.idea/Math-Puzzles.iml
new file mode 100644 (file)
index 0000000..d9e6024
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<module type="PYTHON_MODULE" version="4">\r
+  <component name="NewModuleRootManager">\r
+    <content url="file://$MODULE_DIR$" />\r
+    <orderEntry type="inheritedJdk" />\r
+    <orderEntry type="sourceFolder" forTests="false" />\r
+  </component>\r
+</module>
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644 (file)
index 0000000..105ce2d
--- /dev/null
@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644 (file)
index 0000000..8d93904
--- /dev/null
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<project version="4">\r
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />\r
+</project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644 (file)
index 0000000..823dcb5
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<project version="4">\r
+  <component name="ProjectModuleManager">\r
+    <modules>\r
+      <module fileurl="file://$PROJECT_DIR$/.idea/Math-Puzzles.iml" filepath="$PROJECT_DIR$/.idea/Math-Puzzles.iml" />\r
+    </modules>\r
+  </component>\r
+</project>
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644 (file)
index 0000000..9661ac7
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<project version="4">\r
+  <component name="VcsDirectoryMappings">\r
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />\r
+  </component>\r
+</project>
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..3250ffb
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Georgios Atheridis
+
+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/pebbling_a_chessboard/__init__.py b/pebbling_a_chessboard/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/pebbling_a_chessboard/board.py b/pebbling_a_chessboard/board.py
new file mode 100644 (file)
index 0000000..43be71f
--- /dev/null
@@ -0,0 +1,42 @@
+class Board:\r
+    def __init__(self):\r
+        self.board = [[0 for _ in range(8)] for _ in range(8)]\r
+        self.board[0][0] = 1\r
+        self.board[0][1] = 1\r
+        self.board[1][0] = 1\r
+        self.win = False\r
+        self.print_board()\r
+\r
+    def _expand_board(self, units=1):\r
+        for i in range(units):\r
+            # add column\r
+            self.board.append([0 for _ in range(len(self.board[0]))])\r
+\r
+            # add row\r
+            for row in self.board:\r
+                row.append(0)\r
+\r
+    def play(self, posx, posy):\r
+        if self.board[posx][posy] == 1:\r
+            if self.board[posx + 1][posy] == 0 and self.board[posx][posy + 1] == 0:\r
+                self.board[posx + 1][posy] = 1\r
+                self.board[posx][posy + 1] = 1\r
+                self.board[posx][posy] = 0\r
+\r
+                if (\r
+                    posx + 1 == len(self.board) - 1\r
+                    or posy + 1 == len(self.board[posx]) - 1\r
+                ):\r
+                    self._expand_board()\r
+        self.print_board()\r
+        self._check_win_con()\r
+\r
+    def _check_win_con(self):\r
+        if self.board[0][0] + self.board[0][1] + self.board[1][0] == 0:\r
+            self.win = True\r
+\r
+    def print_board(self):\r
+        for row in self.board:\r
+            print(row)\r
+\r
+        print()\r
diff --git a/pebbling_a_chessboard/game.py b/pebbling_a_chessboard/game.py
new file mode 100644 (file)
index 0000000..6346d25
--- /dev/null
@@ -0,0 +1,105 @@
+import pygame\r
+import sys\r
+from .board import Board\r
+\r
+\r
+class Game:\r
+    def __init__(\r
+        self,\r
+        screen_size=(800, 800),\r
+        square_size=100,\r
+        dark_square_colour=(0, 0, 0),\r
+        light_square_colour=(255, 255, 255),\r
+    ):\r
+\r
+        pygame.init()\r
+\r
+        self.screen_size = screen_size\r
+        self.square_size = square_size\r
+        self.dark_square_colour = dark_square_colour\r
+        self.light_square_colour = light_square_colour\r
+\r
+        self.screen = pygame.display.set_mode(self.screen_size)\r
+        pygame.display.set_caption("Pebbling a Chessboard")\r
+\r
+        self.board = Board()\r
+        self._draw_board()\r
+\r
+    def _draw_board(self):\r
+        # draw the squares\r
+        for row in range(8):\r
+            for col in range(4):\r
+                if row % 2 == 0:\r
+                    pygame.draw.rect(\r
+                        self.screen,\r
+                        self.light_square_colour,\r
+                        (\r
+                            2 * col * self.square_size,\r
+                            row * self.square_size,\r
+                            self.square_size,\r
+                            self.square_size,\r
+                        ),\r
+                    )\r
+\r
+                    pygame.draw.rect(\r
+                        self.screen,\r
+                        self.dark_square_colour,\r
+                        (\r
+                            (2 * col + 1) * self.square_size,\r
+                            row * self.square_size,\r
+                            self.square_size,\r
+                            self.square_size,\r
+                        ),\r
+                    )\r
+                else:\r
+                    pygame.draw.rect(\r
+                        self.screen,\r
+                        self.dark_square_colour,\r
+                        (\r
+                            2 * col * self.square_size,\r
+                            row * self.square_size,\r
+                            self.square_size,\r
+                            self.square_size,\r
+                        ),\r
+                    )\r
+\r
+                    pygame.draw.rect(\r
+                        self.screen,\r
+                        self.light_square_colour,\r
+                        (\r
+                            (2 * col + 1) * self.square_size,\r
+                            row * self.square_size,\r
+                            self.square_size,\r
+                            self.square_size,\r
+                        ),\r
+                    )\r
+\r
+        for row in range(8):\r
+            for col in range(8):\r
+                if self.board.board[row][col]:\r
+                    posx = col * self.square_size + self.square_size / 2\r
+                    posy = self.screen_size[0] - (\r
+                        row * self.square_size + self.square_size / 2\r
+                    )\r
+                    pygame.draw.circle(self.screen, (255, 0, 0), (posx, posy), 40)\r
+\r
+        pygame.display.flip()\r
+\r
+    def handle_events(self):\r
+        for event in pygame.event.get():\r
+            if event.type == pygame.MOUSEBUTTONDOWN:\r
+                row = int((self.screen_size[0] - event.pos[1]) / self.square_size)\r
+                col = int(event.pos[0] / self.square_size)\r
+                if self.board.board[row][col]:\r
+                    self.board.play(row, col)\r
+            elif event.type == pygame.QUIT:\r
+                pygame.quit()\r
+                sys.exit()\r
+            elif event.type == pygame.KEYDOWN:\r
+                if event.key == pygame.K_r:\r
+                    self.board = Board()\r
+\r
+    def play(self):\r
+        while True:\r
+            self.handle_events()\r
+            self._draw_board()\r
diff --git a/pebbling_a_chessboard/main.py b/pebbling_a_chessboard/main.py
new file mode 100644 (file)
index 0000000..844f643
--- /dev/null
@@ -0,0 +1,9 @@
+from .game import Game
+
+
+def main():
+    Game().play()
+
+
+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..44858c4
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,17 @@
+from setuptools import setup, find_packages
+
+setup(
+    name="pebbling_a_chessboard",
+    version="0.0.1",
+    author="Georgios Atheridis",
+    author_email="atheridis@tutamail.com",
+    packages=find_packages(),
+    entry_points={
+        "console_scripts": [
+            "pebble-chessboard=pebbling_a_chessboard.main:main",
+        ],
+    },
+    install_requires=[
+        "pygame",
+    ],
+)