again extracted some logic, implemented rook legal

moves
This commit is contained in:
Karma Riuk 2025-01-30 17:11:36 +01:00
parent 96b9b3db86
commit aabbaa83a8
3 changed files with 32 additions and 19 deletions

View File

@ -1,24 +1,7 @@
from logic.move import Move, PieceMove
from logic.position import Position
from logic.move import Move
from .piece import Piece
class Bishop(Piece):
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 = []

View File

@ -13,6 +13,21 @@ class Piece:
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

View File

@ -1,5 +1,20 @@
from logic.move import Move
from .piece import 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