Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
ac85f3e6d3 | |||
ddfb95176b | |||
bb0a3266c7 | |||
aabbaa83a8 | |||
96b9b3db86 | |||
6b0a134230 | |||
e95caa0015 | |||
bb0b8cdd27 | |||
55ba824b13 | |||
16d107e5ea | |||
baa09135ee | |||
eae87f353b | |||
362b0e157d | |||
c900ebcfa0 | |||
c3e46017eb | |||
324484aa31 | |||
eca7a6ae0c | |||
ffe76b161a | |||
06f78487d9 |
13
src/controller/controller.py
Normal file
13
src/controller/controller.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from logic.board import Board
|
||||||
|
from view.view import View
|
||||||
|
|
||||||
|
|
||||||
|
class Controller:
|
||||||
|
def __init__(self, board: Board, view: View) -> None:
|
||||||
|
self._board = board
|
||||||
|
self._view = view
|
||||||
|
|
||||||
|
self._view.set_controller(self)
|
||||||
|
|
||||||
|
def on_tile_selected(self, x: int, y: int) -> None:
|
||||||
|
raise NotImplementedError(f"Cannot handle tile selected event, {type(self).__name__} did not implement it")
|
20
src/controller/gui_controller.py
Normal file
20
src/controller/gui_controller.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from logic.board import Board
|
||||||
|
from view.view import View
|
||||||
|
from .controller import Controller
|
||||||
|
|
||||||
|
|
||||||
|
class GuiController(Controller):
|
||||||
|
def __init__(self, board: Board, view: View) -> None:
|
||||||
|
super().__init__(board, view)
|
||||||
|
self._view.update_board(self._board, None, [])
|
||||||
|
|
||||||
|
def on_tile_selected(self, x: int, y: int) -> None:
|
||||||
|
piece = self._board.piece_at(x, y)
|
||||||
|
print(f"Clicked on {x, y}, {piece = }")
|
||||||
|
|
||||||
|
if piece:
|
||||||
|
self._view.update_board(self._board, piece, piece.legal_moves(self._board))
|
||||||
|
else:
|
||||||
|
self._view.update_board(self._board, None, [])
|
||||||
|
|
||||||
|
|
@ -4,38 +4,102 @@ from logic.pieces.knight import Knight
|
|||||||
from logic.pieces.queen import Queen
|
from logic.pieces.queen import Queen
|
||||||
from logic.pieces.rook import Rook
|
from logic.pieces.rook import Rook
|
||||||
from logic.pieces.pawn import Pawn
|
from logic.pieces.pawn import Pawn
|
||||||
from logic.pieces.piece import Piece
|
from logic.pieces.piece import Colour, Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
|
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
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()
|
||||||
|
index = 0
|
||||||
|
|
||||||
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:
|
index += 1
|
||||||
piece = Knight
|
if c == " ":
|
||||||
elif x == 2 or x == 5:
|
break
|
||||||
piece = Bishop
|
if c in pieces or c in pieces.upper():
|
||||||
elif x == 3:
|
pos = Position(x, y)
|
||||||
piece = Queen
|
piece = Board._piece_class_from_char(c)
|
||||||
elif x == 4:
|
if c.isupper():
|
||||||
piece = King
|
ret._white[pos] = piece(pos, Colour.WHITE)
|
||||||
assert piece != None, f"Didn't know which piece to assign for {x = }"
|
else:
|
||||||
self._white[pos_w_piece] = piece(pos_w_piece, Piece.WHITE)
|
ret._black[pos] = piece(pos, Colour.BLACK)
|
||||||
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
|
||||||
|
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:
|
def piece_at(self, x: int, y: int) -> Piece | None:
|
||||||
pos = Position(x, y)
|
pos = Position(x, y)
|
||||||
@ -47,6 +111,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()
|
|
||||||
|
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
|
@ -1,4 +1,21 @@
|
|||||||
|
from logic.move import Move
|
||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Bishop(Piece):
|
class Bishop(Piece):
|
||||||
pass
|
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
|
||||||
|
|
||||||
|
@ -1,5 +1,16 @@
|
|||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Knight(Piece):
|
class Knight(Piece):
|
||||||
pass
|
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
|
||||||
|
|
||||||
|
@ -1,31 +1,42 @@
|
|||||||
|
from logic.move import Move, PieceMove
|
||||||
|
from logic.pieces.piece import Colour, Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from logic.pieces.piece import Piece
|
|
||||||
|
|
||||||
class Pawn(Piece):
|
class Pawn(Piece):
|
||||||
def legal_moves(self, board) -> list[Position]:
|
def legal_moves(self, board) -> list[Move]:
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
# can we capture to the left?
|
# can we capture to the left?
|
||||||
if self.pos.x > 0 and (
|
if self.pos.x > 0 and (
|
||||||
(self.colour == self.WHITE and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y + 1)))
|
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y + 1)))
|
||||||
or
|
or
|
||||||
(self.colour == self.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
||||||
):
|
):
|
||||||
if capturable_piece.colour != self.colour:
|
if capturable_piece.colour != self.colour:
|
||||||
ret.append(capturable_piece.pos)
|
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
# can we capture to the right?
|
# can we capture to the right?
|
||||||
if self.pos.x < 7 and (
|
if self.pos.x < 7 and (
|
||||||
(self.colour == self.WHITE and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y + 1)))
|
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y + 1)))
|
||||||
or
|
or
|
||||||
(self.colour == self.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
||||||
):
|
):
|
||||||
if capturable_piece.colour != self.colour:
|
if capturable_piece.colour != self.colour:
|
||||||
ret.append(capturable_piece.pos)
|
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
if self.colour == Colour.WHITE:
|
||||||
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
||||||
break
|
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
||||||
ret.append(Position(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
|
return ret
|
||||||
|
@ -1,17 +1,47 @@
|
|||||||
|
from logic.move import Move, PieceMove
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class Piece:
|
class Colour(Enum):
|
||||||
WHITE = "white"
|
WHITE = "white"
|
||||||
BLACK = "black"
|
BLACK = "black"
|
||||||
|
|
||||||
def __init__(self, pos, colour) -> None:
|
class Piece:
|
||||||
|
def __init__(self, pos: Position, colour: Colour) -> None:
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
assert colour == self.WHITE or colour == self.BLACK, "The colour of the piece must be either Piece.WHITE or Piece.BLACK"
|
assert colour == Colour.WHITE or colour == Colour.BLACK, "The colour of the piece must be either Piece.WHITE or Piece.BLACK"
|
||||||
self.colour = colour
|
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:
|
def position(self) -> Position:
|
||||||
return self.pos
|
return self.pos
|
||||||
|
|
||||||
def legal_moves(self, board) -> list[Position]:
|
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")
|
raise NotImplementedError(f"Can't say what the legal moves are for {type(self).__name__}, the method hasn't been implemented yet")
|
||||||
|
@ -1,5 +1,32 @@
|
|||||||
|
from logic.move import Move
|
||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Queen(Piece):
|
class Queen(Piece):
|
||||||
pass
|
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
|
||||||
|
@ -1,5 +1,20 @@
|
|||||||
|
from logic.move import Move
|
||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Rook(Piece):
|
class Rook(Piece):
|
||||||
pass
|
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
|
||||||
|
@ -9,6 +9,12 @@ class Position:
|
|||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
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:
|
def __eq__(self, value: object, /) -> bool:
|
||||||
if type(value) != type(self):
|
if type(value) != type(self):
|
||||||
return False
|
return False
|
||||||
|
10
src/main.py
10
src/main.py
@ -1,10 +1,14 @@
|
|||||||
from logic.board import create_board
|
from controller.gui_controller import GuiController
|
||||||
|
from logic.board import Board
|
||||||
from view.gui import GUI
|
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 KQkq - 0 1"
|
||||||
|
board = Board.setup_FEN_position(initial_board_position)
|
||||||
|
|
||||||
view = GUI(board)
|
view = GUI()
|
||||||
|
|
||||||
|
controller = GuiController(board, view)
|
||||||
|
|
||||||
view.show()
|
view.show()
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
|
|
||||||
from logic.board import Board
|
from logic.board import Board
|
||||||
from logic.pieces.piece import Piece
|
from logic.move import Move
|
||||||
|
from logic.pieces.piece import Colour, Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from view.view import View
|
from view.view import View
|
||||||
|
|
||||||
class GUI(View):
|
class GUI(View):
|
||||||
def __init__(self, board: Board) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__(board)
|
super().__init__()
|
||||||
|
|
||||||
self.root = tk.Tk()
|
self.root = tk.Tk()
|
||||||
self.root.title("Chess Board")
|
self.root.title("Chess Board")
|
||||||
@ -18,33 +19,42 @@ class GUI(View):
|
|||||||
self.canvas = tk.Canvas(self.root, width=board_size, height=board_size)
|
self.canvas = tk.Canvas(self.root, width=board_size, height=board_size)
|
||||||
self.canvas.pack()
|
self.canvas.pack()
|
||||||
|
|
||||||
self.state = {"selected_piece": None, "legal_moves": []}
|
|
||||||
|
|
||||||
self.canvas.bind("<Button-1>", self._on_canvas_click)
|
self.canvas.bind("<Button-1>", self._on_canvas_click)
|
||||||
self._draw_chess_board()
|
|
||||||
|
|
||||||
|
def _on_canvas_click(self, event):
|
||||||
|
x, y = event.x // self.tile_size, event.y // self.tile_size
|
||||||
|
y = 7 - y
|
||||||
|
|
||||||
def _draw_chess_board(self):
|
self._controller.on_tile_selected(x, y)
|
||||||
|
|
||||||
|
def update_board(self, board: Board, selected_piece: Piece, legal_moves: list[Move]) -> None:
|
||||||
|
self.canvas.delete("all")
|
||||||
|
self._draw_chess_board(board, selected_piece, legal_moves)
|
||||||
|
|
||||||
|
def _draw_chess_board(self, board, selected_piece = None, legal_moves = []):
|
||||||
colours = ["#F0D9B5", "#B58863"] # Light and dark squares
|
colours = ["#F0D9B5", "#B58863"] # Light and dark squares
|
||||||
|
|
||||||
for y in range(8):
|
for y in range(8):
|
||||||
for x in range(8):
|
for x in range(8):
|
||||||
colour = colours[(x + y) % 2]
|
colour = colours[(x + y) % 2]
|
||||||
if self.state["selected_piece"] and Position(x, 7-y) in self.state["legal_moves"]:
|
if selected_piece is not None:
|
||||||
colour = "#ADD8E6" # Highlight legal moves
|
possible_positions = [move.pos for move in legal_moves]
|
||||||
|
if Position(x, 7-y) in possible_positions:
|
||||||
|
colour = "#ADD8E6" # Highlight legal moves
|
||||||
|
|
||||||
self.canvas.create_rectangle(
|
self.canvas.create_rectangle(
|
||||||
x * self.tile_size,
|
x * self.tile_size,
|
||||||
y * self.tile_size,
|
y * self.tile_size,
|
||||||
(x + 1) * self.tile_size,
|
(x + 1) * self.tile_size,
|
||||||
(y + 1) * self.tile_size,
|
(y + 1) * self.tile_size,
|
||||||
fill=colour
|
fill=colour,
|
||||||
|
outline=colour,
|
||||||
)
|
)
|
||||||
|
|
||||||
piece = self.board.piece_at(x, 7-y)
|
piece = board.piece_at(x, 7-y)
|
||||||
|
|
||||||
if piece:
|
if piece:
|
||||||
text_colour = "white" if piece.colour == Piece.WHITE else "black"
|
text_colour = "white" if piece.colour == Colour.WHITE else "black"
|
||||||
self.canvas.create_text(
|
self.canvas.create_text(
|
||||||
(x + 0.5) * self.tile_size,
|
(x + 0.5) * self.tile_size,
|
||||||
(y + 0.5) * self.tile_size,
|
(y + 0.5) * self.tile_size,
|
||||||
@ -72,23 +82,6 @@ class GUI(View):
|
|||||||
fill=text_colour,
|
fill=text_colour,
|
||||||
font=("Arial", 10, "bold")
|
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:
|
def show(self) -> None:
|
||||||
self.root.mainloop()
|
self.root.mainloop()
|
||||||
|
@ -1,10 +1,18 @@
|
|||||||
from logic.board import Board
|
from logic.board import Board
|
||||||
|
from logic.move import Move
|
||||||
|
from logic.pieces.piece import Piece
|
||||||
|
|
||||||
|
|
||||||
class View:
|
class View:
|
||||||
def __init__(self, board: Board) -> None:
|
def __init__(self) -> None:
|
||||||
self.board: Board = board
|
self._controller: "Controller" = None
|
||||||
|
|
||||||
def show(self) -> None:
|
def show(self) -> None:
|
||||||
raise NotImplementedError(f"Can't show the board, the show() method of {type(self)} is not implemented")
|
raise NotImplementedError(f"Can't show the board, the show() method of {type(self)} is not implemented")
|
||||||
|
|
||||||
|
def update_board(self, board: Board, selected_piece: Piece, legal_moves: list[Move]) -> None:
|
||||||
|
raise NotImplementedError(f"Can't update the board, the update_board() method of {type(self)} is not implemented")
|
||||||
|
|
||||||
|
def set_controller(self, controller: "Controller") -> None:
|
||||||
|
self._controller = controller
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user