I’m starting to learn python and I’ve decided to develop a small artificial intelligence game that you can find on this repository.
The game consist in a main program that handles a chess game and expect as input other two programs that plays. This is done by opening subprocesses as the goal is to have the possibility to integrate AI made in any language.
Project setup
First of all, since I’m a code maniac and strongly-typed guy, I’ve downloaded pyCharm and imported some packages to help me standardize the codebase. In the requirements.txt file you can see the package list:
- pylint, to follow the base guidelines (with some exceptions)
- pytest, to execute the unit tests
- mypy, to enforce some typing in the code
- pygame, to show the chess board
No additional chess or AI libraries are imported since this is an exercise to learn python and it doesn’t make sense to use something already done by someone else
Github Actions
Github actions that enforce those rules are setup in the workflow folder. In addition, it also run codeQL, flake8 and a dependency review job
Using the app
game.py contains the main application code (and its code makes me a little sick, there’s a lot of room for improvements), and it’s of course called from the main function.
The main function expect two commands to run the AI as args. For example, to use the stupid_ai.py that you can find in the AI folder, run the application with:
python3 main.py "python3 AI/stupid_ai.py" "python3 AI/stupid_ai.py"
A chess game will pop up, where the two given AIs play for 10 matches. You can see both in the UI and in the console the output:
Since they are playing semi-randomly, the majority of the games end with a draw, but usually on each 10 games batch you can see a checkmate or two.
An explanation of why the match ended is explicited at the end of each game. The explanation can be Checkmate, Stalemate, 50 Rule or Repetition. The game may also end when it reaches the 1000th turn, but it should be impossible 😛
You can see an example of games played on youtube
Logs
During play, a folder named logs/{games_id} will be created in the root of the project. This folder contains a log file (with the same output as the console) and 10 sub-folders (one for each game played), containing 3 files:
- an.txt: the log of the game using the Algebraic Notation. You can take this and paste it on chess.com to analyze the game (and report a bug if something is wrong)
Example:
- board.txt: the log of the game composed by the print of all the boards state. This is a colored log, so you’ll want to cat the file in a terminal
- message.txt: this is the file used for communication between the main game and the AI
Communication
When the game needs to request a move from a player, it creates a txt file containing the board state, like this:
The AI program will receive the path of this file as the first and unique command line arg. It must open the file, parse it and respond using a console log, printing the code of the move to be executed and then interrupt itself with an exit call.
If the response is not included in the move list sent by the program, that player will skip the turn and an “Invalid Move” move will be printed in the logs.
If the AI doesn’t answer in 5 seconds, that player will skip the turn and a “TimeoutExpired” move will be printed in the logs.
If the AI doesn’t exit with error code 0, that player will skip the turn and a “CalledProcessError” move will be printed in the logs.
How to contribute to chess-ai-game?
If you wish to refactor the code, fix a bug or add a feature, you are very welcome.
What if I want to add an AI to the project?
Aside from the stupid test one, proper AIs don’t belong to this repo, so please create a new repository, let me know and I’ll be happy to link it here!
I will also create an AI to be used there, maybe some reinforced learning one since I’m also studying tensorflow. I’ll let you know 🙂
If you liked this post, don’t forget to like it, comment and star the repository!