ChessWithQuests

Move (model.game.move)

model.game.move

Move representation tracking positions, piece transitions, captures, and promotions.

Move

Encapsulates a chess move with coordinates, piece states, and execution logic.

Source code in src/model/game/move.py
class Move:
    """Encapsulates a chess move with coordinates, piece states, and execution logic."""

    def __init__(
        self,
        start_pos: Tuple[int, int],
        end_pos: Tuple[int, int],
        piece: Optional[Any] = None,
        move_type: str = "normal",
        captured_piece: Optional[Any] = None,
        promotion_piece: Optional[Any] = None,
    ):
        """Initialize a Move instance.

        Args:
            start_pos: Starting (row, col) coordinates.
            end_pos: Destination (row, col) coordinates.
            piece: Moving piece instance, or None to infer from board.
            move_type: Type of move (e.g. "normal", "castling", "en_passant").
            captured_piece: Captured piece instance if any.
            promotion_piece: New piece instance if move involves pawn promotion.
        """
        self.start_pos = tuple(start_pos)
        self.end_pos = tuple(end_pos)
        self.piece = piece
        self.move_type = move_type
        self.captured_piece = captured_piece
        self.promotion_piece = promotion_piece

    def validate(self, board: Optional[Any] = None) -> bool:
        """Validate geometric boundaries and basic board rules for this move.

        Args:
            board: Optional Board instance to verify piece existence and target color.

        Returns:
            True if basic validity checks pass, False otherwise.
        """
        if not (0 <= self.start_pos[0] < 8 and 0 <= self.start_pos[1] < 8):
            return False
        if not (0 <= self.end_pos[0] < 8 and 0 <= self.end_pos[1] < 8):
            return False
        if self.start_pos == self.end_pos:
            return False
        if board is not None:
            moving = board.get_piece_at(self.start_pos)
            if moving is None:
                return False
            target = board.get_piece_at(self.end_pos)
            if target is not None and target.getColor() == moving.getColor():
                return False
        return True

    def execute(self, board: Any) -> bool:
        """Execute this move on the given board.

        Args:
            board: Board instance on which the move is applied.

        Returns:
            True if the move executed successfully, False otherwise.
        """
        if self.piece is None:
            self.piece = board.get_piece_at(self.start_pos)
        if self.piece is None:
            return False

        self.captured_piece = board.get_piece_at(self.end_pos)
        success = board.move_piece(self.start_pos, self.end_pos)
        if success and self.promotion_piece is not None:
            board.replace_piece(self.end_pos, self.promotion_piece)
        return success
__init__(start_pos, end_pos, piece=None, move_type='normal', captured_piece=None, promotion_piece=None)

Initialize a Move instance.

Parameters:

Name Type Description Default
start_pos Tuple[int, int]

Starting (row, col) coordinates.

required
end_pos Tuple[int, int]

Destination (row, col) coordinates.

required
piece Optional[Any]

Moving piece instance, or None to infer from board.

None
move_type str

Type of move (e.g. "normal", "castling", "en_passant").

'normal'
captured_piece Optional[Any]

Captured piece instance if any.

None
promotion_piece Optional[Any]

New piece instance if move involves pawn promotion.

None
Source code in src/model/game/move.py
def __init__(
    self,
    start_pos: Tuple[int, int],
    end_pos: Tuple[int, int],
    piece: Optional[Any] = None,
    move_type: str = "normal",
    captured_piece: Optional[Any] = None,
    promotion_piece: Optional[Any] = None,
):
    """Initialize a Move instance.

    Args:
        start_pos: Starting (row, col) coordinates.
        end_pos: Destination (row, col) coordinates.
        piece: Moving piece instance, or None to infer from board.
        move_type: Type of move (e.g. "normal", "castling", "en_passant").
        captured_piece: Captured piece instance if any.
        promotion_piece: New piece instance if move involves pawn promotion.
    """
    self.start_pos = tuple(start_pos)
    self.end_pos = tuple(end_pos)
    self.piece = piece
    self.move_type = move_type
    self.captured_piece = captured_piece
    self.promotion_piece = promotion_piece
execute(board)

Execute this move on the given board.

Parameters:

Name Type Description Default
board Any

Board instance on which the move is applied.

required

Returns:

Type Description
bool

True if the move executed successfully, False otherwise.

Source code in src/model/game/move.py
def execute(self, board: Any) -> bool:
    """Execute this move on the given board.

    Args:
        board: Board instance on which the move is applied.

    Returns:
        True if the move executed successfully, False otherwise.
    """
    if self.piece is None:
        self.piece = board.get_piece_at(self.start_pos)
    if self.piece is None:
        return False

    self.captured_piece = board.get_piece_at(self.end_pos)
    success = board.move_piece(self.start_pos, self.end_pos)
    if success and self.promotion_piece is not None:
        board.replace_piece(self.end_pos, self.promotion_piece)
    return success
validate(board=None)

Validate geometric boundaries and basic board rules for this move.

Parameters:

Name Type Description Default
board Optional[Any]

Optional Board instance to verify piece existence and target color.

None

Returns:

Type Description
bool

True if basic validity checks pass, False otherwise.

Source code in src/model/game/move.py
def validate(self, board: Optional[Any] = None) -> bool:
    """Validate geometric boundaries and basic board rules for this move.

    Args:
        board: Optional Board instance to verify piece existence and target color.

    Returns:
        True if basic validity checks pass, False otherwise.
    """
    if not (0 <= self.start_pos[0] < 8 and 0 <= self.start_pos[1] < 8):
        return False
    if not (0 <= self.end_pos[0] < 8 and 0 <= self.end_pos[1] < 8):
        return False
    if self.start_pos == self.end_pos:
        return False
    if board is not None:
        moving = board.get_piece_at(self.start_pos)
        if moving is None:
            return False
        target = board.get_piece_at(self.end_pos)
        if target is not None and target.getColor() == moving.getColor():
            return False
    return True
ChessWithQuests