Compare commits
28 Commits
4493e3a110
...
v0.0.4
Author | SHA1 | Date | |
---|---|---|---|
bb0a3266c7 | |||
aabbaa83a8 | |||
96b9b3db86 | |||
6b0a134230 | |||
e95caa0015 | |||
bb0b8cdd27 | |||
55ba824b13 | |||
16d107e5ea | |||
baa09135ee | |||
eae87f353b | |||
362b0e157d | |||
c900ebcfa0 | |||
c3e46017eb | |||
324484aa31 | |||
eca7a6ae0c | |||
ffe76b161a | |||
06f78487d9 | |||
51648a5960 | |||
331c475c2a | |||
28ef132944 | |||
f7c0dcbd4b | |||
8f156616f0 | |||
a2ebb314eb | |||
2363b39484 | |||
60abfc794f | |||
4b3be20749 | |||
455fae8ad1 | |||
e2f6b5c8d8 |
20
.github/workflows/automatic-prerelease.yml
vendored
Normal file
20
.github/workflows/automatic-prerelease.yml
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
name: "pre-release"
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "main"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
pre-release:
|
||||||
|
name: "Pre Release"
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: "marvinpinto/action-automatic-releases@latest"
|
||||||
|
with:
|
||||||
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
prerelease: true
|
||||||
|
automatic_release_tag: "latest"
|
||||||
|
title: "Development Build"
|
18
.github/workflows/tagged-release.yml
vendored
Normal file
18
.github/workflows/tagged-release.yml
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
name: "tagged-release"
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tagged-release:
|
||||||
|
name: "Tagged Release"
|
||||||
|
runs-on: "ubuntu-latest"
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: "marvinpinto/action-automatic-releases@latest"
|
||||||
|
with:
|
||||||
|
repo_token: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
prerelease: false
|
113
src/logic/board.py
Normal file
113
src/logic/board.py
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
from logic.pieces.bishop import Bishop
|
||||||
|
from logic.pieces.king import King
|
||||||
|
from logic.pieces.knight import Knight
|
||||||
|
from logic.pieces.queen import Queen
|
||||||
|
from logic.pieces.rook import Rook
|
||||||
|
from logic.pieces.pawn import Pawn
|
||||||
|
from logic.pieces.piece import Colour, Piece
|
||||||
|
from logic.position import Position
|
||||||
|
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
|
class Board:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._white: 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
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _piece_class_from_char(c: str) -> Type[Piece]:
|
||||||
|
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}'")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def setup_FEN_position(position: str) -> "Board":
|
||||||
|
ret = Board()
|
||||||
|
index = 0
|
||||||
|
|
||||||
|
# -- Pieces
|
||||||
|
pieces = "prnbqk" # possible pieces
|
||||||
|
numbers = "12345678" # possible number of empty squares
|
||||||
|
|
||||||
|
x = 0
|
||||||
|
y = 7 # FEN starts from the top left, so 8th rank
|
||||||
|
for c in position:
|
||||||
|
index += 1
|
||||||
|
if c == " ":
|
||||||
|
break
|
||||||
|
if c in pieces or c in pieces.upper():
|
||||||
|
pos = Position(x, y)
|
||||||
|
piece = Board._piece_class_from_char(c)
|
||||||
|
if c.isupper():
|
||||||
|
ret._white[pos] = piece(pos, Colour.WHITE)
|
||||||
|
else:
|
||||||
|
ret._black[pos] = piece(pos, Colour.BLACK)
|
||||||
|
|
||||||
|
x += 1
|
||||||
|
continue
|
||||||
|
if c in numbers:
|
||||||
|
x += int(c)
|
||||||
|
if c == '/':
|
||||||
|
x = 0
|
||||||
|
y -= 1
|
||||||
|
|
||||||
|
|
||||||
|
# -- Active colour
|
||||||
|
if position[index] == "w":
|
||||||
|
ret._turn = Colour.WHITE
|
||||||
|
elif position[index] == "b":
|
||||||
|
ret._turn = Colour.BLACK
|
||||||
|
else:
|
||||||
|
raise ValueError(f"The FEN position is malformed, the active colour should be either 'w' or 'b', but is '{position[index]}'")
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
|
||||||
|
# -- Castling Rights
|
||||||
|
for c in position[index:]:
|
||||||
|
index += 1
|
||||||
|
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
|
||||||
|
if position[index] != "-":
|
||||||
|
ret._en_passant_target = position[index:index+2]
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def piece_at(self, x: int, y: int) -> Piece | None:
|
||||||
|
pos = Position(x, y)
|
||||||
|
white_piece = self._white.get(pos, None)
|
||||||
|
black_piece = self._black.get(pos, None)
|
||||||
|
|
||||||
|
assert white_piece == None or black_piece == None, f"There are two pieces at the same position {pos}, this shouldn't happen!"
|
||||||
|
|
||||||
|
if white_piece != None:
|
||||||
|
return white_piece
|
||||||
|
return black_piece
|
25
src/logic/move.py
Normal file
25
src/logic/move.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# from logic.pieces.piece import Piece
|
||||||
|
from logic.position import Position
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
class Move:
|
||||||
|
def __init__(self, is_capturing: bool) -> None:
|
||||||
|
self.is_capturing = is_capturing
|
||||||
|
|
||||||
|
def to_algebraic(self) -> str:
|
||||||
|
raise NotImplementedError("The move can't be translated to algbraic notation, as it was not implemented")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_algebraic(move: str) -> "Move":
|
||||||
|
raise NotImplementedError("The move can't be translated from algbraic notation, as it was not implemented")
|
||||||
|
|
||||||
|
|
||||||
|
class PieceMove(Move):
|
||||||
|
def __init__(self, piece: "Piece", pos: Position,/, is_capturing: bool = False) -> None:
|
||||||
|
super().__init__(is_capturing)
|
||||||
|
self.piece = piece
|
||||||
|
self.pos = pos
|
||||||
|
|
||||||
|
class Castle(Move, Enum):
|
||||||
|
KING_SIDE_CASTLE = False
|
||||||
|
QUEEN_SIDE_CASTLE = False
|
21
src/logic/pieces/bishop.py
Normal file
21
src/logic/pieces/bishop.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from logic.move import Move
|
||||||
|
from .piece import Piece
|
||||||
|
|
||||||
|
class Bishop(Piece):
|
||||||
|
def legal_moves(self, board: "Board") -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
# looking north east
|
||||||
|
ret.extend(self._look_direction(board, 1, 1))
|
||||||
|
|
||||||
|
# looking south east
|
||||||
|
ret.extend(self._look_direction(board, 1, -1))
|
||||||
|
|
||||||
|
# looking south west
|
||||||
|
ret.extend(self._look_direction(board, -1, -1))
|
||||||
|
|
||||||
|
# looking north west
|
||||||
|
ret.extend(self._look_direction(board, -1, 1))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
5
src/logic/pieces/king.py
Normal file
5
src/logic/pieces/king.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from .piece import Piece
|
||||||
|
|
||||||
|
class King(Piece):
|
||||||
|
pass
|
||||||
|
|
16
src/logic/pieces/knight.py
Normal file
16
src/logic/pieces/knight.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from .piece import Piece
|
||||||
|
|
||||||
|
class Knight(Piece):
|
||||||
|
def legal_moves(self, board: "Board") -> list["Move"]:
|
||||||
|
ret = []
|
||||||
|
for dx, dy in [
|
||||||
|
(+2, +1), (+1, +2), # north east
|
||||||
|
(+2, -1), (+1, -2), # south east
|
||||||
|
(-2, -1), (-1, -2), # south west
|
||||||
|
(-2, +1), (-1, +2), # north west
|
||||||
|
]:
|
||||||
|
move = self._move_for_position(board, self.pos.x + dx, self.pos.y + dy)
|
||||||
|
if move is not None:
|
||||||
|
ret.append(move)
|
||||||
|
return ret
|
||||||
|
|
42
src/logic/pieces/pawn.py
Normal file
42
src/logic/pieces/pawn.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from logic.move import Move, PieceMove
|
||||||
|
from logic.pieces.piece import Colour, Piece
|
||||||
|
from logic.position import Position
|
||||||
|
|
||||||
|
class Pawn(Piece):
|
||||||
|
def legal_moves(self, board) -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
# can we capture to the left?
|
||||||
|
if self.pos.x > 0 and (
|
||||||
|
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y + 1)))
|
||||||
|
or
|
||||||
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
||||||
|
):
|
||||||
|
if capturable_piece.colour != self.colour:
|
||||||
|
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
|
# can we capture to the right?
|
||||||
|
if self.pos.x < 7 and (
|
||||||
|
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y + 1)))
|
||||||
|
or
|
||||||
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
||||||
|
):
|
||||||
|
if capturable_piece.colour != self.colour:
|
||||||
|
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
|
if self.colour == Colour.WHITE:
|
||||||
|
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
||||||
|
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
||||||
|
break
|
||||||
|
pos = Position(self.pos.x, self.pos.y + dy)
|
||||||
|
ret.append(PieceMove(self, pos))
|
||||||
|
else:
|
||||||
|
for dy in range(1, 3 if self.pos.y == 6 else 2):
|
||||||
|
if self.pos.y - dy < 0 or board.piece_at(self.pos.x, self.pos.y - dy):
|
||||||
|
break
|
||||||
|
pos = Position(self.pos.x, self.pos.y - dy)
|
||||||
|
ret.append(PieceMove(self, pos))
|
||||||
|
|
||||||
|
|
||||||
|
print(ret)
|
||||||
|
return ret
|
47
src/logic/pieces/piece.py
Normal file
47
src/logic/pieces/piece.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from logic.move import Move, PieceMove
|
||||||
|
from logic.position import Position
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class Colour(Enum):
|
||||||
|
WHITE = "white"
|
||||||
|
BLACK = "black"
|
||||||
|
|
||||||
|
class Piece:
|
||||||
|
def __init__(self, pos: Position, colour: Colour) -> None:
|
||||||
|
self.pos = pos
|
||||||
|
assert colour == Colour.WHITE or colour == Colour.BLACK, "The colour of the piece must be either Piece.WHITE or Piece.BLACK"
|
||||||
|
self.colour = colour
|
||||||
|
|
||||||
|
def _look_direction(self, board: "Board", mult_dx: int, mult_dy: int):
|
||||||
|
ret = []
|
||||||
|
for d in range(1, 8):
|
||||||
|
dx = mult_dx * d
|
||||||
|
dy = mult_dy * d
|
||||||
|
|
||||||
|
move = self._move_for_position(board, self.pos.x + dx, self.pos.y + dy)
|
||||||
|
if move is None:
|
||||||
|
break
|
||||||
|
ret.append(move)
|
||||||
|
if move.is_capturing:
|
||||||
|
break
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def _move_for_position(self, board: "Board", x: int, y: int) -> Move | None:
|
||||||
|
if not Position.is_within_bounds(x, y):
|
||||||
|
return None
|
||||||
|
piece = board.piece_at(x, y)
|
||||||
|
|
||||||
|
if piece is None:
|
||||||
|
return PieceMove(self, Position(x, y))
|
||||||
|
|
||||||
|
if piece.colour != self.colour:
|
||||||
|
return PieceMove(self, Position(x, y), is_capturing=True)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def position(self) -> Position:
|
||||||
|
return self.pos
|
||||||
|
|
||||||
|
def legal_moves(self, board: "Board") -> list["Move"]:
|
||||||
|
raise NotImplementedError(f"Can't say what the legal moves are for {type(self).__name__}, the method hasn't been implemented yet")
|
31
src/logic/pieces/queen.py
Normal file
31
src/logic/pieces/queen.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
from .piece import Piece
|
||||||
|
|
||||||
|
class Queen(Piece):
|
||||||
|
def legal_moves(self, board: "Board") -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
# looking north east
|
||||||
|
ret.extend(self._look_direction(board, 1, 1))
|
||||||
|
|
||||||
|
# looking south east
|
||||||
|
ret.extend(self._look_direction(board, 1, -1))
|
||||||
|
|
||||||
|
# looking south west
|
||||||
|
ret.extend(self._look_direction(board, -1, -1))
|
||||||
|
|
||||||
|
# looking north west
|
||||||
|
ret.extend(self._look_direction(board, -1, 1))
|
||||||
|
|
||||||
|
# looking east
|
||||||
|
ret.extend(self._look_direction(board, 1, 0))
|
||||||
|
|
||||||
|
# looking south
|
||||||
|
ret.extend(self._look_direction(board, 0, -1))
|
||||||
|
|
||||||
|
# looking west
|
||||||
|
ret.extend(self._look_direction(board, -1, 0))
|
||||||
|
|
||||||
|
# looking north
|
||||||
|
ret.extend(self._look_direction(board, 0, 1))
|
||||||
|
|
||||||
|
return ret
|
20
src/logic/pieces/rook.py
Normal file
20
src/logic/pieces/rook.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from logic.move import Move
|
||||||
|
from .piece import Piece
|
||||||
|
|
||||||
|
class Rook(Piece):
|
||||||
|
def legal_moves(self, board: "Board") -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
# looking east
|
||||||
|
ret.extend(self._look_direction(board, 1, 0))
|
||||||
|
|
||||||
|
# looking south
|
||||||
|
ret.extend(self._look_direction(board, 0, -1))
|
||||||
|
|
||||||
|
# looking west
|
||||||
|
ret.extend(self._look_direction(board, -1, 0))
|
||||||
|
|
||||||
|
# looking north
|
||||||
|
ret.extend(self._look_direction(board, 0, 1))
|
||||||
|
|
||||||
|
return ret
|
31
src/logic/position.py
Normal file
31
src/logic/position.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
class Position:
|
||||||
|
_MIN_POS = 0
|
||||||
|
_MAX_POS = 7
|
||||||
|
|
||||||
|
def __init__(self, x, y) -> None:
|
||||||
|
assert x >= self._MIN_POS and x <= self._MAX_POS, f"Invalid argument: x should be between {self._MIN_POS} and {self._MAX_POS}, but is {x}"
|
||||||
|
assert y >= self._MIN_POS and y <= self._MAX_POS, f"Invalid argument: y should be between {self._MIN_POS} and {self._MAX_POS}, but is {y}"
|
||||||
|
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_within_bounds(x: int, y: int) -> bool:
|
||||||
|
return x >= Position._MIN_POS and x <= Position._MAX_POS \
|
||||||
|
and y >= Position._MIN_POS and y <= Position._MAX_POS
|
||||||
|
|
||||||
|
|
||||||
|
def __eq__(self, value: object, /) -> bool:
|
||||||
|
if type(value) != type(self):
|
||||||
|
return False
|
||||||
|
return value.x == self.x and value.y == self.y
|
||||||
|
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
return hash((self.x, self.y))
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return f"{self.x, self.y}"
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return str(self)
|
||||||
|
|
11
src/main.py
Normal file
11
src/main.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from logic.board import Board
|
||||||
|
from view.gui import GUI
|
||||||
|
from view.tui import TUI
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
initial_board_position = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||||
|
board = Board.setup_FEN_position(initial_board_position)
|
||||||
|
|
||||||
|
view = GUI(board)
|
||||||
|
|
||||||
|
view.show()
|
97
src/view/gui.py
Normal file
97
src/view/gui.py
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
|
||||||
|
from logic.board import Board
|
||||||
|
from logic.pieces.piece import Colour, 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"]:
|
||||||
|
possible_positions = [move.pos for move in self.state["legal_moves"]]
|
||||||
|
if Position(x, 7-y) in possible_positions:
|
||||||
|
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,
|
||||||
|
outline=colour,
|
||||||
|
)
|
||||||
|
|
||||||
|
piece = self.board.piece_at(x, 7-y)
|
||||||
|
|
||||||
|
if piece:
|
||||||
|
text_colour = "white" if piece.colour == Colour.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()
|
57
src/view/tui.py
Normal file
57
src/view/tui.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
from logic.board import Board
|
||||||
|
from logic.pieces.bishop import Bishop
|
||||||
|
from logic.pieces.king import King
|
||||||
|
from logic.pieces.knight import Knight
|
||||||
|
from logic.pieces.pawn import Pawn
|
||||||
|
from logic.pieces.piece import Piece
|
||||||
|
from logic.pieces.queen import Queen
|
||||||
|
from logic.pieces.rook import Rook
|
||||||
|
from view.view import View
|
||||||
|
|
||||||
|
class TUI(View):
|
||||||
|
def __init__(self, board: Board) -> None:
|
||||||
|
super().__init__(board)
|
||||||
|
|
||||||
|
def show(self) -> None:
|
||||||
|
board_view = [
|
||||||
|
[" " for _ in range(0, 8)]
|
||||||
|
for _ in range(0, 8)
|
||||||
|
]
|
||||||
|
|
||||||
|
for pos, piece in self.board._white.items():
|
||||||
|
board_view[pos.y][pos.x] = self.string_of(piece).upper()
|
||||||
|
|
||||||
|
for pos, piece in self.board._black.items():
|
||||||
|
board_view[pos.y][pos.x] = self.string_of(piece)
|
||||||
|
|
||||||
|
# we reverse the board because (0, 0) in in the bottom left, not top left
|
||||||
|
board_view.reverse()
|
||||||
|
print(self.to_string(board_view))
|
||||||
|
|
||||||
|
def to_string(self, board_view: list[list[str]]) -> str:
|
||||||
|
VER_SEP = "|"
|
||||||
|
HOR_SEP = "-"
|
||||||
|
ROW_SEP = HOR_SEP * (2*len(board_view[0]) + 1)
|
||||||
|
ret = ROW_SEP + "\n"
|
||||||
|
for row_view in board_view:
|
||||||
|
row_str = VER_SEP + VER_SEP.join(row_view) + VER_SEP
|
||||||
|
ret += row_str + "\n"
|
||||||
|
ret += ROW_SEP + "\n"
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def string_of(self, piece: Piece) -> str:
|
||||||
|
type_ = type(piece)
|
||||||
|
if type_ == Pawn:
|
||||||
|
return "p"
|
||||||
|
if type_ == Queen:
|
||||||
|
return "q"
|
||||||
|
if type_ == Bishop:
|
||||||
|
return "b"
|
||||||
|
if type_ == Knight:
|
||||||
|
return "n"
|
||||||
|
if type_ == Rook:
|
||||||
|
return "r"
|
||||||
|
if type_ == King:
|
||||||
|
return "k"
|
||||||
|
raise ValueError(f"Unknown piece type {type(piece)}")
|
10
src/view/view.py
Normal file
10
src/view/view.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from logic.board import Board
|
||||||
|
|
||||||
|
|
||||||
|
class View:
|
||||||
|
def __init__(self, board: Board) -> None:
|
||||||
|
self.board: Board = board
|
||||||
|
|
||||||
|
def show(self) -> None:
|
||||||
|
raise NotImplementedError(f"Can't show the board, the show() method of {type(self)} is not implemented")
|
||||||
|
|
Reference in New Issue
Block a user