-
-
Notifications
You must be signed in to change notification settings - Fork 3
BoardRepresentation.md
This page explains how the MicroChess project represents the chess board, focusing on the board_t
class defined in board.h
and board.cpp
. The board representation is optimized for Arduino’s limited memory (less than 2K RAM), using bit fields and efficient data structures to store piece positions and states.
The chess board is an 8x8 grid (64 squares), with each square potentially holding a piece (pawn, knight, bishop, rook, queen, king) of either color (white or black). MicroChess uses the board_t
class to manage the board, ensuring memory efficiency while supporting all chess operations, such as piece placement, movement, and state queries.
The board_t
class, defined in board.h
and implemented in board.cpp
, is the core of board representation. Key features include:
- Memory Optimization: Uses bit fields to store piece types and colors, minimizing RAM usage.
- Square Access: Provides methods to get and set piece information for any square.
- State Management: Tracks board state, including piece counts and special move conditions (e.g., castling availability).
-
Piece Representation:
- Each square is represented by a
spot_t
structure (defined inMicroChess.h
), which uses bit fields to store:- Piece Type: Encoded as a few bits (e.g., pawn=1, knight=2, king=6).
- Color: 1 bit to indicate white (0) or black (1).
- Occupied Flag: Indicates if the square is empty or holds a piece.
- This structure ensures each square’s data fits in a minimal number of bytes.
- Each square is represented by a
-
Board Storage:
- The board is stored as an array of 64
spot_t
elements, corresponding to squares a1 to h8. - The layout follows standard chess notation (e.g., a1=0, h1=7, a2=8, h8=63).
- The board is stored as an array of 64
The board_t
class provides methods for board manipulation and queries, including:
-
get(index)
: Returns thespot_t
data for a given square (0-63). -
set(index, spot)
: Sets the piece type and color for a square. -
isOccupied(index)
: Checks if a square is occupied by a piece. -
clear()
: Resets the board to an empty state. -
setup()
: Initializes the board to the standard chess starting position (e.g., white rook on a1, black king on e8). -
Move Application: Updates the board state when a move is executed (called via
game_t
).
To fit within Arduino’s 2K RAM limit:
-
Bit Fields: The
spot_t
structure uses bit fields to pack piece type, color, and occupancy into a few bits per square, reducing memory usage compared to storing full integers. -
Minimal Overhead: The
board_t
class avoids unnecessary data, focusing only on essential board state. -
Efficient Access: Methods like
get()
andset()
are optimized for speed and low memory footprint.
The board_t
class interacts closely with other parts of MicroChess:
-
Move Generation (
move.h/cpp
): Provides board state to generate legal moves, checking piece positions and move validity. -
Game Logic (
game.h/cpp
): Updates the board when moves are executed and checks for conditions like check or checkmate. -
Display (
led_strip.cpp
,MicroChess.ino
): Mapsboard_t
data to the LED strip or Serial Monitor for visualization. -
Evaluation (
chessutil.cpp
,pieces.cpp
): Uses board state to compute position scores based on piece placement.
The starting position is set by board_t::setup()
:
- White pieces: Pawns on a2-h2, rooks on a1/h1, knights on b1/g1, bishops on c1/f1, queen on d1, king on e1.
- Black pieces: Pawns on a7-h7, rooks on a8/h8, knights on b8/g8, bishops on c8/f8, queen on d8, king on e8.
- The Serial Monitor prints this as:
MicroChess | GitHub Repository | License: MIT | Contributing
© 2025 Trent M. Wyatt. All rights reserved.