Compare commits
4 Commits
e95caa0015
...
bb0a3266c7
Author | SHA1 | Date | |
---|---|---|---|
|
bb0a3266c7 | ||
|
aabbaa83a8 | ||
|
96b9b3db86 | ||
|
6b0a134230 |
@ -1,4 +1,4 @@
|
|||||||
from logic.pieces.piece import Piece
|
# from logic.pieces.piece import Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ class Move:
|
|||||||
|
|
||||||
|
|
||||||
class PieceMove(Move):
|
class PieceMove(Move):
|
||||||
def __init__(self, piece: Piece, pos: Position,/, is_capturing: bool = False) -> None:
|
def __init__(self, piece: "Piece", pos: Position,/, is_capturing: bool = False) -> None:
|
||||||
super().__init__(is_capturing)
|
super().__init__(is_capturing)
|
||||||
self.piece = piece
|
self.piece = piece
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
|
@ -1,38 +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 _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]:
|
def legal_moves(self, board: "Board") -> list[Move]:
|
||||||
ret = []
|
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,5 +1,5 @@
|
|||||||
from logic.move import Move, PieceMove
|
from logic.move import Move, PieceMove
|
||||||
from logic.pieces.piece import Piece
|
from logic.pieces.piece import Colour, Piece
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
|
|
||||||
class Pawn(Piece):
|
class Pawn(Piece):
|
||||||
@ -8,23 +8,23 @@ class Pawn(Piece):
|
|||||||
|
|
||||||
# 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(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
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(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
ret.append(PieceMove(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
if self.colour == Piece.WHITE:
|
if self.colour == Colour.WHITE:
|
||||||
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
||||||
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
||||||
break
|
break
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from logic.move import Move, PieceMove
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@ -12,6 +13,33 @@ 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:
|
||||||
|
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
|
||||||
|
|
||||||
|
@ -1,5 +1,31 @@
|
|||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user