Skip to content

jeevan9s/APSC-143-Chess-Engine

 
 

Repository files navigation

APSC-143 Chess Engine

Chess engine in C that validates moves, detects checkmate/stalemate, and recommends optimal moves using both forced mate detection and material + positional evaluation engine.

Features

Complete Chess Rules

  • 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

Engine Move Recommendation System

  1. 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
  2. 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

Move Validation & Parsing

  • 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

Input Format

The program reads all chess moves from standard input and sets the board state. Moves are entered in standard algebraic notation:

Output Format

During Game:

  • Validates each move
  • Reports illegal moves with error messages

Game Completion: The engine outputs one of:

  • white wins by checkmate
  • black wins by checkmate
  • draw by stalemate
  • game 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

Architecture

Core Components

Board Representation (board.c/h)

  • 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

Move Generation (moves.c/h)

  • 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

Evaluation Engine (engine.c/h)

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)

Forced Mate Detection (board.c)

  • 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

Algorithm Details

Forced Mate Search

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

Position Evaluation

Score = Material Balance + Positional Value
      = (White Material - Black Material) 
        + (White Position - Black Position)

Positive score = White advantage
Negative score = Black advantage

Move Recommendation Priority

1. Check for forced mate (if found, recommend)
2. Run evaluation engine (recommend best static move)
3. If neither succeeds, report no recommendation

Configuration

Constants in board.h

  • MAX_DEPTH - Forced mate search depth (default: 3 moves)

Debug Mode (tools.h)

Set DEBUG to true for debugging output:

  • Board display after moves
  • Detailed move information
  • Search progress

Piece-Square Tables (engine.c)

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[]

Technical Implementation Notes

Coordinate System

  • Squares numbered 0-63
  • Conversion functions: from_cords(x, y), from_id(id, &x, &y), more found in tools.c/h
  • Algebraic notation: square_string(id)

Move Generation Details

  1. Generate pseudo-legal moves (piece movement patterns only)
  2. Test each move by applying it to a board copy
  3. Check if king is in check after the move
  4. Only include moves that don't leave own king in check

Castling Validation

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

Authors

Alexander Szura
Jeevan Sanchez
Edward Sicoe

B.Sc. (Eng) Candidates, Smith Engineering, Queen's University

About

chess engine in C | APSC 143 course project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C 99.4%
  • CMake 0.6%