使用Python创建属于你的国际象棋AI
Python3
最后更新 2020-10-23 16:23
阅读 120
最后更新 2020-10-23 16:23
阅读 120
Python3
##FlyAI文献翻译
**英文原文:**[:Let’s create a Chess AI](https://medium.com/dscvitpune/lets-create-a-chess-ai-8542a12afef)
> **标签** :Python
![](https://static.leiphone.com/uploads/new/sns/article/202010/1602836298314379.png)
Chess has been played by the world’s masterminds for ages and is the game where the brain needs a lot of processing. So, how can we teach a computer to play a mastermind game like chess? Let’s check it out.
To get started all you need is a python3 interpreter installed in your system and the basic level of python3. Cool right?
> 国际象棋已经被世界上的智囊们玩了很久,是一种需要大脑进行大量处理的游戏。那么,我们怎样才能教电脑玩象棋这样的游戏呢?我们来看看吧。
>要开始使用,您只需要在系统中安装python3解释器和python3的基本级别。很酷吧?
![](https://static.leiphone.com/uploads/new/sns/article/202010/1602836302228149.png)
**Today’s Outline**
1. Board Representation
3. Board Evaluation
5. Move Selection
7. Testing our AI
9. Making an Interface
>**今天的概要**
>1. Board 表现形式
2. Board 评估
3. 移动选择
4. 测试AI
5. 制作界面
**Board Representation**
![](https://static.leiphone.com/uploads/new/sns/article/202010/1602836298728531.png)
Before writing any algorithm for our engine it’s important for us to code the logic behind chess pieces i.e. assign every possible legal move to each and every chess piece.
For that, our work has been made a lot simpler by the [python-chess](https://github.com/niklasf/python-chess "python-chess") library which will provide us with all of the move generation and validation. So, let’s start by installing it.
>在为引擎编写任何算法之前,对我们来说,对棋子背后的逻辑进行编码非常重要,也就是说,将每一个可能的合法举动分配给每一个棋子。
>为此,python-chess 库使我们的工作变得简单得多,该库将为我们提供所有移动生成和验证。 因此,让我们从安装它开始。
```
!pip install python-chess
```
Now, as we have the library installed, we can start by importing and then initializing the board.
>现在,我们安装好了库,我们通过导入和初始化棋盘开始。
```
import chess
board = chess.Board()
board
```
This will be the output which you will be able to see in your notebook.
>下图所示将会输出在你的笔记本上
![](https://static.leiphone.com/uploads/new/sns/article/202010/1602836297472553.png)
Output-1
This board object will be the complete board representation which will provide us with some important functions such as board.is_checkmate() to check if there’s a checkmate, board.push() to append a move, board.pop() to undo the last move, etc. You can read the complete documentation [here](https://python-chess.readthedocs.io/en/latest/ "here").
> 输出-1
>该棋盘对象的表示形式是完整的棋盘,它将为我们提供一些重要的功能,例如board.is_checkmate()来检查是否有一个棋盘格,board.push()来附加一个动作,board.pop()来撤销最后一个移动等。您可以在here阅读完整的文档。
**Board Evaluation**
For evaluating the board initially, we must think about how a Grandmaster would think in his respective match.
>**Board评估**
>为了初步评估Board,我们必须考虑大师在各自比赛中的想法。
![](https://static.leiphone.com/uploads/new/sns/article/202010/1602836298516551.png)
ome of the points which should come in our mind are:
1. Avoid exchanging one minor piece for three pawns.
3. Always have the bishop in pairs.
5. Avoid exchanging two minor pieces for a rook and a pawn.
So, the equations from the above insights will be:
1. Bishop > 3 Pawns & Knight > 3 Pawns
3. Bishop > Knight
5. Bishop + Knight > Rook + Pawn
By simplifying the above equations, we get Bishop > Knight > 3 Pawns. Also, we must convert the third equation as Bishop + Knight = Rook + 1.5 Pawn as two minor pieces are worth a rook and two pawns.
>在我们脑海中浮现的一些要点:
>1. 避免将一个较小的棋子换成三个兵。
2. 使象成对出现。
3. 避免将两个棋子交换车和兵。
>从以上见解中得到以下公式:
>1. 象>3兵 &马>3兵
2. 象>马
3. 象+马>车+兵
>通过简化以上等式,我们得到象> 马> 3 兵。 此外,我们必须将第三个方程转换为 象 + 马 = 车 + 1.5 兵,因为两个较小的棋子值得一个车和两个兵。
![](https://static.leiphone.com/uploads/new/sns/article/202010/1602836300572071.png)
We will use [piece square tables](https://www.chessprogramming.org/Piece-Square_Tables "piece square tables") to evaluate our board pieces and the values will be set in an 8x8 matrix such as in chess such that it must have a higher value at favorable positions and a lower value at a non-favorable place.
For example, the probability of the white’s king crossing the centerline will be less than 20% and therefore we will place negative values in that matrix.
Let’s take another example, suppose the Queen, she would like her to be placed at the center position as she can dominate more number of positions from the center and therefore we will set higher values at the center and the same for the other pieces as chess is all about defending the king and dominating the center.
Enough of the theory lets initialize the piece square tables:
>我们将使用棋子方桌来评估我们的棋盘棋子,并将这些值设置在8x8矩阵中(例如象棋中),以使其在有利位置必须具有较高的值,而在不利位置则必须具有较低的值。
>例如,白人国王越过中心线的概率将小于20%,因此我们将负值放置在该矩阵中。
>让我们再举一个例子,假设王后,她希望将她放置在中心位置,因为她可以控制中心位置的更多位置,因此我们将在中心设置更高的值,而在其他棋子上将其设置为更高的值 就是要保卫国王并控制中心。
>足够的理论让我们初始化块棋盘: