Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
06f78487d9 | |||
51648a5960 | |||
331c475c2a | |||
28ef132944 | |||
f7c0dcbd4b |
1
.github/workflows/automatic-prerelease.yml
vendored
1
.github/workflows/automatic-prerelease.yml
vendored
@ -17,3 +17,4 @@ jobs:
|
|||||||
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
prerelease: true
|
prerelease: true
|
||||||
automatic_release_tag: "latest"
|
automatic_release_tag: "latest"
|
||||||
|
title: "Development Build"
|
||||||
|
@ -7,35 +7,100 @@ from logic.pieces.pawn import Pawn
|
|||||||
from logic.pieces.piece import Piece
|
from logic.pieces.piece import Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
|
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
|
KING_SIDE_CASTLE = "king side castle"
|
||||||
|
QUEEN_SIDE_CASTLE = "queen side castle"
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._white: dict[Position, Piece] = {}
|
self._white: dict[Position, Piece] = {}
|
||||||
self._black: dict[Position, Piece] = {}
|
self._black: dict[Position, Piece] = {}
|
||||||
|
self._turn = None
|
||||||
|
self._white_castling_write = set()
|
||||||
|
self._black_castling_write = set()
|
||||||
|
self._en_passant_target = None
|
||||||
|
|
||||||
for x in range(8):
|
@staticmethod
|
||||||
pos_w_pawn = Position(x, 1)
|
def _piece_class_from_char(c: str) -> Type[Piece]:
|
||||||
pos_b_pawn = Position(x, 6)
|
assert len(c) == 1, f"The piece {c} isn't denoted by 1 character"
|
||||||
|
c = c.lower()
|
||||||
|
if c == "p":
|
||||||
|
return Pawn
|
||||||
|
if c == "r":
|
||||||
|
return Rook
|
||||||
|
if c == "n":
|
||||||
|
return Knight
|
||||||
|
if c == "b":
|
||||||
|
return Bishop
|
||||||
|
if c == "q":
|
||||||
|
return Queen
|
||||||
|
if c == "k":
|
||||||
|
return King
|
||||||
|
raise ValueError(f"Unknown piece '{c}'")
|
||||||
|
|
||||||
self._white[pos_w_pawn] = Pawn(pos_w_pawn, Piece.WHITE)
|
@staticmethod
|
||||||
self._black[pos_b_pawn] = Pawn(pos_b_pawn, Piece.BLACK)
|
def setup_FEN_position(position: str) -> "Board":
|
||||||
|
ret = Board()
|
||||||
|
|
||||||
pos_w_piece = Position(x, 0)
|
# -- Pieces
|
||||||
pos_b_piece = Position(x, 7)
|
pieces = "prnbqk" # possible pieces
|
||||||
|
numbers = "12345678" # possible number of empty squares
|
||||||
|
|
||||||
piece = None
|
x = 0
|
||||||
if x == 0 or x == 7:
|
y = 7 # FEN starts from the top left, so 8th rank
|
||||||
piece = Rook
|
for c in position:
|
||||||
elif x == 1 or x == 6:
|
if c == " ":
|
||||||
piece = Knight
|
break
|
||||||
elif x == 2 or x == 5:
|
if c in pieces or c in pieces.upper():
|
||||||
piece = Bishop
|
pos = Position(x, y)
|
||||||
elif x == 3:
|
piece = Board._piece_class_from_char(c)
|
||||||
piece = Queen
|
if c.isupper():
|
||||||
elif x == 4:
|
ret._white[pos] = piece(pos, Piece.WHITE)
|
||||||
piece = King
|
else:
|
||||||
assert piece != None, f"Didn't know which piece to assign for {x = }"
|
ret._black[pos] = piece(pos, Piece.BLACK)
|
||||||
self._white[pos_w_piece] = piece(pos_w_piece, Piece.WHITE)
|
|
||||||
self._black[pos_b_piece] = piece(pos_b_piece, Piece.BLACK)
|
x += 1
|
||||||
|
continue
|
||||||
|
if c in numbers:
|
||||||
|
x += int(c)
|
||||||
|
if c == '/':
|
||||||
|
x = 0
|
||||||
|
y -= 1
|
||||||
|
|
||||||
|
|
||||||
|
# -- Active colour
|
||||||
|
index = position.find(" ") # find the first space
|
||||||
|
index += 1
|
||||||
|
if position[index] == "w":
|
||||||
|
ret._turn = Piece.WHITE
|
||||||
|
elif position[index] == "b":
|
||||||
|
ret._turn = Piece.BLACK
|
||||||
|
else:
|
||||||
|
raise ValueError(f"The FEN position is malformed, the active colour should be either 'w' or 'b', but is '{position[index]}'")
|
||||||
|
|
||||||
|
|
||||||
|
# -- Castling Rights
|
||||||
|
for c in position:
|
||||||
|
if c == "-" or c == " ":
|
||||||
|
break
|
||||||
|
|
||||||
|
sides = "kq"
|
||||||
|
assert c in sides or c in sides.upper(), f"The FEN position is malformed, the castling rights should be either k or q (both either lower- or upper-case), instead is '{c}'"
|
||||||
|
if c == "K":
|
||||||
|
ret._white_castling_write.add(Board.KING_SIDE_CASTLE)
|
||||||
|
if c == "Q":
|
||||||
|
ret._white_castling_write.add(Board.QUEEN_SIDE_CASTLE)
|
||||||
|
if c == "k":
|
||||||
|
ret._black_castling_write.add(Board.KING_SIDE_CASTLE)
|
||||||
|
if c == "q":
|
||||||
|
ret._black_castling_write.add(Board.QUEEN_SIDE_CASTLE)
|
||||||
|
|
||||||
|
# -- En passant target
|
||||||
|
index = position.find(" ", index + 1)
|
||||||
|
if position[index] != "-":
|
||||||
|
ret._en_passant_target = position[index:index+2]
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
def piece_at(self, x: int, y: int) -> Piece | None:
|
def piece_at(self, x: int, y: int) -> Piece | None:
|
||||||
pos = Position(x, y)
|
pos = Position(x, y)
|
||||||
@ -47,6 +112,3 @@ class Board:
|
|||||||
if white_piece != None:
|
if white_piece != None:
|
||||||
return white_piece
|
return white_piece
|
||||||
return black_piece
|
return black_piece
|
||||||
|
|
||||||
def create_board():
|
|
||||||
return Board()
|
|
||||||
|
@ -2,8 +2,8 @@ from logic.position import Position
|
|||||||
|
|
||||||
|
|
||||||
class Piece:
|
class Piece:
|
||||||
WHITE = 0
|
WHITE = "white"
|
||||||
BLACK = 1
|
BLACK = "black"
|
||||||
|
|
||||||
def __init__(self, pos, colour) -> None:
|
def __init__(self, pos, colour) -> None:
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
from logic.board import create_board
|
from logic.board import Board, create_board
|
||||||
|
from view.gui import GUI
|
||||||
from view.tui import TUI
|
from view.tui import TUI
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
board = create_board()
|
initial_board_position = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w"
|
||||||
|
board = Board.setup_FEN_position(initial_board_position)
|
||||||
|
|
||||||
view = TUI(board)
|
view = GUI(board)
|
||||||
|
|
||||||
view.show()
|
view.show()
|
||||||
|
94
src/view/gui.py
Normal file
94
src/view/gui.py
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from logic.board import Board
|
||||||
|
from logic.pieces.piece import Piece
|
||||||
|
from logic.position import Position
|
||||||
|
from view.view import View
|
||||||
|
|
||||||
|
class GUI(View):
|
||||||
|
def __init__(self, board: Board) -> None:
|
||||||
|
super().__init__(board)
|
||||||
|
|
||||||
|
self.root = tk.Tk()
|
||||||
|
self.root.title("Chess Board")
|
||||||
|
|
||||||
|
self.tile_size = 80
|
||||||
|
board_size = self.tile_size * 8
|
||||||
|
|
||||||
|
self.canvas = tk.Canvas(self.root, width=board_size, height=board_size)
|
||||||
|
self.canvas.pack()
|
||||||
|
|
||||||
|
self.state = {"selected_piece": None, "legal_moves": []}
|
||||||
|
|
||||||
|
self.canvas.bind("<Button-1>", self._on_canvas_click)
|
||||||
|
self._draw_chess_board()
|
||||||
|
|
||||||
|
|
||||||
|
def _draw_chess_board(self):
|
||||||
|
colours = ["#F0D9B5", "#B58863"] # Light and dark squares
|
||||||
|
|
||||||
|
for y in range(8):
|
||||||
|
for x in range(8):
|
||||||
|
colour = colours[(x + y) % 2]
|
||||||
|
if self.state["selected_piece"] and Position(x, 7-y) in self.state["legal_moves"]:
|
||||||
|
colour = "#ADD8E6" # Highlight legal moves
|
||||||
|
|
||||||
|
self.canvas.create_rectangle(
|
||||||
|
x * self.tile_size,
|
||||||
|
y * self.tile_size,
|
||||||
|
(x + 1) * self.tile_size,
|
||||||
|
(y + 1) * self.tile_size,
|
||||||
|
fill=colour
|
||||||
|
)
|
||||||
|
|
||||||
|
piece = self.board.piece_at(x, 7-y)
|
||||||
|
|
||||||
|
if piece:
|
||||||
|
text_colour = "white" if piece.colour == Piece.WHITE else "black"
|
||||||
|
self.canvas.create_text(
|
||||||
|
(x + 0.5) * self.tile_size,
|
||||||
|
(y + 0.5) * self.tile_size,
|
||||||
|
text=piece.__class__.__name__[0],
|
||||||
|
fill=text_colour,
|
||||||
|
font=("Arial", 32, "bold")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Cell annotations
|
||||||
|
text_colour = colours[(x + y + 1) % 2] # the other colour
|
||||||
|
|
||||||
|
if x == 0: # numbers in the top left of the first column
|
||||||
|
self.canvas.create_text(
|
||||||
|
(x + 0.15) * self.tile_size,
|
||||||
|
(y + 0.15) * self.tile_size,
|
||||||
|
text=8-y,
|
||||||
|
fill=text_colour,
|
||||||
|
font=("Arial", 10, "bold")
|
||||||
|
)
|
||||||
|
if y == 7: # numbers in the top left of the first column
|
||||||
|
self.canvas.create_text(
|
||||||
|
(x + 0.85) * self.tile_size,
|
||||||
|
(y + 0.85) * self.tile_size,
|
||||||
|
text="abcdefgh"[x],
|
||||||
|
fill=text_colour,
|
||||||
|
font=("Arial", 10, "bold")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _on_canvas_click(self, event):
|
||||||
|
x, y = event.x // self.tile_size, event.y // self.tile_size
|
||||||
|
y = 7 - y
|
||||||
|
piece = self.board.piece_at(x, y)
|
||||||
|
print(f"Clicked on {x, y}, {piece = }")
|
||||||
|
|
||||||
|
if piece:
|
||||||
|
self.state["selected_piece"] = piece
|
||||||
|
self.state["legal_moves"] = piece.legal_moves(self.board)
|
||||||
|
else:
|
||||||
|
self.state["selected_piece"] = None
|
||||||
|
self.state["legal_moves"] = []
|
||||||
|
|
||||||
|
self.canvas.delete("all")
|
||||||
|
self._draw_chess_board()
|
||||||
|
|
||||||
|
def show(self) -> None:
|
||||||
|
self.root.mainloop()
|
Reference in New Issue
Block a user