Coding With Fun
Home Docker Django Node.js Articles FAQ

Python Realizes AI Five Chess (First Edition)

Jun 01, 2021 Article blog

This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column

Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn


Idle to write a more stupid AI five chess.

T_T Of course you're good with it, it's still more witty.

Let's start happily

Related documents

Web disk download link:

Password: ujrh

Develop tools

Python version: 3.6.4

Related modules:

Graphics module.

Environment construction

Just install Python and add it to the environment variable.


The graphics module is already available in the relevant file, which is a py file, either directly in the current path or in the site-packages folder under the python installation folder.

Introduction to the principle

For game class AI such as Chess, the natural idea is to let the computer try all possible current situations to find the best drop point. Here are two questions:

(1) how to try all possible situations again;

(2) How to quantitatively judge the merits of a falling sub-point.

For the first question, in fact, is the so-called game tree search, for the second question, in fact, is the so-called selection evaluation function. T he selection of evaluation functions directly determines the advantages and disadvantages of AI algorithms, and their forms are ever-changing. It can be said that each evaluation function is a player, for different chess type each player naturally has different views and countermeasures, of course, their chess strength is therefore different.

But game tree search is relatively fixed, its core idea is nothing more than to let the computer consider the current situation after N steps all possible circumstances, of which odd steps (because now it is AI's turn) to let the AI side score the most, even steps to let the AI side score the least (because the opponent is human, you can also choose the optimal strategy).

Of course, such searches are extremely computational, and at this point they need to be pruned to reduce the amount of computation. For example, the following image:

 Python Realizes AI Five Chess (First Edition)1

A stands for AI and P stands for human. T he AI side searches for the maximum value, and the human side searches for the minimum value. S o the final result of Layer3's A1 search down is 4, Layer3's A2 searches down, first searches for Layer4's P3, gets a score of 6, considering that Layer2's P1 takes the smaller values of Layer3's A1 and A2 when searching down, and Layer3's A2 searches for P3 in Layer4, its value must be greater than Layer3's A1, so there is no need to search The path 3 from Layer3 to Layer 4 can be cut off.

The essence of the above search strategy is:

Minimax algorithm s alpha-beta pruning algorithm.

Once you understand the above principles, you can write your own code. Of course, in the actual implementation process, I made some simplifications, but it is not far from the family, its core ideas are the same.

The implementation process is detailed in the source code in the relevant file.

Use the demo

Run the file in the cmd window.

The following video is my game against AI, I'm black first, so it's easier to win T_T. b20> After all, the five-piece chess pioneer has a huge advantage, or in some cases/rules is a must-win. A s for the reasons, two papers are available in the relevant documents, which can be interesting to see.


After there is time will study the AI five-piece chess algorithm based on deep learning, the current version is still relatively low, of course, you and it under the words, it can still come out of some subtle steps.

T_T not carefully tested, if there are any bugs can leave a message feedback to me, I will fix.