In order to run our program, the following libraries must be installed:
pip install python-chess
pip install pygameThe methods which make up our Chess AI are located in the /ChessHelpers/
directory:
We have defined a "move generator" simply as any function which accepts a chess.Board
and returns a chess.Move. The goal of this project then is to use AI to create an
intelligent move generator. The file ChessEngineHelper.py contains our move generation
methods:
random_moveA function which generates a random move from the list of available legal moves.greedy_next_best_moveuses a heuristic to generate the current best move without looking ahead.mobility_next_best_movefinds the next best move by maximizing number of legal moves available to itself and minimizing the number of legal moves available to the opponent.mobility_advanced_next_best_movefinds the next best move by maximizing the number of pieces which are being attacked and defended.mini_max_easyuses a heuristic to generate the current best move by looking ahead to the opponents next move.mini_max_moveis our finished Minimax algorithm which can search to any specified depth and which has been modified to utilize Alpha Beta pruning and intelligent move ordering.
All of our move generation methods (except random_move) require the use of a heuristic
to score leaf nodes or board positions. The file ChessHeuristics.py contains our board
evaluation heuristics:
heuristic_1generates a score for a position based on the value of the pieces on the board.heuristic_2generates a score for a position based on both the value of the pieces on the board and on the control of center squares and center diagonals.heuristic_3generates a score for a position based on the value of the pieces, the control of the center and diagonals, and on the mobility of the board and the number of pieces attacked/defended.
Each of these three heuristics is built from a number of valuable pieces: score_material,
control_diagonals, control_center, mobility, mobility_advanced, etc.
The /interface/ folder contains a very basic chess UI which uses
Python Chess and
Pygame to implement a working chess board.
To play a game, you just need to call the play_chess() function. It accepts a chess board and
two optional arguments, white and black with which you can pass any move generation function.
Run either example_xxx.py to try this out.
board = chess.Board()
# Case 1. No additional arguments. The user will play both sides of the board.
play_chess(board)
# Case 2. The user will play white. The computer will play black.
play_chess(board, black=move_generation_function)
# Case 3. The user will play black. The computer will play white.
play_chess(board, white=move_generation_function)
# Case 4. The computer will play both sides.
play_chess(board, white=move_generation_function_a, black=move_generation_function_b)I learned enough about pygame to make a basic chess interface which can interact with our chess library.
You can run example_gui.py to see the graphical interface in action.
The graphical interface is useful for human play, but the terminal interface is much more convenient
for self-play, when the AI wants to play against itself, maybe many times in rapid succession.
You can run example_tui.py to see the terminal interface in action as well.

