14 steps to achieve a powerful AI Gobang game

Source: Internet
Author: User

I am also an artificial intelligence work ... The first apology, from Word to livewrter, a lot of formatting, and did not do code highlighting ... Everyone is living to see ... Want to be a good person machine chess Gobang, can say need to consider the problem is still a lot of, we will make the process that has the formidable AI Gobang divides into 14 steps, let me step by step introduction.

The first step is to understand the rules of the forbidden hand

Do a Gobang program, natural to Gobang need to have enough understanding, now the default everyone now and I study gobang before understanding is as much. On this basis, the most unfamiliar aspects are introduced. There are actually two kinds of rules for Gobang: the prohibition of hands and the prohibition of hands. Because the rule of the ban is relatively simple, it is accepted by more people. In fact, for the professional under the Gobang, there is a ban on hand is the rule. So, here is a brief introduction to the "Forbidden hand":

Gobang in the "winning the tempo" has been demonstrated, similar to the "Flower-moon formula" and "Pu-moon formula", a lot of the winning of the tempo although the need for a lot of memory, but the master can do to win. So the rules of Gobang are optimized, and the "Gobang" is obtained. In Gobang, Black is bound to advance. Therefore, "Forbidden hand" Gobang competition in the Black has the following "Ban hand" restrictions: "33 prohibition": black chess position at the same time form more than two three; "44 Forbidden": Black chess position at the same time to form two more than four; "Long Lian Ban": Six son above the black chess to connect into a gleam. Black chess as follows "ban hand" then immediately lose the chess game. However, if "even five" and "ban" at the same time the "ban" is ineffective. So for black chess only four live three (the latter will have an explanation) is no solution to the situation. White is a more than a way to win, that is, forced black must be under the ban point.

In order to cater to all players, Gobang naturally needs to make two versions, or can be controlled by the hands of the ban.

The second step is to implement the game interface

Here, I made a simple interface, but for the man-machine chess, absolutely enough. And a lot of beautiful online interface, my interface may be slightly rough, but, the development of high speed, only in less than half a day. Let's take a quick look at the approach of the interface below.

interface I used WPF, presentation layer and logic layer completely separate, the foreground can be dragged through the layout, here is not too much introduction. A brief introduction to the screen screen

1 actual listing two gradient label stitching, 2, 3 is two label,4, 5 is actually two button, but did not do event response. Modify the content properties of the label and button through the control of Buttons 6, 7, 8, and 9. Perhaps some people will wonder why the button will not be able to see the shadow of a button, where Comrade Whrxiao wrote a style as follows

<style x:key= "ButtonStyle1" targettype= "{x:type Button}" >

<setter property= "Template" >

<Setter.Value>

<controltemplate targettype= "{x:type Button}" >

<Grid>

<contentpresenter horizontalalignment= "{templatebinding horizontalcontentalignment}" VerticalAlignment= "{ TemplateBinding Verticalcontentalignment} "snapstodevicepixels=" {TemplateBinding Snapstodevicepixels} " recognizesaccesskey= "True"/>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

Here we call this style Style1. In the logic of the interface, the Boolean value of two global variables will be started, whether the hands are forbidden or not, and the value of bool can be logically controlled by setting and judging. In the middle of the chessboard is a canvas, a 15*15 grid filled with button and each button to apply Style1 the beginning of the transparency set to 0, that is, simply do not see, in chess when changing the background and transparency of the button to achieve drop effect, Because of the position of the grid, it looks as if it is at the intersection of the following lines.

The third step is to win or lose judgment:

Because the rules are different, "no ban" and "have forbidden hands" of the winning and losing judgments are naturally different. First look at the open hand: This is relatively simple, traversing each position, and then starting from this position, respectively, to determine its four directions: that is, horizontal, vertical, left up to the lower right, left down to the upper right. Each direction starts at the middle point, counting the number of children on both sides, and then adds and adds one (the middle piece) to the two-direction word. If the result is greater than or equal to 5, then it means that the side wins chess.

