pawns have their legal move list
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled

This commit is contained in:
Karma Riuk 2025-01-29 12:08:03 +01:00
parent a2ebb314eb
commit 8f156616f0
2 changed files with 38 additions and 4 deletions

View File

@ -37,6 +37,16 @@ class Board:
self._white[pos_w_piece] = piece(pos_w_piece, Piece.WHITE)
self._black[pos_b_piece] = piece(pos_b_piece, Piece.BLACK)
def piece_at(self, x: int, y: int) -> Piece | None:
pos = Position(x, y)
white_piece = self._white.get(pos, None)
black_piece = self._black.get(pos, None)
assert white_piece == None or black_piece == None, f"There are two pieces at the same position {pos}, this shouldn't happen!"
if white_piece != None:
return white_piece
return black_piece
def create_board():
return Board()

View File

@ -1,7 +1,31 @@
from .piece import Piece
from logic.position import Position
from logic.pieces.piece import Piece
class Pawn(Piece):
def __init__(self, pos) -> None:
super().__init__(pos)
self.already_moved = False
def legal_moves(self, board) -> list[Position]:
ret = []
# can we capture to the left?
if self.pos.x > 0 and (
(self.colour == self.WHITE and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y + 1)))
or
(self.colour == self.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
):
if capturable_piece.colour != self.colour:
ret.append(capturable_piece.pos)
# can we capture to the right?
if self.pos.x < 7 and (
(self.colour == self.WHITE and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y + 1)))
or
(self.colour == self.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
):
if capturable_piece.colour != self.colour:
ret.append(capturable_piece.pos)
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):
break
ret.append(Position(self.pos.x, self.pos.y + dy))
return ret