【问题标题】:Tic Tac Toe alpha-beta MiniMaxfor 4x4 board (JavaScript)Tic Tac Toe alpha-beta MiniMaxfor 4x4 board (JavaScript)
【发布时间】:2018-06-07 18:40:41
【问题描述】:

我已经实现了应用 alpha-beta minimax 的 JavaScript。它适用于 3x3 板,但当我将板更改为 4x4 或更高时,程序似乎挂起。

更新:当可用步数超过 10 时,程序效率不高

这是 alpha-beta minimax 函数:

function AlphaBetaMinMax(game_board, depth, alpha, beta) {
    if (CheckForWinner(game_board) === 1 || CheckForWinner(game_board) === 2 ||
        CheckForWinner(game_board) === 3)
        return GameScore(game_board, depth);

    depth += 1;

    var availableMoves = GetAvailableMoves(game_board);

    var move, result, possible_game;
    if (active_turn === "COM") {

        for (var i = 0; i < availableMoves.length; i++) {
            move = availableMoves[i];
            possible_game = GetNewState(move, game_board);
            result = AlphaBetaMinMax(possible_game, depth, alpha, beta);
            game_board = UndoMove(game_board, move);
            if (result > alpha) {
                alpha = result;
                if (depth == 1)
                    choice = move;
            } else if (alpha >= beta) {
                return alpha;
            }
        }
        return alpha;
    } else {
        for (var i = 0; i < availableMoves.length; i++) {
            move = availableMoves[i];
            possible_game = GetNewState(move, game_board);
            result = AlphaBetaMinMax(possible_game, depth, alpha, beta);
            game_board = UndoMove(game_board, move);
            if (result < beta) {
                beta = result;
                if (depth == 1)
                    choice = move;
            } else if (beta <= alpha) {
                return beta;
            }
        }
        return beta;
    }
}

function GameScore(game_board, depth) {
    var score = CheckForWinner(game_board);
    var t = (board_size + 1);
    if (score === 1)
        return 0;
    else if (score === 2)
        return depth - t;
    else if (score === 3)
        return t - depth;
}

function UndoMove(game_board, move) {
    game_board[move] = UNOCCUPIED;
    ChangeTurn();
    return game_board;
}

function GetNewState(move, game_board) {
    var piece = ChangeTurn();
    game_board[move] = piece;
    return game_board;
}

function ChangeTurn() {
    var piece;
    if (active_turn === "COM") {
        piece = 'X';
        active_turn = "PLAYER";
    } else {
        piece = 'O';
        active_turn = "COM";
    }
    return piece;
}

function GetAvailableMoves(game_board) {
    var AvailableMoves = new Array();
    for (var i = 0; i < board_size; i++) {
        if (game_board[i] === UNOCCUPIED) {
            AvailableMoves.push(i);
        }
    }
    return AvailableMoves;
}

CheckForWinner() 返回:

  • 0 表示没有平局或没有获胜
  • 1 个领带
  • 2 代表玩家获胜
  • 3 for Computer win

感谢您的帮助

【问题讨论】:

  • 让我们尝试将 active_turn 传递给 minimax 函数

标签: javascript minimax alpha-beta-pruning


【解决方案1】:

我已经成功构建了一个名为 Othello 的游戏(它几乎就像 GO 游戏),它与您正在做的事情相同,但我的主要语言是 Java(并且您正在使用 JavaScript)。所以,我会为你展示我的pseudocode(因为我不懂 JavaScript)。我希望它可以在这种情况下对您有所帮助:

function minimax(turn, max_depth ,depth, alpha, beta):
    if isFinishState() or depth==max_depth :
       return value of the node

    if turn == "computer" :
        best = -INFINITY 
        turn = "Human"
        for each move in available moves :
           possible_game = GetNewState(move, game_board); 
           value = minimax(turn, depth+1,max_depth, alpha, beta)
           //insert some code to undo if you have changed the game board
           best = max( best, value) 
           alpha = max( alpha, best)
           if beta <= alpha:
              break
         return best
    else : //human turn
        best = +INFINITY 
        turn = "Computer"
        for each move in available_moves:
           possible_game = GetNewState(move, game_board);
           value = minimax(turn, depth+1, max_depth, alpha, beta)
           //insert some code to undo if you have changed the game board
           best = min( best, value) 
           beta = min( beta, best)
           if beta <= alpha:
              break
        return best

然后,您需要像findBestMove() 这样的函数来返回computer 的最佳下一个位置:

  int findBestMove(int max_depth) :
    alpha = -9999999; //-INFINITY 
    beta  = 9999999; //+INFINITY 
    choice = null;
    best =-9999999;//-INFINITY 
   for each move in available_moves:

                possible_game = GetNewState(move, game_board);
                moveVal = minimax("computer", 0, max_depth, alpha, beta);
                if (best < moveVal):
                    best = moveVal;
                    choice = move;

                //insert code here to undo game_board
    return choice;

【讨论】:

  • 感谢您的回复,我已经在 J​​S 中实现了您的minimax(),但在findBestMove() 中,函数 alpha 应该是 Infinity 或 -无穷大 ?还有GetNewState 应该生成 Computer State 还是 Human State ?我都试过了,但它返回了错误的动作。
  • 谢谢,我已经在findBestMove() 编辑了我的答案。要不要参考我的java代码。
  • 非常感谢,我想出了 best = +Infinity;possible_game = Get_New_State(move,game_board,"PLAYER");if (best &gt; moveVal) :它在不到 1 分钟的时间内找到了 4x4 棋盘的第一步。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多