implemented bishop's possible moves
Some checks are pending
pre-release / Pre Release (push) Waiting to run
Some checks are pending
pre-release / Pre Release (push) Waiting to run
This commit is contained in:
parent
bb0b8cdd27
commit
e95caa0015
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user