For the Gobang, the winning and losing judgment also need to judge the ban, the ban on the hands of the decision is more complex. Place the points to be judged into black pieces. Then search the perimeter of the game to determine the chessboard; to restore the chessboard; Use the search results in order to analyze the parties, to determine whether the formation of the chess formed by Black to form a long link or form some kind of four-link or three-linked chess type. If the formation of long company, the decision is forbidden hand, return long ban hand logo. If the form of a four-or three-linked chess-type, the chess-type statistics plus 1, and then the next direction to judge, until the analysis of the end of each direction. If the statistics of the four-link chess type or the three-link chess type are greater than 1, then the return is the forbidden hand. The remaining cases returned to the non-proscribed hands.

Fourth Step: Structural Chess Type Assessment

The "ban hand" rule is more complex, involving more chess skills, and the idea of the algorithm has no effect, so the following we mainly consider the rules of the free hand ai design. If the design of a good hand ai, only to allow the AI to hold the black resolute not to stop the hand point, you can quickly construct the AI ban hand. Although this approach does not take advantage of the techniques under the rule of prohibition, these techniques only need to modify the estimate function as described below.

We can divide the Gobang into the following categories:

Into 5: That is, the formation of five sub-alignments

Live 4: That is, the composition of the two sides are not intercepted four alignments.

Dead 4: The four sides intercepted by the side

Live 3: Three words that are not intercepted on either side

Dead 3: Three words intercepted on one side

Live 2: Two sides that are not intercepted

Dead 2: Side intercepted two alignments

List: Four weeks without attached pieces

According to Gobang's technique, we can classify the chess type of Gobang by the way of alignment, and then we will grade each chess type according to the power after classification. Because Gobang only one son at a time, it is easy to understand that the two live three and three live three of the same power is the same, similar to not much to explain. In the program, I divided 100 into full marks, the chess type of the following rating:

into 5, 100 minutes.

Live 4, double Dead 4, Die 4 Live 3, 90 points

Double Live 3, 80 minutes

Dead 3 Live 3, 70 points

Dead 4, 60 minutes.

Live 3, 50 minutes.

Double Live 2, 40 minutes

Dead 3, 30 minutes.

Live 2, 20 minutes.

Dead 2, 10 minutes.

List 0 Points

With the method of estimation, there is the basis of Gobang AI, and then there are some game methods.

Fifth step: Get position estimate AI

Simple application of games and the current situation of gobang analysis of each step to the assessment, the program to do the following: the analysis of each position, assuming the AI drop in the position, using the above scoring rules for AI scoring, and will get the score plus one. Then, suppose the player drop at that point, scoring the player and then summarizing all the points. Take the highest score as the assessment of this position, next is to take the highest points in the position of chess. "Position assessment", when playing chess, can take into account their opponents, but also take into account the opponent's defense, you can say, most of the time can be considered on top of the two-step AI. As an experiment, downloaded from the Internet a game of AI, and "location assessment" to the next, the result is a win-minus. Who first son, who wins. And the one-step estimate is undoubtedly the fastest, even if you traverse all the positions, you can quickly make decisions.

Sixth step: applying game tree to improve AI intelligence

Do Gobang game, will naturally use the game tree, here I say their own ideas. In the game, according to the next step by WHO to go, AI for any situation according to the previous estimate method to give a score, we summed up this estimate method into an evaluation function, and return the score. The next step is chosen accordingly. Since the person and AI are taking turns drop, you can calculate the person's estimate and add a minus sign to the front. So, the greater the value of the AI, the better the evaluation, the smaller the assessment indicates that the more adverse to the AI. Each time the AI selection is from a node in the possible tree of its path, the maximum point in the evaluation value is returned. The user always chooses the minimum point from a certain layer of the walking tree to form a minimax search tree, and then, according to the depth first search, can finally get the best way to go under the fixed search depth. I did the next test, the pure application of game trees, can be in the 100ms to allow AI to consider the complete two steps, because the combination of explosions, when the need to consider three steps, it needs 6s or so, 4 steps will take 1 minutes. Take two steps to compare with one step estimate, although relatively slow, but do have a certain intelligence.

Seventh Step: Considering layer number, improve AI intelligence

The above design is uniformly handled for the return value, but the layer is a very important information. Because if you can win 2 steps in chess, you should not choose 4 steps to win. For the loss of the number of chess layer is more important, AI must be as long as possible to delay the loss of time, there is a greater likelihood of the AI saved. This can be done by setting a DEP value. Depth about shallow, the greater the DEP, with DEP and get the score multiplied, get the search node score, and then carry out the above algorithm, further improve the AI intelligence.

