Skip to content

๐Ÿš€ Advanced Checkers AI โ€“ Python-based engine with Minimax + Alpha-Beta Pruning, heuristic evaluation, multi-jump support, and variable depth search.

License

Notifications You must be signed in to change notification settings

MilanSazdov/checkers-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

17da549 ยท Feb 26, 2025

History

17 Commits
Feb 26, 2025
Feb 26, 2025
Feb 26, 2025
Feb 26, 2025
Feb 26, 2025

Repository files navigation

๐Ÿ† Checkers AI - Intelligent Game Player

๐Ÿ” Checkers AI is a Python-based program that plays the game of Checkers (Draughts) as one of the players. The game is played on an 8ร—8 board with 12 pieces per player, following the standard checkers rules. The goal is to eliminate all opponent's pieces or block their possible moves.

๐Ÿค– This project implements AI-based decision-making, using:

  • Heuristic Evaluation โ€“ Determines the best move based on piece positioning and game state.
  • Variable Depth Search Engine โ€“ Adjusts search depth dynamically for better performance.
  • Minimax Algorithm โ€“ Computes the best possible move considering all future possibilities.
  • Alpha-Beta Pruning โ€“ Optimizes Minimax by eliminating unnecessary calculations.
  • Hash Map Optimization โ€“ Reduces redundant computations and speeds up move selection.

๐ŸŽฎ Game Modes:

  • Standard Checkers Rules: Pieces move diagonally and can jump over opponent pieces.
  • Two Play Modes: Choose whether capturing opponent pieces is mandatory or optional.
  • King Promotion: When a piece reaches the opponent's back row, it becomes a "king" and can move both forward and backward.
  • Multi-Jump Sequences: If a capture is possible, a piece can continue jumping over multiple opponent pieces in one turn.
  • Console-Based Gameplay: The game is played through the terminal, with a graphical board representation and move selection.

โณ Performance:

  • The AI must decide its move within 5 seconds.
  • For higher scores, the move must be computed in less than 3 seconds.

๐Ÿ“– This project was developed as part of the "Algorithms and Data Structures" course (2023/2024).


๐Ÿ“š Table of Contents


๐Ÿ›  Technologies & Specifications

Python Pygame Platform License Contributions

๐Ÿ“ฆ Dependencies

The project relies on the following libraries and technologies:

Library Purpose
pygame Used for rendering the game board and handling user interactions
queue Utilized for internal AI processing and state management
time Used for AI move timing and performance measurement
json Handles game configuration and move storage
os File system operations and handling saved games

๐Ÿ“ฅ Install Dependencies

Before running the project, install the required dependencies using:

pip install pygame

๐Ÿ”ง Installing and Running the Project

๐Ÿ“ฅ 1๏ธโƒฃ Clone the Repository

First, download the project by cloning the repository:

git clone https://github.com/MilanSazdov/checkers-ai.git
cd checkers-ai

๐Ÿ“ฆ 2๏ธโƒฃ Install Dependencies

Ensure you have all required dependencies installed:

pip install pygame

โ–ถ๏ธ 3๏ธโƒฃ Run the Game

To start the checkers game, simply run the board.py script located inside the checkers folder:

python checkers/board.py

๐Ÿ“– How It Works

The Checkers AI game follows standard checkers rules, where players take turns moving pieces diagonally across the board. The AI calculates its best move using Minimax with Alpha-Beta Pruning and adjusts its search depth dynamically for performance optimization.

๐ŸŽฎ Game Interface

  • The game starts with an 8ร—8 checkers board, where black and red pieces are placed in their initial positions.
  • The AI's thinking depth is displayed in the top-left corner of the screen.
  • Pieces that can be moved are highlighted with a yellow glowing effect.

๐Ÿ–ผ Game Start:
At the beginning of the game, the depth is set to 3, and all available pieces that can be moved are highlighted.

Game Start


๐ŸŽฏ Selecting a Piece

  • When a player selects a piece, it turns green to indicate the selection.
  • The possible move positions are displayed as gray dots on the board.

๐Ÿ–ผ Piece Selection Example:

Piece Selection


๐Ÿ”ฅ Multi-Jump Capture

  • If an opponent's piece can be captured, the game forces a jump.
  • The game supports multi-jump captures, allowing a piece to continue jumping as long as legal moves exist.

๐Ÿ–ผ Multi-Jump in Action:

Multi-Jump Capture


๐Ÿ‘‘ King Promotion

  • When a piece reaches the opponent's back row, it gets promoted to a king.
  • Kings are visually distinct and can move both forward and backward.

๐Ÿ–ผ King Pieces:


๐Ÿค– Game AI

The AI in this checkers game is built using Minimax algorithm with Alpha-Beta pruning for optimizing decision-making. Additionally, the engine supports variable search depth, heuristic evaluation, and a hash map for board state caching to improve performance.


๐Ÿ” Minimax Algorithm

