1

I am writing a tic-tac-toe game in javascript. I done with the GUI, ect, but I still have a problem with the AI. I use the Alpha-beta-Prune to find the win move. However, my code never give the move that can win a game. I did a lot of research but still got no idea what is wrong with my code. Can you guys please have a look at my main AI part here. My idea is create a 2D-array that store the move: 1 is human, -1 is AI, and 0 is empty. Init call: var test = getBestMove(-1); `

Get the best move function:

function getBestMove(player)  {

    var alpha    = -maxValue;
    var beta     =  maxValue;

    var bestScore = -maxValue;
    var bestMove  = -1;

    for (var r = 0; r < boardSize; ++r)
    {
        for (var c = 0; c < boardSize; ++c)
        {
            if (gameArray[r][c] === 0)
            {
                gameArray[r][c] = player;
                var score = alphaBeta(gameArray, alpha,beta, r * boardSize + c, player);
                gameArray[r][c] = 0;

                if (score > bestScore)
                {
                    bestScore = score;
                    bestMove = r * boardSize + c;
                }
            }
        }
    }
    console.log(bestScore);
    return bestMove;
}

alpha-beta search:

// 1: human
// -1: AI
// 0: empty
function alphaBeta(board, alpha, beta,lastMove, player) {    
    var bestValue;
    //check if the last move make a win
    //checkwin return the winner / 0 if no winner
    var win = checkwin(board, 
                   Math.floor(lastMove / boardSize), 
                   lastMove % boardSize);

    if (win === - 1){
        return 1000;
    }
    else if (win === 1){
        return -1000;
    }
    else if (checktie(board) === true){
        //console.log(win);
        return 0;
    }
    else{
        //return evaluation(board);
    }
    if (player === -1) 
    {
        bestValue = alpha;
        // Recurse for all children of node.
        for (var r = 0; r < boardSize; r++)
        {
            for (var c = 0; c < boardSize; c++)
            {
                if (board[r][c] === 0)
                {            
                    board[r][c] = player;
                    var childValue = alphaBeta(board, bestValue, beta,r*boardSize+c, -player);
                    board[r][c] = 0;
                    bestValue = Math.max(bestValue, childValue);
                    if (beta <= bestValue) 
                    {
                        return bestValue;
                    }
                }
            }
        }
    }
    else {
        bestValue = beta;       
        // Recurse for all children of node.
        for (var r = 0; r < boardSize; r++)
        {
            for (var c = 0; c < boardSize; c++)
            {
                if (board[r][c] === 0)
                {
                    board[r][c] = player;
                    var childValue = alphaBeta(board, alpha, bestValue,r*boardSize+c, -player);
                    board[r][c] = 0;
                    bestValue = Math.min(bestValue, childValue);
                    if (bestValue <= alpha)
                    {
                        return bestValue;
                    }
                }
            }
        }        
    }
    return bestValue;
}

I did not use the evaluation function, since I think that the alpha-beta should be able to found the win without it.

Pang
  • 9,564
  • 146
  • 81
  • 122
Rock Rach
  • 39
  • 4

1 Answers1

1

I figure out the problem, it is not about the algorithm but about my implementation: Since the AI always make move after human so if (player === -1) bestValue = beta; and if (player === 1) bestValue = alpha;

Second mistake, the child move is of other player so: board[r][c] = -player; in everywhere

Here is the complete version of the game: www.neokites.com/gomoku/ I hope this may help someone.

Rock Rach
  • 39
  • 4