-
-
Notifications
You must be signed in to change notification settings - Fork 3
MoveGeneration.md
This page explains how MicroChess generates and validates legal chess moves, focusing on the move_t class and related logic defined in move.h and move.cpp. Move generation is a critical component of the chess engine, ensuring all possible moves are considered efficiently within Arduino’s memory constraints (less than 2K RAM).
Move generation involves identifying all legal moves for a given board position, including standard moves (e.g., pawn advances, knight jumps) and special moves (castling, en passant, pawn promotion). MicroChess optimizes this process to minimize memory usage and computational overhead, supporting the minimax algorithm with alpha-beta pruning.
The move_t class, defined in move.h and implemented in move.cpp, represents a single chess move. Key attributes include:
- From Square: The starting square (0-63, corresponding to a1-h8).
- To Square: The destination square (0-63).
- Evaluation Score: A score assigned during move evaluation, used to rank moves.
- Captured Piece: (Optional) Stores the piece type and color captured, if any.
- Special Move Flags: Indicate castling, en passant, or promotion.
- Constructor: Initializes a move with from/to squares and optional flags.
-
isValid(): Checks if the move is legal based on board state and chess rules. -
Getters/Setters: Access or modify move attributes (e.g.,
getFrom(),setScore()).
Move generation is handled by functions in move.cpp, which:
-
Identify Pieces: Scan the board (via
board_t) to find pieces of the current player’s color. -
Generate Moves per Piece:
- Pawns: Forward moves (one or two squares from initial rank), captures (diagonal), en passant, and promotions (on reaching rank 8 or 1).
- Knights: L-shaped jumps (e.g., from e4 to f6, d6, etc.).
- Bishops: Diagonal moves, stopping at obstacles.
- Rooks: Horizontal/vertical moves, stopping at obstacles.
- Queens: Combines bishop and rook moves.
- Kings: One-square moves in any direction, plus castling (if legal).
-
Validate Moves:
- Ensure moves stay within the board (0-63).
- Check for legal captures (opponent’s piece or en passant).
- Verify the king is not left in check after the move.
- Store Moves: Collect valid moves in a list or array for evaluation by the search algorithm.
MicroChess handles all standard chess special moves:
-
Castling:
- Kingside (
O-O) or queenside (O-O-O) is allowed if:- The king and rook have not moved.
- No pieces block the path between them.
- The king is not in check and does not pass through or land on a checked square.
- Represented in
move_twith a special flag and dual-square update (king and rook).
- Kingside (
-
En Passant:
- Occurs when a pawn advances two squares and an opponent’s pawn can capture it as if it moved one square.
- Tracked in
board_tand validated inmove_t.
-
Pawn Promotion:
- When a pawn reaches the 8th (white) or 1st (black) rank, it promotes to a queen, rook, bishop, or knight.
- Specified in move input (e.g., "e7e8q" for queen promotion).
To fit Arduino’s constraints:
- Incremental Generation: Generates moves for one piece at a time, reducing memory usage.
-
Bit Fields: Uses compact
move_tstructures to store move data. - Precomputed Patterns: Leverages fixed movement patterns for knights and kings to speed up generation.
- Early Filtering: Discards illegal moves (e.g., those leaving the king in check) during generation.
Move generation interacts with:
-
Board Representation (
board.h/cpp): Queriesboard_tfor piece positions and occupancy. -
Game Logic (
game.h/cpp): Ensures moves are legal in the current game state (e.g., not in check). -
Evaluation (
chessutil.cpp,pieces.cpp): Assigns scores to generated moves for ranking. -
Search Algorithm (
MicroChess.ino): Feeds generated moves into the minimax algorithm for selection.
For a white pawn on e2 in the starting position:
- Possible moves:
e2e3(one square forward),e2e4(two squares forward). - If an opponent’s piece is on d3 or f3, captures (
e2d3,e2f3) are generated. - These moves are validated and stored as
move_tobjects for evaluation.
Move generation affects the visual output:
- LED Strip: Updated after each move (see MicroChessSmall.gif).
- Serial Console: Shows move notation (e.g., "e2e4") and updated board (see MicroChessConsole2.gif). More details are in the Visuals page.
- Explore Game Logic for how moves are applied and tracked.
- See Evaluation for move scoring.
- Check Code Structure for an overview of all files.
MicroChess | GitHub Repository | License: MIT | Contributing
© 2025 Trent M. Wyatt. All rights reserved.