Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reversi Game AI Implementation

Reversi Game AI Implementation

AI Midterm Project, our group implement minimax with alpha-beta pruning and Monte Carlo Tree Search these to algorithms

Yukai

May 03, 2016
Tweet

More Decks by Yukai

Other Decks in Programming

Transcript

  1. src/ !"" DrawBoard.js !"" Game.js !"" ReversiBoard.js !"" assets #

    !"" images # !"" index.html # !"" minimax.html # $"" minimax.js !"" player # !"" BasePlayer.js # !"" HumanPlay.js # !"" MctsPlayer.js # !"" RandomPlay.js # !"" index.js # $"" mcts !"" stylesheets # $"" style.css $"" utils $"" index.js
  2. export default class RandomPlay extends BasePlayer { getMove(board) { var

    validPositions = board.getValidPositions(); return( validPositions[ this.randomPick(0, validPositions.length-1) ]); } randomPick(min, max) { return Math.floor(Math.random()*(max-min+1)+min); } } Random
  3. monteCarloTreeSearch(board) { var rootNode = new Node(board); var simCount =

    0 var begin = new Date().getTime(); while (new Date().getTime() - begin < 3000) { var pickedNode = this.treePolicy(rootNode); var result = this.simulate(pickedNode.gameState); this.backPropogation(pickedNode, result); simCount += 1; } return this.bestAction(rootNode) } MCTS
  4. bestChild(node) { var C = 1; // 'exploration' value var

    max = { node: null, value: Number.NEGATIVE_INFINITY }; for (var child in node.children) { var { wins, plays } = node.children[child].getWinsPlays(); var { plays: parentPlays } = node.getWinsPlays(); var value = (wins / plays) + C * Math.sqrt(2 * Math.log(parentPlays) / plays); if ( value > max.value ) { max.node = node.children[child]; max.value = value; } } return max.node; } MCTS (cont.)
  5. Minimax var boardData = [ _, _, _, _, _,

    _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, O, X, _, _, _, _, _, _, X, O, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, ];
  6. var scoreBoard = [ 90, -60,10,10,10,10,-60, 90, -60, -80, 5,

    5, 5, 5,-80,-60, 10, 5, 1, 1, 1, 1, 5, 10, 10, 5, 1, 1, 1, 1, 5, 10, 10, 5, 1, 1, 1, 1, 5, 10, 10, 5, 1, 1, 1, 1, 5, 10, -60, -80, 5, 5, 5, 5,-80,-60, 90, -60,10,10,10,10,-60, 90 ]; Minimax (cont.)
  7. Minimax (cont.) function evaluate(color, board){ ... for(var i = 11;

    i < 89; i++){ if(board[i] == color){ boardS += scoreBoard[i-11]; } } count_score = m_weight*mobility + b_weight*boardS; return count_score; }
  8. Minimax (cont.) function minmax(turn, depth, alpha, beta, board){ ... if(!turn){

    var value=-9999; for(i = 11; i < 89; i++){ if(isLegal(1,2,i)){ touchMove(1,2,i,c_board); score=minmax(true,depth-1,alpha,beta,c_board); if(score>value){ value=score; } alpha=Math.max(alpha,score); if(alpha>=beta){ break; } } } } ...
  9. if(turn){ var value=9999 for(i=11;i<89;i++){ if(isLegal(2,1,i)){ touchMove(2,1,i,c_board); score = minmax(false,depth-1,alpha,beta,c_board); if(score

    < value){ value=score; } alpha=Math.min(beta,score); if(alpha<=beta){ break; } } } } return value; } Minimax (cont.)
  10. function moveWithMinmax(){ var value=-9999; var score; var pos; for(var i=11;i<89;i++){

    if(isLegal(1,2,i)){ score=minmax(false,depth,-9999,9999,testBoard); if(score>value){ value=score; pos=i; } } } return pos; } Minimax (cont.)