Skip to content

Tuxedolphin/python_chess_engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Chess Engine

This is a simple python chess engine with all the essentials - moving generation, move searching, nega-max algorithm - and some more advanced features such as piece table evaluation function and move ordering. A UI is also included which includes move highlighting and a UI for choosing which colour(s) the AI plays.

Getting Started

Prerequisites

As this project is entirely built in python3, please make sure that you have Python 3 installed on your device. You can download it from the following link if you have not done so.

Installation

To install the engine, download the file directly from Git Hub or create a fork of the repository using the forked repository.

After installing the folder, open the terminal and change the directory to this file. Then, run the following to install all the extensions used:

pip install -r requirements.txt

Thereafter, simply run the file chess_game.py, either from the terminal or using a code editor. A main menu should pop up as shown below:

chess_engine_menu

Navigating The UI

By default, you will be playing the white pieces and the engine will be playing black. To change this, simply click on options, which will bring up the options menu:

chess_engine_options

Simply click on the options that you desire. The options selected is automatically saved and you can simply exit back to the main menu.

Important

This program supports human vs human and ai vs ai. However, do note that if you choose ai vs ai, it is not possible to stop the program other than terminating it or waiting till the game is over.

After clicking on "play", a similar screen to this will greet you (of course, this screenshot has been played out to demonstrate the UI).

chess_engine_play

Note the following:

  1. The piece marked yellow is the last move made.
  2. The piece marked blue is the current piece selected.
  3. The squares marked red are the squares that the selected piece can move to.
  4. The pgn of the game is avaliable on the right hand side.
  5. To undo a move, press the left arrow key. To restart the game, press the PgDn key.

When the game is over, a screen similar to this will show:

chess_engine_over

Important

Do not undo a move if it was a move made by the AI. As this engine is largely deterministic in nature as the evaluation function usually leads to only 1 best move, undoing a move will only make the engine think again and play the same move. Spamming the button will not work either but will only cause a backlog of undo moves, creating more lag.

Note

Note that the number of positions searched by the AI as well as the evaluation of the move played is printed in the terminal. Do note that the evaluation is always positive for the AI, i.e. no matter the colour, the higher the number, the better the AI thinks the move is. If the AI sees mate, the evaluation will either be 100000 (if it is mating) or -100000 (if it is getting mated). To translate the evaluation to our what we commonly use, simply divide the number by 100.

Acknowledgements

Special thanks to the kind people who has put up the following resources, without which I wouldn't have been able to complete this:

  1. The Chess Programming Wiki for all of the amazing information about creating a chess engine.
  2. The PESTO evaluation function which I edited slightly to create the evaluation function used.
  3. The Chess Engine in Python Series by Eddie Sharick which I took reference from whenever I am stuck or was looking for inspiration.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages