ChessWithQuests

Board (model.game.board)

model.game.board

Chessboard representation managing piece layout, bounds, and piece movements.

Board

Represents a chessboard and tracks piece positions and captures.

Source code in src/model/game/board.py
class Board:
    """Represents a chessboard and tracks piece positions and captures."""

    def __init__(self, dimensions: Tuple[int, int] = (8, 8), setup_pieces: bool = True):
        """Initialize a chessboard.

        Args:
            dimensions: Board dimensions as (rows, cols) tuple (default: (8, 8)).
            setup_pieces: Whether to initialize pieces in standard positions (default: True).
        """
        self.dimensions = dimensions
        self.rows, self.cols = dimensions
        self.board: List[List[Optional[Piece]]] = [
            [None for _ in range(self.cols)] for _ in range(self.rows)
        ]
        self.captured_white: List[Piece] = []
        self.captured_black: List[Piece] = []

        if setup_pieces and dimensions == (8, 8):
            self.setup_default_board()

    def is_within_bounds(self, row: int, col: int) -> bool:
        """Check if coordinates lie within the board boundaries.

        Args:
            row: 0-indexed board row.
            col: 0-indexed board column.

        Returns:
            True if (row, col) is within bounds, False otherwise.
        """
        return 0 <= row < self.rows and 0 <= col < self.cols

    def get_piece_at(self, position: Tuple[int, int]) -> Optional[Piece]:
        """Retrieve the piece located at the specified board position.

        Args:
            position: Tuple of (row, col) coordinates.

        Returns:
            Piece instance at the position, or None if empty or out of bounds.
        """
        row, col = position
        if not self.is_within_bounds(row, col):
            return None
        return self.board[row][col]

    def set_piece_at(self, position: Tuple[int, int], piece: Optional[Piece]) -> None:
        """Place or remove a piece at a specified position.

        Args:
            position: Tuple of (row, col) coordinates.
            piece: Piece instance to place, or None to clear square.
        """
        row, col = position
        if self.is_within_bounds(row, col):
            self.board[row][col] = piece

    def move_piece(self, start_pos: Tuple[int, int], end_pos: Tuple[int, int]) -> bool:
        """Move a piece from start_pos to end_pos, tracking captured pieces.

        Args:
            start_pos: Source (row, col) coordinates.
            end_pos: Target (row, col) coordinates.

        Returns:
            True if the piece was successfully moved, False if invalid or empty start.
        """
        piece = self.get_piece_at(start_pos)
        if piece is None:
            return False
        if not self.is_within_bounds(end_pos[0], end_pos[1]):
            return False

        target = self.get_piece_at(end_pos)
        if target is not None:
            if target.getColor() == 1 or target.getColor() == "white":
                self.captured_white.append(target)
            else:
                self.captured_black.append(target)

        self.set_piece_at(end_pos, piece)
        self.set_piece_at(start_pos, None)
        if hasattr(piece, "setMoved"):
            piece.setMoved(True)
        return True

    def replace_piece(self, position: Tuple[int, int], new_piece: Piece) -> None:
        """Replace a piece at the given position (e.g. during pawn promotion).

        Args:
            position: Target (row, col) coordinate.
            new_piece: Replacement Piece instance.
        """
        self.set_piece_at(position, new_piece)

    def setup_default_board(self) -> None:
        """Initialize the board with standard 8x8 chess starting positions."""
        self.board = [[None for _ in range(self.cols)] for _ in range(self.rows)]
        self.captured_white.clear()
        self.captured_black.clear()

        # White pieces (row 0 and 1, color = 1)
        self.board[0][0] = Rook(1)
        self.board[0][1] = Horse(1)
        self.board[0][2] = Bishop(1)
        self.board[0][3] = Queen(1)
        self.board[0][4] = King(1)
        self.board[0][5] = Bishop(1)
        self.board[0][6] = Horse(1)
        self.board[0][7] = Rook(1)
        for c in range(8):
            self.board[1][c] = Pawn(1)

        # Black pieces (row 7 and 6, color = -1)
        self.board[7][0] = Rook(-1)
        self.board[7][1] = Horse(-1)
        self.board[7][2] = Bishop(-1)
        self.board[7][3] = Queen(-1)
        self.board[7][4] = King(-1)
        self.board[7][5] = Bishop(-1)
        self.board[7][6] = Horse(-1)
        self.board[7][7] = Rook(-1)
        for c in range(8):
            self.board[6][c] = Pawn(-1)
__init__(dimensions=(8, 8), setup_pieces=True)

Initialize a chessboard.

Parameters:

Name Type Description Default
dimensions Tuple[int, int]

Board dimensions as (rows, cols) tuple (default: (8, 8)).

(8, 8)
setup_pieces bool

Whether to initialize pieces in standard positions (default: True).

True
Source code in src/model/game/board.py
def __init__(self, dimensions: Tuple[int, int] = (8, 8), setup_pieces: bool = True):
    """Initialize a chessboard.

    Args:
        dimensions: Board dimensions as (rows, cols) tuple (default: (8, 8)).
        setup_pieces: Whether to initialize pieces in standard positions (default: True).
    """
    self.dimensions = dimensions
    self.rows, self.cols = dimensions
    self.board: List[List[Optional[Piece]]] = [
        [None for _ in range(self.cols)] for _ in range(self.rows)
    ]
    self.captured_white: List[Piece] = []
    self.captured_black: List[Piece] = []

    if setup_pieces and dimensions == (8, 8):
        self.setup_default_board()