The Minimax algorithm is used for making optimal decisions in a two-player turn-based game. The idea is to build a game tree, where each node represents a possible board state. The AI then evaluates these states recursively and picks the best possible move.

  • The Maximizing player (AI) tries to maximize the score.
  • The Minimizing player (human) tries to minimize the AIโ€™s score.
  • The algorithm explores all possible moves, evaluates them, and chooses the best one.

๐Ÿงฉ Algorithm Workflow:

  1. Generate all possible moves for the AI.
  2. Simulate each move and generate a game tree up to a predefined depth.
  3. Evaluate board positions using a heuristic function.
  4. Use Alpha-Beta pruning to eliminate unnecessary computations.
  5. Return the best move based on the computed scores.

โšก Alpha-Beta Pruning

Since Minimax searches an exponential number of board states, we use Alpha-Beta Pruning to skip unpromising branches of the tree. This improves efficiency by eliminating unnecessary calculations.

  • Alpha (ฮฑ): The best score that the maximizing player can guarantee.
  • Beta (ฮฒ): The best score that the minimizing player can guarantee.
  • If at any node, the Maximizing Player finds a move that is better than Beta, further evaluation stops.
  • If at any node, the Minimizing Player finds a move that is worse than Alpha, further evaluation stops.

๐Ÿš€ This results in a significant speedup of the Minimax algorithm!


๐Ÿ“Š Heuristic Evaluation Function

Since checking all possible moves until the end of the game is computationally expensive, the AI evaluates board positions before reaching the final state. The evaluation function assigns a numerical score to each board state.

๐ŸŽฏ Factors Considered in the Evaluation Function:

โœ”๏ธ Material Advantage: Counts the number of normal pieces and kings.
โœ”๏ธ Mobility: Prioritizes positions where more pieces have possible moves.
โœ”๏ธ Safety: Figures that cannot be captured are favored.
โœ”๏ธ Center Control: Figures in the center of the board are prioritized.
โœ”๏ธ Defense Strategy: Figures in the last two rows are valued higher.
โœ”๏ธ Promotion Potential: More empty squares on the opponentโ€™s promotion row increases the score.
โœ”๏ธ Multiple Captures: Moves leading to multiple jumps are highly favored.

๐Ÿ“Œ Final evaluation formula:

Score = 1.3 ( safe pieces ) + 3.3 ( safe kings ) + 1.15 ( movable pieces ) + 3.15 ( movable kings ) โˆ’ 1.5 ( loner pieces ) โˆ’ 3.5 ( loner kings ) + 1.35 ( defensive pieces ) + 3.35 ( defensive kings ) + 27.5 ( captured pieces ) + 33.5 ( captured kings ) + 1.25 ( center-controlled pieces ) + 3.25 ( center-controlled kings )

๐Ÿ”ข The AI aims to maximize this score for itself and minimize it for the opponent.


๐Ÿ”„ Variable Search Depth

The AI dynamically adjusts the search depth based on the game state:

  • Many pieces on the board โ†’ Lower depth (faster decision-making)
  • Few pieces remaining โ†’ Higher depth (more precise evaluation)
  • Multiple jumps available โ†’ Increases depth for better decision-making
Board State Search Depth
๐ŸŸข More than 30 possible moves 3
๐Ÿ”ต 15-30 possible moves 4
๐Ÿ”ด Less than 8 possible moves 5
๐Ÿ† Multiple capture moves available 5

This ensures that the AI plays efficiently in early game, but becomes highly precise in endgame situations.


๐Ÿ”ฅ Hash Map Optimization

Since some board states repeat multiple times, recalculating Minimax for the same state is wasteful. To speed up the AI, we use a Hash Map that stores previously evaluated board states.

โœ”๏ธ Key Idea: Each board state is converted into a unique string key (e.g., "wwbbbwwwbwwbww").
โœ”๏ธ If a state has already been evaluated, its best move is instantly retrieved from the hash map instead of recalculating Minimax.
โœ”๏ธ The evaluations are stored in evaluations.txt and updated dynamically.

This significantly improves performance, especially in longer games.


๐Ÿ•น๏ธ Example of AI Decision-Making

1๏ธโƒฃ AI generates possible moves โ†’ Filters out bad moves
2๏ธโƒฃ Evaluates board states using the heuristic function
3๏ธโƒฃ Prunes unpromising branches with Alpha-Beta pruning
4๏ธโƒฃ Selects the best move and executes it
5๏ธโƒฃ Stores the move in Hash Map for faster decision-making in future turns

๐Ÿš€ This results in an AI that plays efficiently, adapts to different game states, and continuously improves its decision-making!


๐Ÿ“œ License

This project is licensed under the MIT License.
See the LICENSE file for more details.


๐Ÿ”— Useful Links


๐Ÿ“ฌ Contact

๐Ÿ“ง Email: milansazdov@gmail.com
๐Ÿ™ GitHub: MilanSazdov


About

๐Ÿš€ Advanced Checkers AI โ€“ Python-based engine with Minimax + Alpha-Beta Pruning, heuristic evaluation, multi-jump support, and variable depth search.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages