Chess engine in C that validates moves, detects checkmate/stalemate, and recommends optimal moves using both forced mate detection and material + positional evaluation engine.
- All Piece Moves: Pawn, knight, bishop, rook, queen, and king movement rules
- Special Moves:
- Castling
- En passant
- Pawn promotion to any piece
- Game State Detection:
- Check and checkmate detection
- Stalemate detection
- Legal move generation with self-check prevention
-
Forced Mate Engine (Priority)
- Searches for forced checkmate sequences up to configurable depth (default: 3 moves for each player)
- Recommends the first move of a forced mate sequence when found
-
Material + Positional Evaluation Engine (Fallback)
- Material evaluation with standard piece values
- Advanced positional evaluation using piece-square tables
- Considers:
- Piece activity and central control
- Pawn structure advantages
- King safety (penalizes exposed kings)
- Piece development
- Recommends the move with the best static evaluation
- Parses standard algebraic notation (SAN)
- Validates all moves according to chess rules
- Handles move disambiguation (e.g., Nbd2 vs Nfd2)
- Error reporting for illegal moves
The program reads all chess moves from standard input and sets the board state. Moves are entered in standard algebraic notation:
During Game:
- Validates each move
- Reports illegal moves with error messages
Game Completion: The engine outputs one of:
white wins by checkmateblack wins by checkmatedraw by stalemategame incomplete
Move Recommendations (bonus): For incomplete games, the engine suggests the best move:
suggest WHITE knight from g1 to f3
suggest BLACK pawn from e7 to e5
- 64-element array representing the 8×8 chess board
- Tracks piece positions, colors, and types
- Maintains castling rights and en passant state
- Manages turn order
- Generates all legal moves for each piece type
- Validates moves don't leave king in check
- Handles special move logic (castling, en passant, promotion)
- Creates move structures from board positions
Material Evaluation:
Pawn: 10 points
Knight: 20 points
Bishop: 30 points
Rook: 50 points
Queen: 90 points
Positional Evaluation:
- Uses piece-square tables for all pieces
- Separate tables for white and black pieces
- Examples:
- Pawns: Rewards center pawns and passed pawns
- Knights: Prefers central squares, penalizes rim squares
- Bishops: Rewards long diagonals and development
- Rooks: Prefers 7th rank and open files
- Queens: Rewards central activity
- King: Prefers safety in opening (castled position)
- Recursive search for checkmate sequences
- Configurable depth (default: MAX_DEPTH = 3)
- Validates that opponent has no escape
- Returns the first move of the mating sequence
1. Generate all legal moves for current player
2. For each move:
a. Apply move to board copy
b. Recursively search opponent's responses
c. If all opponent moves lead to mate, mate found
3. Return first move in mating sequence
Score = Material Balance + Positional Value
= (White Material - Black Material)
+ (White Position - Black Position)
Positive score = White advantage
Negative score = Black advantage
1. Check for forced mate (if found, recommend)
2. Run evaluation engine (recommend best static move)
3. If neither succeeds, report no recommendation
MAX_DEPTH- Forced mate search depth (default: 3 moves)
Set DEBUG to true for debugging output:
- Board display after moves
- Detailed move information
- Search progress
Modify the positional evaluation arrays to adjust playing style:
White_pawnScore[]/Black_pawnScore[]White_knightScore[]/Black_knightScore[]White_bishopScore[]/Black_bishopScore[]White_rookScore[]/Black_rookScore[]White_queenScore[]/Black_queenScore[]White_kingScore[]/Black_kingScore[]
- Squares numbered 0-63
- Conversion functions:
from_cords(x, y),from_id(id, &x, &y), more found intools.c/h - Algebraic notation:
square_string(id)
- Generate pseudo-legal moves (piece movement patterns only)
- Test each move by applying it to a board copy
- Check if king is in check after the move
- Only include moves that don't leave own king in check
Checks multiple conditions:
- King and rook haven't moved
- Squares between king and rook are empty
- King is not in check
- King doesn't pass through or land on attacked squares
Alexander Szura
Jeevan Sanchez
Edward Sicoe
B.Sc. (Eng) Candidates, Smith Engineering, Queen's University