-
-
Notifications
You must be signed in to change notification settings - Fork 3
GameLogic.md
This page describes how MicroChess manages game state and logic, focusing on the game_t
class defined in game.h
and game.cpp
. The game logic ensures that the chess engine correctly handles turns, applies moves, detects check/checkmate, and determines game outcomes (e.g., win, draw) within Arduino’s memory constraints (less than 2K RAM).
The game logic is responsible for maintaining the state of a chess game, including whose turn it is, the move history, and conditions like check, checkmate, or stalemate. The game_t
class orchestrates these aspects, coordinating with other components like the board and move generation to ensure a valid and complete chess experience.
The game_t
class, defined in game.h
and implemented in game.cpp
, is the central hub for game state management. Key attributes include:
- Current Turn: Tracks whether it’s white’s or black’s turn (e.g., using a boolean or enum).
-
Move History: Stores executed moves (as
move_t
objects) for tracking and undoing moves during search. - Game State: Indicates the game’s status (e.g., ongoing, check, checkmate, stalemate, draw).
- Special Move Tracking: Monitors conditions for castling availability and en passant eligibility.
-
makeMove(move)
: Applies a move to the board, updating piece positions and game state. -
undoMove()
: Reverts the last move, used during minimax search to explore move trees. -
isCheck(color)
: Checks if the specified player’s king is in check. -
isCheckmate()
: Determines if the current player is in checkmate (no legal moves to escape check). -
isStalemate()
: Checks for stalemate (no legal moves, but not in check). -
getState()
: Returns the current game state (e.g., ongoing, draw). -
switchTurn()
: Toggles the turn between white and black.
The game logic follows these steps in the main loop (MicroChess.ino
):
-
Initialize Game:
-
game_t
sets up the initial board state viaboard_t::setup()
. - The game starts with white’s turn.
-
-
Process Moves:
- For the engine’s turn,
game_t
uses move generation (move.h/cpp
) to get legal moves and selects one via the minimax algorithm. - For user input,
game_t
validates moves entered via Serial Monitor (e.g., "e2e4"). -
makeMove()
updates the board and game state.
- For the engine’s turn,
-
Check Game Conditions:
- After each move,
game_t
checks for check, checkmate, stalemate, or draw conditions (e.g., threefold repetition, 50-move rule). - If a terminal state is reached, the game ends, and the result is displayed.
- After each move,
-
Update Outputs:
- The board state is sent to the LED strip (
led_strip.cpp
) and Serial Monitor (MicroChess.ino
).
- The board state is sent to the LED strip (
The game_t
class handles all standard chess conditions:
-
Check: Detects if the current player’s king is under attack, using
isCheck()
. - Checkmate: Confirms no legal moves can escape check, ending the game.
- Stalemate: Identifies when the current player has no legal moves but is not in check, resulting in a draw.
-
Draws:
- Threefold Repetition: Tracks board positions in the move history to detect repeated states.
- 50-Move Rule: Counts moves without captures or pawn moves, declaring a draw after 50 such moves.
- Insufficient Material: Detects cases like king vs. king or king and bishop vs. king, which cannot lead to checkmate.
- Castling and En Passant: Updates tracking variables to ensure these moves are only allowed when legal.
To fit within 2K RAM:
-
Compact State:
game_t
uses minimal variables, storing only essential data (e.g., turn, move history). - Efficient Checks: Check and checkmate detection are optimized to avoid redundant board scans.
-
Move History: Stores only necessary move data (via
move_t
) to support undo operations during search.
The game_t
class interacts with:
-
Board Representation (
board.h/cpp
): Updates theboard_t
instance when moves are applied. -
Move Generation (
move.h/cpp
): Retrieves legal moves for the current position. -
Evaluation (
chessutil.cpp
,pieces.cpp
): Uses board state to evaluate positions during move selection. -
Search Algorithm (
MicroChess.ino
): Explores move trees, callingmakeMove()
andundoMove()
to evaluate outcomes. -
Display (
led_strip.cpp
,MicroChess.ino
): Reflects game state changes on the LED strip and Serial Monitor.
After white plays "e2e4":
-
game_t
validates the move, updates the board viaboard_t
, and checks for check or special conditions. - It switches to black’s turn, generates black’s moves, and selects one (e.g., "e7e5").
- The Serial Monitor shows:
MicroChess | GitHub Repository | License: MIT | Contributing
© 2025 Trent M. Wyatt. All rights reserved.