From e95caa00154a105d8a2706a3453228e26951c8e1 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Thu, 30 Jan 2025 11:48:45 +0100 Subject: [PATCH] implemented bishop's possible moves --- src/logic/move.py | 10 +++++--- src/logic/pieces/bishop.py | 52 +++++++++++++++++++++++++++++++++++--- src/logic/position.py | 6 +++++ 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/logic/move.py b/src/logic/move.py index 0c4a550..6a6db1d 100644 --- a/src/logic/move.py +++ b/src/logic/move.py @@ -3,6 +3,9 @@ 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") @@ -13,11 +16,10 @@ class Move: class PieceMove(Move): def __init__(self, piece: Piece, pos: Position,/, is_capturing: bool = False) -> None: - super().__init__() + super().__init__(is_capturing) self.piece = piece self.pos = pos - self.is_capturing = is_capturing class Castle(Move, Enum): - KING_SIDE_CASTLE = "O-O" - QUEEN_SIDE_CASTLE = "O-O-O" + KING_SIDE_CASTLE = False + QUEEN_SIDE_CASTLE = False diff --git a/src/logic/pieces/bishop.py b/src/logic/pieces/bishop.py index 3e9e386..47e2880 100644 --- a/src/logic/pieces/bishop.py +++ b/src/logic/pieces/bishop.py @@ -1,6 +1,52 @@ -from logic.move import Move +from logic.move import Move, PieceMove +from logic.position import Position from .piece import Piece class Bishop(Piece): - def legal_moves(self, board) -> list[Move]: - return super().legal_moves(board) + 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 _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 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 + diff --git a/src/logic/position.py b/src/logic/position.py index 0113050..a636bd7 100644 --- a/src/logic/position.py +++ b/src/logic/position.py @@ -9,6 +9,12 @@ class Position: 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