From: Georgios Atheridis Date: Thu, 14 Jul 2022 09:09:22 +0000 (+0300) Subject: pebbling a chessboard X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=4232e88d8fee4e4b04b636b428c81013677b4778;p=personal%2Fpebbling-a-chessboard.git pebbling a chessboard --- diff --git a/.flake8 b/.flake8 new file mode 100644 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 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Math-Puzzles.iml b/.idea/Math-Puzzles.iml new file mode 100644 index 0000000..d9e6024 --- /dev/null +++ b/.idea/Math-Puzzles.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8d93904 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..823dcb5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 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 index 0000000..e69de29 diff --git a/pebbling_a_chessboard/board.py b/pebbling_a_chessboard/board.py new file mode 100644 index 0000000..43be71f --- /dev/null +++ b/pebbling_a_chessboard/board.py @@ -0,0 +1,42 @@ +class Board: + def __init__(self): + self.board = [[0 for _ in range(8)] for _ in range(8)] + self.board[0][0] = 1 + self.board[0][1] = 1 + self.board[1][0] = 1 + self.win = False + self.print_board() + + def _expand_board(self, units=1): + for i in range(units): + # add column + self.board.append([0 for _ in range(len(self.board[0]))]) + + # add row + for row in self.board: + row.append(0) + + def play(self, posx, posy): + if self.board[posx][posy] == 1: + if self.board[posx + 1][posy] == 0 and self.board[posx][posy + 1] == 0: + self.board[posx + 1][posy] = 1 + self.board[posx][posy + 1] = 1 + self.board[posx][posy] = 0 + + if ( + posx + 1 == len(self.board) - 1 + or posy + 1 == len(self.board[posx]) - 1 + ): + self._expand_board() + self.print_board() + self._check_win_con() + + def _check_win_con(self): + if self.board[0][0] + self.board[0][1] + self.board[1][0] == 0: + self.win = True + + def print_board(self): + for row in self.board: + print(row) + + print() diff --git a/pebbling_a_chessboard/game.py b/pebbling_a_chessboard/game.py new file mode 100644 index 0000000..6346d25 --- /dev/null +++ b/pebbling_a_chessboard/game.py @@ -0,0 +1,105 @@ +import pygame +import sys +from .board import Board + + +class Game: + def __init__( + self, + screen_size=(800, 800), + square_size=100, + dark_square_colour=(0, 0, 0), + light_square_colour=(255, 255, 255), + ): + + pygame.init() + + self.screen_size = screen_size + self.square_size = square_size + self.dark_square_colour = dark_square_colour + self.light_square_colour = light_square_colour + + self.screen = pygame.display.set_mode(self.screen_size) + pygame.display.set_caption("Pebbling a Chessboard") + + self.board = Board() + self._draw_board() + + def _draw_board(self): + # draw the squares + for row in range(8): + for col in range(4): + if row % 2 == 0: + pygame.draw.rect( + self.screen, + self.light_square_colour, + ( + 2 * col * self.square_size, + row * self.square_size, + self.square_size, + self.square_size, + ), + ) + + pygame.draw.rect( + self.screen, + self.dark_square_colour, + ( + (2 * col + 1) * self.square_size, + row * self.square_size, + self.square_size, + self.square_size, + ), + ) + else: + pygame.draw.rect( + self.screen, + self.dark_square_colour, + ( + 2 * col * self.square_size, + row * self.square_size, + self.square_size, + self.square_size, + ), + ) + + pygame.draw.rect( + self.screen, + self.light_square_colour, + ( + (2 * col + 1) * self.square_size, + row * self.square_size, + self.square_size, + self.square_size, + ), + ) + + for row in range(8): + for col in range(8): + if self.board.board[row][col]: + posx = col * self.square_size + self.square_size / 2 + posy = self.screen_size[0] - ( + row * self.square_size + self.square_size / 2 + ) + pygame.draw.circle(self.screen, (255, 0, 0), (posx, posy), 40) + + pygame.display.flip() + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.MOUSEBUTTONDOWN: + row = int((self.screen_size[0] - event.pos[1]) / self.square_size) + col = int(event.pos[0] / self.square_size) + if self.board.board[row][col]: + self.board.play(row, col) + elif event.type == pygame.QUIT: + pygame.quit() + sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_r: + self.board = Board() + + def play(self): + while True: + self.handle_events() + self._draw_board() diff --git a/pebbling_a_chessboard/main.py b/pebbling_a_chessboard/main.py new file mode 100644 index 0000000..844f643 --- /dev/null +++ b/pebbling_a_chessboard/main.py @@ -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 index 0000000..7906c60 --- /dev/null +++ b/requirements.txt @@ -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 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", + ], +)