get_piece_at(position)

Retrieve the piece located at the specified board position.

Parameters:

Name Type Description Default
position Tuple[int, int]

Tuple of (row, col) coordinates.

required

Returns:

Type Description
Optional[Piece]

Piece instance at the position, or None if empty or out of bounds.

Source code in src/model/game/board.py
def get_piece_at(self, position: Tuple[int, int]) -> Optional[Piece]:
    """Retrieve the piece located at the specified board position.

    Args:
        position: Tuple of (row, col) coordinates.

    Returns:
        Piece instance at the position, or None if empty or out of bounds.
    """
    row, col = position
    if not self.is_within_bounds(row, col):
        return None
    return self.board[row][col]
is_within_bounds(row, col)

Check if coordinates lie within the board boundaries.

Parameters:

Name Type Description Default
row int

0-indexed board row.

required
col int

0-indexed board column.

required

Returns:

Type Description
bool

True if (row, col) is within bounds, False otherwise.

Source code in src/model/game/board.py
def is_within_bounds(self, row: int, col: int) -> bool:
    """Check if coordinates lie within the board boundaries.

    Args:
        row: 0-indexed board row.
        col: 0-indexed board column.

    Returns:
        True if (row, col) is within bounds, False otherwise.
    """
    return 0 <= row < self.rows and 0 <= col < self.cols
move_piece(start_pos, end_pos)

Move a piece from start_pos to end_pos, tracking captured pieces.

Parameters:

Name Type Description Default
start_pos Tuple[int, int]

Source (row, col) coordinates.

required
end_pos Tuple[int, int]

Target (row, col) coordinates.

required

Returns:

Type Description
bool

True if the piece was successfully moved, False if invalid or empty start.

Source code in src/model/game/board.py
def move_piece(self, start_pos: Tuple[int, int], end_pos: Tuple[int, int]) -> bool:
    """Move a piece from start_pos to end_pos, tracking captured pieces.

    Args:
        start_pos: Source (row, col) coordinates.
        end_pos: Target (row, col) coordinates.

    Returns:
        True if the piece was successfully moved, False if invalid or empty start.
    """
    piece = self.get_piece_at(start_pos)
    if piece is None:
        return False
    if not self.is_within_bounds(end_pos[0], end_pos[1]):
        return False

    target = self.get_piece_at(end_pos)
    if target is not None:
        if target.getColor() == 1 or target.getColor() == "white":
            self.captured_white.append(target)
        else:
            self.captured_black.append(target)

    self.set_piece_at(end_pos, piece)
    self.set_piece_at(start_pos, None)
    if hasattr(piece, "setMoved"):
        piece.setMoved(True)
    return True
replace_piece(position, new_piece)

Replace a piece at the given position (e.g. during pawn promotion).

Parameters:

Name Type Description Default
position Tuple[int, int]

Target (row, col) coordinate.

required
new_piece Piece

Replacement Piece instance.

required
Source code in src/model/game/board.py
def replace_piece(self, position: Tuple[int, int], new_piece: Piece) -> None:
    """Replace a piece at the given position (e.g. during pawn promotion).

    Args:
        position: Target (row, col) coordinate.
        new_piece: Replacement Piece instance.
    """
    self.set_piece_at(position, new_piece)
set_piece_at(position, piece)

Place or remove a piece at a specified position.

Parameters:

Name Type Description Default
position Tuple[int, int]

Tuple of (row, col) coordinates.

required
piece Optional[Piece]

Piece instance to place, or None to clear square.

required
Source code in src/model/game/board.py
def set_piece_at(self, position: Tuple[int, int], piece: Optional[Piece]) -> None:
    """Place or remove a piece at a specified position.

    Args:
        position: Tuple of (row, col) coordinates.
        piece: Piece instance to place, or None to clear square.
    """
    row, col = position
    if self.is_within_bounds(row, col):
        self.board[row][col] = piece
setup_default_board()

Initialize the board with standard 8x8 chess starting positions.

Source code in src/model/game/board.py
def setup_default_board(self) -> None:
    """Initialize the board with standard 8x8 chess starting positions."""
    self.board = [[None for _ in range(self.cols)] for _ in range(self.rows)]
    self.captured_white.clear()
    self.captured_black.clear()

    # White pieces (row 0 and 1, color = 1)
    self.board[0][0] = Rook(1)
    self.board[0][1] = Horse(1)
    self.board[0][2] = Bishop(1)
    self.board[0][3] = Queen(1)
    self.board[0][4] = King(1)
    self.board[0][5] = Bishop(1)
    self.board[0][6] = Horse(1)
    self.board[0][7] = Rook(1)
    for c in range(8):
        self.board[1][c] = Pawn(1)

    # Black pieces (row 7 and 6, color = -1)
    self.board[7][0] = Rook(-1)
    self.board[7][1] = Horse(-1)
    self.board[7][2] = Bishop(-1)
    self.board[7][3] = Queen(-1)
    self.board[7][4] = King(-1)
    self.board[7][5] = Bishop(-1)
    self.board[7][6] = Horse(-1)
    self.board[7][7] = Rook(-1)
    for c in range(8):
        self.board[6][c] = Pawn(-1)
ChessWithQuests