14-step wuziqi game with powerful AI
Blogger Press: I have a deep understanding of this Article. After all, the first C language in my college is wuziqi, after more than a decade, I used Java to re-design the mobile version of The wuziqi game in Android. This article is very practical in the analysis of the wuziqi algorithm, and I feel a little anxious, I want to improve AI.
There are still a lot of issues to consider when you want to be a good man-machine player. We will divide the process of making a powerful AI player into fourteen steps. Let me introduce it step by step.
Step 1: unban the manual rule
To be a wuziqi program, you naturally need to know enough about wuziqi. By default, you now know as much about wuziqi as you did before. Based on this, we will introduce aspects that most people are not familiar. Wuziqi has two types of rules: banned and unbanned. Since the rule of No-ban is relatively simple, it is accepted by more people. In fact, it is the rule for professional players to ban the game. Here, we will give a brief introduction to "banned users:
In wuziqi, the "first hand wins" has been demonstrated. Similar to the "Flowers and months" and "Pu Yue", many first players must win the next method. Although they need a lot of memory, they can indeed win. Therefore, the rules of wuziqi were optimized to get the "banned players" wuziqi. In wuziqi, Black is a must. Therefore, the following restrictions are imposed on black games: "three prohibitions": Two or more three are formed at the same time in the position of black games; "Four prohibitions ": the playing position of the black game forms more than two things at the same time; "long connection ban": Black games with more than six sons are connected to one line. As follows, black game immediately loses the game if it says "no players. However, if both "Lian Wu" and "Ban Shou" appear at the same time, "Ban Shou" is invalid. Therefore, for black games, only four active three (which will be explained later) are unsolvable. In contrast, the white game has another way to win, that is, forcing the black game to be banned.
In order to cater to all players, wuziqi naturally requires two versions, or can be controlled by banned players.
Step 2: implement the game interface
Here, I have made a simple interface, but it is absolutely sufficient for man-machine compaction. Compared with many exquisite web interfaces, my interface may be slightly rough, but it takes less than half a day to develop at a high speed. Next, let's take a look at the lower-bound practices.
I used WPF on the interface. The presentation layer and the logic layer are completely separated. You can drag and drop the front-end to complete the layout. I will not describe it too much here. Brief Introduction Based on the Interface
The first part is actually the splicing of two gradient labels, 2 and 3 are two labels, and 4 and 5 are actually two buttons, but no event response is made. Modify the content attribute of the label and button through buttons 6, 7, 8, and 9. Some may wonder why the button does not have any shadow. Here, the comrade-in-arms 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 placement = "{templatebinding placement}" verticalalignment = "{templatebinding placement}" snapstodevicepixels = "{templatebinding snapstodevicepixels}" placement = "true"/>
</GRID>
</Controltemplate>
</Setter. value>
</Setter>
</Style>
In this case, we call this style style1. In the interface logic, whether to start, whether to ban the mobile device, and whether to use the computer as the Boolean value of two global variables, and perform logical control by setting and judging the bool value. The middle chessboard is a canvas. A 15*15 grid is filled with a button and the transparency of each button is set to 0 when style1 is applied, when playing a game, you can change the background and transparency of the button to achieve the effect of dropping a child. Because of the grid position, it may look like it is at the vertical line.
Step 3: Determine whether to win or lose:
Because the rules are different, the winning or losing judgments of "no banned users" and "no banned users" are naturally different. First, let's look at the unban: This is relatively simple. Traverse each location and start from this location to determine its four directions: horizontal, vertical, top left to bottom right, bottom left to top right. Start from the center point in each direction, count the numbers on both sides, and then add the number of words in the two directions (the middle pawn ). If the score is greater than or equal to 5, the player wins.
To determine whether a player can win or lose a game, you must determine whether the player can be banned. Place the points to be judged into the black pawns. Then, search for the checkerboard around the points to be judged, restore the checkerboard, and analyze each direction in sequence using the search results, determine whether a long connection or a four-or three-connection chess game is formed after the game is put into the game. If a persistent connection is formed, the user is determined to be banned, and the identity of the persistent connection is returned. If a four-or three-connected chess board is formed, the number of the chess board is increased by 1, and then the next direction is judged until the analysis in each direction ends. If the number of statistics for a four-connected or three-connected chess game is greater than 1, the return result is disabled. In other cases, a non-banned user is returned.
Step 4: Construct a chess score
The rule of "getting banned players" is complex, involves many technical skills in playing chess, and has no influence on the idea of algorithms, therefore, we will mainly consider the AI Design without the ban rules. If no banned AI is designed, you can quickly construct a banned AI by sticking to the banned AI. Although this method does not use skills with banned rules, you only need to modify the scoring function described below.
We can divide the linking of wuziqi into the following types:
To 5: constitute five sub-Beads
Active 4: A Four-Child ball that is not intercepted on both sides.
Dead 4: four sub-beads intercepted on one side
Active 3: Three-character links that are not intercepted on both sides
Dead 3: Three-character ball links intercepted on one side
Active 2: two sub-beads that are not intercepted on both sides
Dead 2: two sub-beads intercepted on one side
List: No connected pawns around
Based on wuziqi's skills, you can classify wuziqi's chess models by ball. After classification, We will rate each type of Chess Model Based on the power. It is easy to understand that wuziqi has only one sub-game at a time. The power of Dual-active and three-active is the same. Similar situations are not explained much. In the program, I scored a full score of 100 and scored the following for the chess model:
5,100 points
Active 4, active 4, dead 4 3, 90
Double active 3, 80 points
Dead 3 active 3, 70 points
Dead 4, 60
Active 3, 50 points
Dual-active 2, 40 minutes
Dead 3, 30 minutes
Active 2, 20 points
Dead 2, 10 points
List 0 Points
With the estimation method, we have the foundation of wuziqi AI, followed by some game methods.
Step 5: obtain location estimation AI
The current situation of wuziqi is analyzed based on the chess spectrum and the function of estimating each step. The program will do the following: analyze each location. If AI falls into this position, use the above scoring rules to score AI and add one to the score. Then, assuming that the player falls into the position, scores the player, and summarizes all the scores. Take the highest score as the estimated score for this position, and then play the game at the highest score. "Location estimation": when playing a game, you can consider both attacking your opponent and defending your opponent. It can be said that you can often consider two steps of AI at the top. As an experiment, I downloaded a game-based AI from the Internet, and compared it with the location score, the result was one win and one loss. Who wins, who wins. In addition, the one-step estimation is undoubtedly the fastest. Even if you traverse all the locations, you can make quick decisions.
Step 6: Apply the game tree to improve AI intelligence
The game tree will naturally be used in the game of wuziqi. Here I will talk about my own ideas. In comparison, AI gives a score for any situation based on the previous evaluation method based on the next step. We summarize this evaluation method into an evaluation function and return the score. Select the Next Step accordingly. Since humans and AI take turns, we can calculate people's estimates and add a negative number to them. The larger the valuation, the more favorable it is for AI. The smaller the estimation, the more unfavorable it is for AI. Then, each AI selection is based on a layer of nodes in its possible tree, and the largest point in the evaluation value is returned. However, the user always selects the minimum point from a layer node of the tree to form a very large and extremely small search tree, and then searches based on the depth first, finally, we can get the best way to get a fixed search depth. I did a test. Simply using the game tree, AI can consider the two complete steps within Ms. Due to the combination explosion, when we need to consider three steps, we need about 6 s, step 4 takes one minute. Take two steps to compare the score with one step. Although it is relatively slow, it is indeed intelligent.
Step 7: Consider layers to improve AI intelligence
The above design processes the returned values in a unified manner. However, the number of layers is an important piece of information, because when playing a game, if you can win in two steps, you should not choose to win in four steps. It is more important to lose the number of chess layers. AI must delay the time to lose as much as possible, so there is a larger possibility of making AI dangerous. In this way, you can set a Dep value. The depth is about shallow. The greater the DEP, the greater the DEP is. The score is multiplied by the score obtained by Dep to obtain the score of the search node. Then, the above algorithm is used to further improve AI intelligence.
Step 8: Use α-β pruning to increase AI speed
In the process of searching the game tree, there are actually many unnecessary points in the search, for example
In the figure, the square frame node is the AI walk, and the circle frame node is the person walk. For example, the C node needs to select the largest value from E and F. At present, it has been concluded that E is 2. When searching for F nodes, F needs to select the smallest node from k l m because K is already 1, that is to say, F <= 1, then L and M do not need to be searched, so α pruning occurs. Then, let's look at node A. When this person leaves, we need to select the smallest value from node C and node D. Because node C is 2 and node G is 7, node D is at least 7. Therefore, another node of D does not have to be considered, as shown in. To sum up the above rule, we can obtain the pruning method as follows:
Currently, it is an AI chess node:
Alpha pruning: if the value of the current node is not greater than that of the former brother node of the parent node, the node is discarded.
Beta pruning: if the value of the subnode of the current node is not smaller than the minimum value of the former sibling node of the current node, the child node and all the subsequent sibling nodes of the subnode are discarded.
Currently, this is the user's chess node:
Alpha pruning: if the value of a subnode of the current node is not greater than the maximum value of the former sibling node of the current node, the child node and all the subsequent sibling nodes of the subnode are discarded.
Beta pruning: if the value of the child node of the current node is smaller than the minimum value of the former brother node of the current parent node, the child node is discarded.
After α-β pruning, the number of searches can be greatly reduced. In many cases, the search depth can be increased by 1 by reducing the number of billions of searches to hundreds of millions.
Step 9: Apply the playing range to improve AI speed
When the number and order of subnodes of the previous node have a crucial impact on the search speed. Based on the characteristics of wuziqi, a range of chess and plane searches can be generated. Record the leftmost
It is a rectangle composed of top, bottom, right, top, and bottom points. We believe that the position of the next move will not be more than three steps out of this frame. In this way, when the number of pawns is small, the number of search nodes is greatly reduced. AI can be doubled.
Step 10: Improve AI speed with chess scores
Because each method corresponds to a score, you can only consider the top 10 nodes in the current score each time for the next search, greatly reducing the search range and further increasing the search depth.
Step 2: Use the replacement table to increase the AI speed
We generally use recursive methods to implement the game tree. However, the recursive efficiency is low and it is obvious that there are many nodes for repeated searches. Therefore, we can use a table, record the status of all searched nodes, and then you can get the result directly if you encounter a searched node. What is placed in this "table" is a replacement table, which uses the Zobrist Algorithm for hash processing to greatly shorten the time for searching in the table, thus increasing the AI speed by an order of magnitude.
Step 2: Improve AI speed with multithreading
We can actually use multi-core technology and multiple threads to Implement Parallel Computing for algorithms and improve AI speed. At the first layer, we use a thread distributor to allocate candidate nodes on the second layer to multiple threads. Each thread contains searches starting from a candidate node on the second layer, after All threads are finished, the results of all threads are summarized to select the maximum value. Parallel Programs can increase the speed by about doubled.
Step 1: Use the randomization algorithm to make sure that the method cannot win
Because of the fixed nature of AI algorithms, a single-game player will surely win again in the same way. However, in addition to the mandatory or mandatory actions, there is often no best way to do this. However, there are some good practices, so we can collect these scores, and then randomly choose one of them to avoid fixing the AI logic. this is the simplest way to avoid losing the fixed method AI.
Step 2: Let AI learn and stop making mistakes in the same place
The above algorithms do not have the ability to learn from each other, so that AI may make the same mistake when playing chess. Therefore, at the end of each game, if AI loses, the number of steps beyond the search depth will be rolled back. You can set the count-to-bottom search depth as the target situation, and add a step-by-step prediction from the bottom-to-end depth to find a situation where the goal is not to be defeated. Record the situation and the previous situation, and modify the number of scoring letters accordingly. In this way, AI will not make mistakes that have been made before, so as to achieve self-learning effect.
In the above fourteen steps, a game of wuziqi with powerful AI will be born!