again extracted some logic, implemented rook legal
moves
This commit is contained in:
parent
96b9b3db86
commit
aabbaa83a8
@ -1,24 +1,7 @@
|
|||||||
from logic.move import Move, PieceMove
|
from logic.move import Move
|
||||||
from logic.position import Position
|
|
||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Bishop(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]:
|
def legal_moves(self, board: "Board") -> list[Move]:
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
|
@ -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"
|
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:
|
def _move_for_position(self, board: "Board", x: int, y: int) -> Move | None:
|
||||||
if not Position.is_within_bounds(x, y):
|
if not Position.is_within_bounds(x, y):
|
||||||
return None
|
return None
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user