Eighth step: Application of Βsearch pruning, improve the AI speed

In the process of searching the game tree, there are actually many points in the search that are superfluous, such as the following figure

Figure, the Square box node is the AI walk, the Circular box node is the person walking. For example, c node, it needs to select the largest value from E and F. Now that E is 2, when the search F node, because F is a human walking node, then f need to choose from the K L m the smallest, because K is already 1, that is to say f<=1, then l,m do not need to search, so the alpha pruning occurred. Then look at a node, the person is gone, need to choose the minimum from C and D, because C node is 2, and G is 7, then D is at least 7. Therefore, the other nodes of D do not need to be considered, and the beta pruning as shown in the above image occurs. To sum up the above rules, we can get a pruning method as follows:

Currently for AI chess node:

Alpha Pruning: If the value of the current node is not greater than the value of the parent node's former sibling node, this node is discarded.

Beta pruning: If the value of the current node node is not smaller than the minimum value in the former sibling node of the current nodes, discard the child node and all the sibling nodes of the child node.

Currently playing chess node for user:

α pruning: If the value of a child node of the current node is not greater than the maximum value in the former sibling node of the current node, the child node and all the sibling nodes of that child node are discarded.
Beta pruning: This node is discarded if the value of the child node of the current node is not smaller than the minimum value in the former sibling node of the current parent node.

After Βsearch pruning, can greatly reduce the number of searches, many times, can be billions of of the number of searches, reduced to hundreds of millions of, then, you can increase the depth of the search 1.

nineth Step: The application of chess range, improve the AI speed

The number and order of child nodes of the current node have a critical impact on the speed of the search. According to the characteristics of Gobang, can produce a chess surface search scope. To record the most left and right of all the pieces on the current chess surface, we think the next move will not be more than 3 steps away from this box. In this way, the number of search nodes is greatly reduced when fewer pieces are found. You can increase the speed of AI by about one times.

Tenth step: Use the Chess type score, raises the AI speed

Because each dismount corresponds to a score, so you can only consider the current score before the first ten nodes for the next search, greatly reducing the search scope, you can further increase the depth of the search.

11th Step: Use permutation table, improve AI speed

 

We generally use the recursive method to implement the game tree, however, the efficiency of recursion is low, and it is obvious that there are many repeated search nodes, so we can use a table, record all the search points of the situation, and then as long as the discovery of the node, you can directly get results. What is placed in this "table" is a permutation table, using the Zobrist algorithm, hash processing, so that the time to find in the table greatly shortened, so that the speed of AI can be increased by an order of magnitude.

12th step: Using multiple threads to improve the AI speed

In fact, we can use multi-core technology, using multiple threads, so that the algorithm to achieve parallel computing, improve the speed of AI. In the first layer we use a thread allocator to allocate the second-tier candidate nodes to multiple threads, each containing a search that starts with a candidate node in the second tier, and then, when all threads are finished, aggregates the results of all threads to select the maximum value. Parallel programs, you can increase the speed by about one times.

13th step: Using the randomization algorithm, so that the determination method can not win

Because of the fixed nature of AI algorithm, so a player to win, according to the same way, will win again. But in addition to must kill recruit or must prevent recruit, a situation many times have no absolute best walk method. But there are some good ways to go, then you can put these scores almost go together, and then randomly choose one of them to avoid the AI's approach to the fixed. The easiest way to avoid fixing the method AI must lose.

The 14th step: Let Ai learn from the same place no more mistakes

The above algorithm has not self-learning ability, so the AI in chess may also be the same mistake. So at the end of each game, if the AI loses, then the number of steps that are larger than the search depth is rolled back. Can be counted as the number of search depth of the situation as the target situation, from the countdown to the depth of the situation to predict, to find the situation will not export the goal situation. Then record the situation and the previous situation, and modify the scoring function accordingly. So the AI will not commit the mistakes that have been made, to achieve the effect of self-learning.

Do the above 14 steps, a powerful AI Gobang game can be born.

Turn from: http://www.cnblogs.com/goodness/archive/2010/05/27/1745756.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.