AI+CHESS is an open-source, easy-to-set-up educational platform with the vision of implementing and understanding AI algorithms (Brute-force, Supervised, Unsupervised and Reinforcement learning) in the context of the beautiful game of chess. The chess logic and engine is written from scratch in python and the GUI is written with the pygame python library.
- GUI (with piece-moving/capture sounds) display resolution of 600 x600
- Voice commentary
- Square highlighting
- Game mode selection menus
- AI Vs AI mode
- Game playback after checkmate or stale (Human vs Human mode currently)
- Minimax algorithm w/ Alpha-beta pruning.
- Move ordering based off heuristics (captures, promotions, e.t.c)
- Efficient board evaluation function
- Human Vs Human
- AI Vs AI
- Online mode
- Stronger ML-based AI
- Human vs AI mode
Composed of five files:
The main_menu.py
Selects White or Black player as well as offline or online modes (online mode in the works)
The menu.py
Selects AI mode or human mode
The main.py
Chess GUI using pygame
The engine.py
Creates the chess objects (board, pieces, moves e.t.c) and their game logics.
The ai.py
Simplistic mini-max AI bot. below is a detail description of each file
Implements the first game menu with player and mode selections.
Implements the second menu from the main_menu with game mode selection and returning to main-menu T
This is the GUI displaying all aspects of the chess game and handling user inputs. it contains the main()
function which displays the chess game using pygame with the help of these supporting functions:
display_game_state
:display all graphicsdisplay_board
: display chess squares on boarddisplay_pieces
: display chess pieces on board from current game statedisplay_ranks_files
:display ranks (numbers) and files (letters) around boardplay_sound
: plays 'move' and 'capture' soundsanimate
: creates moving animations for chess pieces:get_chess_notation
: makes the voice commentary of moves, checkmate and stalemate
This implements the rules of the game and stores the state of the chess board, including its pieces and moves. It has two Classes Game_state
and Move
. Each Class has a specific function:
self.board
: 8 X 8 dimensional array (Matrix of 8 rows and 8 columns ) i.e a list of lists. Each element of the Matrix is a string of two characters representing the chess pieces in the order "type" + "colour".. light pawn = “pl” dark pawn = “pd” and empty square = " " double empty space.get_pawn_moves
,get_rook_moves
,get_knight_moves
,get_queen_moves
,get_king_moves
andget_bishop_moves
: this functions Calculates all possible moves for a given color (light or dark) and appends them to a list. This includes all types of chess moves, capture, castling and enpassantmake_move
: moves pieces on the board from one square to anotherundo_move
: this undo moves made in the by using the move_log that saves all moves doneget_all_possible_moves
: this gives naive possible moves of pieces on the board without taking checks into accountget_valid_moves
: gives the valid piece moves on the board while considering potential checks
A Move class abstracting all parameters needed for moving chess pieces on the board
self.start_row
: row location of piece to be movedself.start_col
: column location of piece to be movedself.end_row
: intended row destination of piece to be movedself.end_col
: intended column destination of piece to be movedself.piece_moved
: actual piece movedself.piece_captured
: opponent piece if any on the destination square
Included in the ai.py
is the Minimax functions, which utilizes the MiniMax algorithm to evaluate board states. The MiniMax algorithm provided comes with alpha-beta pruning, move ordering. Some of the functions are:
Evaluation
: this function evaluates the board at a given game state. It sums up the pieces on the board using piece values and also adds piece-square tables, which alter the value of a piece depending on which square it sits on.Minimax
: Minimax is a search algorithm that finds the next optimal move by minimizing the potential loss in a worst case scenario. This algorithm was adapted from Sebastian Lague’s Algorithms Explained – minimax and alpha-beta pruning. It uses the evaluation function to determine the best possible move to win the game. The Minimax was made better using the alpha beta pruning, This significantly reduces the number of moves required to be generated hence increasing search speed without affecting the outcomeMove_ordering
: this function helps the ai to order moves base on importance (capture, promotion, defending the king) this helps to further assist in choosing the best move without making unnecessary sacrifices.light_pieces and dark_pieces
: These are dictionaries containing all the pieces at a particular game state which makes for faster static evaluation of the board. It is updated on the fly as moves and captures are being madeupdate_pieces_dictionaries
: This updates light_pieces and dark_pieces during the hidden simulation in the minimax algorithm. It creates a copy of the dictionary to be used by the minimax during tree search for best move.ai_light_move
andai_dark_move
: this serves as a plug for the ai to make moves generated using the minimax algorithm on the board. It also updates the light_pieces and dark_pieces dictionaryai_move
: determines the turn for both team's AI also saves a running memory of the board state with moves and capturesai_reset
: resets the light pieces and dark pieces dictionaries when AI mode is activated/deactivated (just in case moves were made outside AI mode)
-
Clone/Download and extract repo in a folder on your system
-
Create and activate an environment
- Linux or Mac:
python3 -m venv chess source chess/bin/activate
- Windows:
conda create --name chess python=3 activate chess
-
Install dependecies from within chess environment
pip install -r requirements.txt
-
Start game by running the following command:
- Linux, Mac, windows:
python main_menu.py
- Double click mouse-left to select options in the menu window
- Press “R” on keyboard to reset game (only human vs human mode)
- Press “U” on keyboard to undo move made (only human vs human mode)
- Mouse left click on piece and square to select piece and make move respectively (only human vs human mode)
- Press "M" to mute commentary during game play
- Press "P" after end of game (
stalemate
orcheckmate
) to playback the entire game move by move. press "N" to toggle next move and "B" for previous move