Gobang AI algorithm-refactoring code

Source: Internet
Author: User

Why refactoring is needed

The previous code has a lot of loose modules grouped together. When integrating the Zobrist, it will be found that all operations that need to be moves need to be Zobrist or operated at once. In addition, logically, many modules can be merged into the same class, so this time the code is a large refactoring. So it's normal to find that some of the modules that the blog says are missing, because most of the modules are moved to the Board class.

The main task of this refactoring is to divide the AI-related code into four modules:

    1. Board, all operations related to checkers are here, including scoring, judging, winning, Zobrist caching, heuristic functions, and so on.
    2. Negamax Search Module
    3. Checkmate Count Kill module
    4. Enclosures, configurations, some ancillary methods, etc., including ai.js , role.js etc.
Integrated Zobrist

After all the moves operations are placed in the Board class, you only need to zobrist the XOR operation in Board. You can avoid the Zobrist operation in search or Count kill.

The Zobrist update is done in the following three ways

1 // Son2Board.prototype.put =function(p, role, record) {3  This. board[p[0]][p[1]] =role;4  This. Zobrist.go (P[0], p[1], role);5  This. Updatescore (p);6 if(record) This. Steps.push (p);7 }8 9 //Removing a pawnTenBoard.prototype.remove =function(p) { One varR = This. board[p[0]][p[1]]; A  This. Zobrist.go (P[0], p[1], R); -  This. board[p[0]][p[1]] =R.empty; -  This. Updatescore (p); the } -  - //undo -Board.prototype.back =function() { + if( This. steps.length < 2)return; - vars = This. Steps.pop (); +  This. Zobrist.go (S[0], s[1], This. board[s[0]][s[1]]); A  This. board[s[0]][s[1]] =R.empty; at  This. Updatescore (s); - vars = This. Steps.pop (); -  This. Zobrist.go (S[0], s[1], This. board[s[0]][s[1]]); -  This. board[s[0]][s[1]] =R.empty; -  This. Updatescore (s); -}

so any time after completing the moves, you can pass   board.zobrist.code to get the current hash value. Negamax checkmate You can then store the score with this code as a unique marker for the current game.

At present, the simplest way to do this is to save the scores of each game, and the next time you meet the same game, you don't have to score, but you take the last stored scores directly. But storing the score here is the same as storing the depth, because only fractions with a depth greater than or equal to the current depth are valid. A concrete implementation can look at the code in the Board class.

The actual effect is not ideal, about 10% of the hit rate, although 10% is also very objective to promote, but in fact, the final moves speed effect is not big, that is, add this way of the replacement table and not feel the speed of ascension.
In fact, the best application of the replacement table should be used in the heuristic search for the selection of nodes to be filtered and sorted, but specifically how to achieve the temporary or not think well.

Gobang AI algorithm-refactoring code

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.