国际象棋简单ai

by Lauri Hartikka

通过劳里·哈蒂卡(Lauri Hartikka)

(A step-by-step guide to building a simple chess AI)

Let’s explore some basic concepts that will help us create a simple chess AI:

让我们探索一些基本概念,这些概念将帮助我们创建一个简单的国际象棋AI:

  • move-generation
  • board evaluation
  • minimax
  • and alpha beta pruning.

At each step, we’ll improve our algorithm with one of these time-tested chess-programming techniques. I’ll demonstrate how each affects the algorithm’s playing style.

在每一步中,我们都会使用这些经过时间考验的国际象棋编程技术中的一种来改进算法。 我将演示每种方法如何影响算法的播放风格。

You can view the final AI algorithm here on GitHub.

您可以在GitHub上查看最终的AI算法。

(Step 1: Move generation and board visualization)

We’ll use the chess.js library for move generation, and chessboard.js for visualizing the board. The move generation library basically implements all the rules of chess. Based on this, we can calculate all legal moves for a given board state.

我们将使用Chess.js库生成棋盘 ,并使用Chessboard.js可视化棋盘。 移动生成库基本上实现了所有国际象棋规则。 基于此,我们可以计算给定董事会状态下的所有合法举动。

Using these libraries will help us focus only on the most interesting task: creating the algorithm that finds the best move.

使用这些库将帮助我们仅专注于最有趣的任务:创建找到最佳动作的算法。

We’ll start by creating a function that just returns a random move from all of the possible moves:

我们将从创建一个函数开始,该函数仅从所有可能的移动中返回随机移动:

Although this algorithm isn’t a very solid chess player, it’s a good starting point, as we can actually play against it:

尽管此算法不是非常可靠的国际象棋棋手,但它是一个很好的起点,因为我们可以与之抗衡:

(Step 2 : Position evaluation)

Now let’s try to understand which side is stronger in a certain position. The simplest way to achieve this is to count the relative strength of the pieces on the board using the following table:

现在,让我们尝试了解在特定位置上哪一方更强。 实现此目的的最简单方法是使用下表来计算木板上零件的相对强度:

With the evaluation function, we’re able to create an algorithm that chooses the move that gives the highest evaluation:

使用评估功能,我们能够创建一种算法,该算法选择具有最高评估能力的举动:

The only tangible improvement is that our algorithm will now capture a piece if it can.

唯一切实的改进是,我们的算法现在可以捕获一块。

(Step 3: Search tree using Minimax)

Next we’re going to create a search tree from which the algorithm can chose the best move. This is done by using the Minimax algorithm.

接下来,我们将创建一个搜索树,算法可以从中选择最佳移动。 这是通过使用Minimax算法完成的。

In this algorithm, the recursive tree of all possible moves is explored to a given depth, and the position is evaluated at the ending “leaves” of the tree.

在该算法中,将所有可能移动的递归树探索到给定深度,并在树的末尾“叶子”处评估位置。

After that, we return either the smallest or the largest value of the child to the parent node, depending on whether it’s a white or black to move. (That is, we try to either minimize or maximize the outcome at each level.)

之后,根据要移动的是白色还是黑色,我们将子节点的最小值或最大值返回到父节点。 (也就是说,我们尝试在每个级别上最小化或最大化结果。)

With minimax in place, our algorithm is starting to understand some basic tactics of chess:

有了minimax,我们的算法就开始了解国际象棋的一些基本策略:

The effectiveness of the minimax algorithm is heavily based on the search depth we can achieve. This is something we’ll improve in the following step.

minimax算法的有效性很大程度上取决于我们可以实现的搜索深度。 这是我们将在下一步中改进的内容。

(Step 4: Alpha-beta pruning)

Alpha-beta pruning is an optimization method to the minimax algorithm that allows us to disregard some branches in the search tree. This helps us evaluate the minimax search tree much deeper, while using the same resources.

Alpha-beta修剪是对minimax算法的一种优化方法,它使我们可以忽略搜索树中的某些分支。 这有助于我们在使用相同资源的情况下更深入地评估minimax搜索树。

The alpha-beta pruning is based on the situation where we can stop evaluating a part of the search tree if we find a move that leads to a worse situation than a previously discovered move.

alpha-beta修剪基于这样的情况:如果我们发现比以前发现的移动导致更糟的情况的移动,我们可以停止评估搜索树的一部分。

The alpha-beta pruning does not influence the outcome of the minimax algorithm — it only makes it faster.

alpha-beta修剪不会影响minimax算法的结果-只会使其更快。

The alpha-beta algorithm also is more efficient if we happen to visit first those paths that lead to good moves.

α-β算法也更有效的,如果我们碰巧访问这些路径是导致好棋。

With alpha-beta, we get a significant boost to the minimax algorithm, as is shown in the following example:

有了alpha-beta,我们极大地提高了minimax算法,如以下示例所示:

Follow this link to try the alpha-beta improved version of the chess AI.

单击此链接尝试国际象棋AI的alpha-beta改进版本。

(Step 5: Improved evaluation function)

The initial evaluation function is quite naive as we only count the material that is found on the board. To improve this, we add to the evaluation a factor that takes in account the position of the pieces. For example, a knight on the center of the board is better (because it has more options and is thus more active) than a knight on the edge of the board.

初始评估功能非常幼稚,因为我们只计算板上的材料。 为了改善这一点,我们在评估中增加了考虑部件位置的因素。 例如,板子中央的骑士比板子边缘的骑士更好(因为它有更多选择,因此更活跃)。

We’ll use a slightly adjusted version of piece-square tables that are originally described in the chess-programming-wiki.

我们将使用经过稍作调整的棋子方桌版本,该版本最初在chess-programming-wiki中进行了描述。

With the following improvement, we start to get an algorithm that plays some “decent” chess, at least from the viewpoint of a casual player:

经过以下改进,至少从休闲玩家的角度出发,我们开始获得一种可以下“体面”象棋的算法:

(Conclusions)

The strength of even a simple chess-playing algorithm is that it doesn’t make stupid mistakes. This said, it still lacks strategic understanding.

即使是简单的下棋算法,其优点也在于它不会犯愚蠢的错误。 也就是说,它仍然缺乏战略上的了解。

With the methods I introduced here, we’ve been able to program a chess-playing-algorithm that can play basic chess. The “AI-part” (move-generation excluded) of the final algorithm is just 200 lines of code, meaning the basic concepts are quite simple to implement. You can check out the final version is on GitHub.

通过这里介绍的方法,我们已经能够编写能够下棋的国际象棋算法。 最终算法的“ AI部分”(不包括移动代)只有200行代码,这意味着基本概念非常容易实现。 您可以在GitHub上查看最终版本。

Some further improvements we could make to the algorithm would be for instance:

我们可以对该算法进行一些进一步的改进,例如:

If you want to learn more, check out the chess programming wiki. It’s a helpful resource for exploring beyond these basic concepts I introduced here.

如果您想了解更多信息,请查阅国际象棋编程维基 。 这是探索我在这里介绍的这些基本概念之外的有用资源。

Thanks for reading!

谢谢阅读!

翻译自: https://www.freecodecamp.org/news/simple-chess-ai-step-by-step-1d55a9266977/

国际象棋简单ai