Skip to content

Commit

Permalink
Implement board flip functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ahira-justice committed Jul 10, 2023
1 parent 7597175 commit 7f82628
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ while True:
display.check_for_quit()
display.update(valid_fen, game_board)

display.terminate()
# board flip interface
if not game_board.flipped:
display.flip(game_board)

```
Expand Down
18 changes: 16 additions & 2 deletions chessboard/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def __init__(self, bg_color, display_surf):
self.display_surf = display_surf

self.piece_rect = []
self.current_fen = ''
self.flipped = False
self.fp = FenParser()

def display_board(self):
self.display_surf.fill(self.bg_color)
Expand All @@ -58,9 +61,14 @@ def draw_tiles(self):
self.display_surf.blit(Board.w_tile, Board.board_rect[i - 1][j - 1])

def update_pieces(self, fen):
self.current_fen = fen
self.display_board()
self.draw_pieces(fen)

def flip(self):
self.flipped = not self.flipped
self.update_pieces(self.current_fen)

def create_piece(self, color, piece_type, position):
piece = Piece(color, piece_type, self.display_surf)
piece.set_position(position)
Expand Down Expand Up @@ -91,10 +99,16 @@ def create_piece_for_side(self, board_piece, color, position):
piece = self.create_piece(color, PieceType.ROOK, position)
self.piece_rect.append(piece)

def parse_fen(self, fen):
return self.fp.parse(fen)

def draw_pieces(self, fen):
self.piece_rect = []
fp = FenParser(fen)
fen_board = fp.parse()
fen_board = self.fp.parse(fen)

if self.flipped:
fen_board.reverse()
fen_board = list(map(lambda x: x[::-1], fen_board))

for i in range(len(fen_board)):
for j in range(len(fen_board[i])):
Expand Down
8 changes: 8 additions & 0 deletions chessboard/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ def update(fen, game_board):

pygame.display.update()
fps_clock.tick(FPS)


def flip(game_board: Board):
check_for_quit()
game_board.flip()

pygame.display.update()
fps_clock.tick(FPS)
12 changes: 2 additions & 10 deletions chessboard/fenparser.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
"""
https://github.com/tlehman/fenparser
"""


import re
from itertools import chain


class FenParser():
def __init__(self, fen_str):
self.fen_str = fen_str


def parse(self):
ranks = self.fen_str.split(" ")[0].split("/")
def parse(self, fen_str):
ranks = fen_str.split(" ")[0].split("/")
pieces_on_all_ranks = [self.parse_rank(rank) for rank in ranks]
return pieces_on_all_ranks

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
EMAIL = '[email protected]'
AUTHOR = 'Ahira Adefokun'
REQUIRES_PYTHON = '>=3.6.0'
VERSION = '0.3.1'
VERSION = '0.4.0'

# What packages are required for this module to be executed?
REQUIRED = [
Expand Down

0 comments on commit 7f82628

Please sign in to comment.