Understanding and creation of "algorithm Design" (Integrated) game tree

Source: Internet
Author: User

An understanding of a game tree is simply a tree that is created by analyzing all possible results of the next step in the current steps after analyzing the possible results of each step

The professional represents the maximal minimum game tree: The Minimax game tree is named for a simple algorithm that depicts this structure. Let's assign a score to the results of the TTT game. If the fork (X) wins, the score is 1. If the circle (O) wins, the score is-1. Now the fork will try to get a maximum score, and the circle will try to minimize the score. So the first researcher to study the problem decided to name the player Fork Max and the player's circle as Min. Therefore, this complete data structure is named the maximal (Max) minimum (min) game tree.

For example:

The creation of a game tree requires weights for any possible result: for example, the following weights are set up in Othello

Even 3,100 points

250 points Double-connected

Draw 0 Points

Dead Heat 1

If you score, dead heat will continue searching until you find three other states,

Using the game tree, because the results may be a lot of necessary pruning, the idea of the algorithm is as follows:

    1. Min computer ai chess, if the number of steps to consider 0, then the direct return to the current Checkerboard value W (the larger the number represents the more advantageous to Max, the smaller the greater the advantage for Min, w=maxw-minw).

    2. If you consider the number of steps to N, first get min computer can play chess position steps.

    3. For a step that can play chess, the computer AI plays chess to step row, column.

    4. If this time min computer has won, then the board back one step, return to the board valuation and chess position, no longer consider the other way.

    5. Otherwise, Min needs to be in every way, choose a way, so that Max Human walk N-1 step, their own advantage to maintain the maximum (that is, the lowest W value).

    6. What is Alpha-beta pruning? That is, if Max Human is now a way to go 1 at least to get alpha advantage, and another way 2,min computer's one-step chess may allow humans to gain a smaller advantage than Alpha, then Max Human certainly do not choose to go 2, so calculate in the calculation of Min computer's way, The other way to the Min computer is no longer counted.

    7. Last min computer after the steps.length of the method of comparison, choose a way to minimize the W value, the board back one step, and return to the board valuation and chess position.

    8. In the same way that Max walks, humans choose to play chess with the largest W value, so the Max function and the Min function represent humans and AI playing chess, recursively calling each other, until recursion returns to the value of N-Step after the number of steps is 0 o'clock.

If the first sub-node of each inner node of a tree game tree returns the optimal solution, the tree is called well-ordered. For a well-ordered game tree, the Alpha-beta algorithm trims the subtree that is not necessarily searched, and the tree after pruning is called the smallest tree (minimal, or critical tree). When the game tree is good order, the Alpha-beta algorithm needs the search subtree to be included in the smallest tree. According to the classification method of the above nodes, all the nodes in the minimum tree are of the defined type. Because those types of undefined nodes are pruned.


Homogeneous game tree and its minimum tree

If all the internal nodes (interior node) of a game tree have the same branching factor, and all the root nodes are the same depth as the leaf nodes, then the search tree is an even (uniform). The smallest tree of a well-ordered game tree with a depth of D, from the root node to the leaf node, Went through the D-side. The first edge of the set I is the first branch of its parent node. Will all follow the "." Connected together to form a string:. Set to the first value greater than 1 in the string. If it does not exist, that is, all 1, then the leaf node is the PV node. If present, then the corresponding edge of the sub-node must be cut node. If D-j is an even number then the string corresponds to the Cut node, otherwise, if D- J is an odd number, and the leaf node is the all node.

For a cut type of leaf node, it corresponds to the string existence of the property: to all I, if the d-j is an even number, then 1. This string (except for all 1 strings) and cut leaf node one by one corresponds. The number of such strings is, therefore, the number of cut leaf nodes is also.

Similarly, for a leaf node of all type, it corresponds to a string that has a property: to all I, if d-j is odd, then 1. Such a string (except for all 1 strings) and all leaf node one by one corresponds. The number of such strings is, so the number of all leaf nodes is. Plus the PV leaf node, A minimum tree contains the number of leaf nodes, which is also the minimum number of leaf nodes to search in the Alpha-beta algorithm in the homogeneous game tree.

Reference game tree to build the code.

intGameState (Char_board[9]){    intState ; Static inttable[][3] =     {        {0,1,2},         {3,4,5},          {6,7,8},         {0,3,6},          {1,4,7},          {2,5,8},          {0,4,8},          {2,4,6},      }; CharChess = _board[0];  for(Chari =1; I <9; ++i) {chess&=_board[i]; }      BOOLIsfull =0!=chess; BOOLIsfind =false;  for(inti =0; I <sizeof(table)/sizeof(int[3]); ++i) {chess= _board[table[i][0]]; intJ;  for(j =1; J <3; ++j)if(_board[table[i][j]]! =chess) Break; if(Chess! = Empty && J = =3) {Isfind=true;  Break; }        }        if(Isfind)//got win or losestate = Chess = = O?Win:lose; Else        {            if(isfull)//All position have been set without win or lose                returnDRAW; Else            {                //finds[0], ' o ', finds[1], ' x '                intfinds[2] = {0, };  for(inti =0; I <sizeof(table)/sizeof(int[3]); ++i) {BOOLFindempty =false; Chess=0xFF; intJ;  for(j =0; J <3; ++j)if(_board[table[i][j]] = = Empty &&!findempty) Findempty=true; ElseChess&=_board[table[i][j]]; if(chess = = O | | chess = = x) &&findempty) {Isfind=true; if(O = =chess)++finds[0]; Else++finds[1]; }            }             if(finds[0] >1&& finds[1] <1)                 //2 ' o ' have been founded twice in row, column or diagonal directionstate =-(INFINITY/2) * finds[0]; Else if(finds[1] >1&& finds[0] <1)                 //2 ' x ' have been founded twice in row, column or diagonal directionstate = INFINITY/2* finds[1]; Else                 //need to search more.State =INPROGRESS; }    }     returnState ;}

Understanding and creation of "algorithm Design" (Integrated) game tree

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.