A Python-based chess engine that uses the Minimax algorithm with Alpha-Beta pruning to calculate the best moves. The engine evaluates board positions based on piece values, positional advantages, and checkmate scenarios. This project includes move generation, check/checkmate detection, and an evaluation function.
- β Minimax algorithm with Alpha-Beta pruning for efficient search.
- β Move generation for all piece types.
- β Check and checkmate detection.
- β Positional evaluation considering material and control of center squares.
- β Pathfinding to ensure legal moves.
- β Basic AI that can play as White or Black.
- Position Class
- Manages the current state of the board.
- Tracks pieces and their locations.
- Provides helper functions to retrieve piece positions and check for occupied squares.
- Move Class
- Represents a move from one square to another.
- Stores the piece involved and the start/end locations.
- ChessAI Class
- Uses the Minimax algorithm with Alpha-Beta pruning.
- Evaluates the board based on:
- Material value of pieces.
- Positioning advantage (central squares).
- Check and checkmate scenarios.
- Generates legal moves for all piece types.
- Ensures the king is not left in check after a move.
git clone https://github.com/your-username/ChessEngine.git
cd chess_engine
python chess_engine.pypos = Position()
# White pieces
pos.add_piece(1, 'K', 'white', 'e1')
pos.add_piece(2, 'Q', 'white', 'f6')
# Black pieces
pos.add_piece(3, 'K', 'black', 'h8')
pos.add_piece(4, 'p', 'black', 'g7')
pos.add_piece(5, 'p', 'black', 'h7')
ai = ChessAI()
# Find the best move for White
best_move = ai.find_best_move(pos, 'white')
if best_move:
piece_type = pos.pieces[best_move.piece_id][0]
print(f"Best move: {piece_type} {best_move.from_square} to {best_move.to_square}")Output:
Best move: Q f6 to h6The engine evaluates the position based on:
- Material advantage.
- Control of the center.
- Mobility (number of legal moves).
- Checkmate and stalemate detection.
- The engine searches up to a depth of 3.
- Alpha-Beta pruning reduces the number of nodes evaluated.
- The engine calculates the best move based on the highest evaluation score for White and lowest for Black.
- Add castling support.
- Implement en passant.
- Improve evaluation for pawn structure and king safety.
- Add GUI for gameplay.