Gobang AI algorithm-zobrist

Source: Internet
Author: User

This blog is not to Gobang algorithm research thoroughly after writing, but one side of the research algorithm while writing code, while writing a blog, so some of the order of the blog is not quite right, such as Zobrist in fact should be put on the count before the death of the said. However, this does not have a big impact, the overall order is OK.

In addition, this series of blogs about the Gobang code is actually an open source project, source address: Https://github.com/lihongxun945/gobang

Because is the side writes the code side writes the blog, therefore the code in the blog is not the newest, even has the bug, therefore the source code please as far as possible refer to above the open source project's codes. For example, when the max-min search was changed to a negative maximum, there was an important bug in the player's rating and the bug was fixed in the post-post submission.

Zobrist

Zobrist is a fast hash algorithm, ideal for use in a variety of board games (in fact, there are a lot of applications in various board games).

We've talked about negative maximum search and counting, and in fact there are often repeated searches, such as this:

[7,7],[8,7],[7,6],[7,9]

In fact, it and the following this way is only a different order, the final step out of the situation is the same:

[7,6],[7,9],[7,7],[8,7]

So if we hit the above two cases in the search, we will have to rate the two cases, and actually have the first score, can be cached, the second time without scoring directly using the cache data. In addition to this situation, the previous search results can also be saved, can be used in heuristic search.

So the question now is, how should we express a situation? Obviously, a hashing algorithm is required, and the algorithm cannot be too slow, or it may slow down the search. And Zobrist is a fast array hashing algorithm that satisfies our needs. Refer to Https://en.wikipedia.org/wiki/Ben_Zobrist for Zobrist algorithm

Zobrist efficiency is very high, each next move, only need to do one 异或 operation, compared to each step of the score, this time the different or operation of the performance cost can be negligible. Zobrist specific implementations are as follows:

  • Initializes a two Zobrist[M][M] -dimensional array where m is the checkerboard width of the Gobang. Of course it can be Zobrist[M*M] a one-dimensional array. Set two is for one to represent black chess, one to represent a white flag.
  • Each of the above arrays is filled with a random number that is guaranteed to be at least 32 bits long (that is, 32bit), preferably 64 bits. The initial key value is also set to a random number.
  • For each next move, the result is the new key value, using the random number of the corresponding position in the current key or Zobrist array. If you delete a pawn (undo), it will be different or one time.

The corresponding JS code is as follows:

1 varZobrist =function(size) {2    This. Size = Size | | 15;3 }4 5Zobrist.prototype.init =function() {6    This. com = [];7    This. Hum = [];8    for(vari=0;i< This. size* This. size;i++) {9      This. Com.push ( This. _rand ());Ten      This. Hum.push ( This. _rand ()); One   } A  -    This. Code = This. _rand (); - } the  -Zobrist.prototype._rand =function() { -   returnMath.floor (Math.random () * 1000000000);//One more of them overflowed.  - } +  -Zobrist.prototype.go =function(x, y, role) { +   varindex = This. Size * x +y; A    This. Code ^= (role = = r.com?) This. Com[index]: This. Hum[index]); at   return  This. Code; -}

Source in zobrist.js in the file.

Note that a zobrist operation is performed each time moves. Never design a hash function yourself unless you can guarantee that your hash function is simpler than the XOR of a 64-bit integer at a time, and that the probability of a conflict is low. Zobrist the number of random numbers in the array 质量 is very important, but I use JS built-in Math.random() random number of generated temporarily no problem, if the randomness is not high enough, you can consider swapping some better random functions.

With this fast hash algorithm, we can represent a chess game by a 64-bit integer. As for what information to store, how to use, the next one.

Gobang AI algorithm-zobrist

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.