3

我目前正在为国际象棋编写一个带有 alpha beta 修剪的 minimax 算法。

从我看到的所有示例中,极小极大算法将返回一个 int 值,该值表示最佳得分或最佳棋局将产生的棋盘状态。

我的问题是我们如何返回与得分返回值相关的最佳移动?

例如,我的字母表()在下面的伪...

public int alphabeta(int depth, Board b, int alpha, int beta, boolean maxPlayer) {
    if(depth == 0)
        return evaluateBoard(b);
    if(maxPlayer) {
        for(each of max player's moves) {
            // make move on a tempBoard
            int eval = alphabeta(depth - 1, tempBoard, alpha, beta, false);
            alpha = Math.max(alpha, eval);
            if(beta <= alpha) 
                break;
        }
        return alpha;
    }
    else {
        for(each of min's moves) {
            // make move on a tempBoard
            int eval = alphabeta(depth - 1, tempBoard, alpha, beta, true);
            beta = Math.min(beta, eval);
            if(beta <= alpha)
                break; 
        }
        return beta;
    }
}

在我的 minimax/alphabeta 的实现中,我有一个 Board 对象,它代表棋盘,棋子可以在其上移动以代表不同的棋盘纹理/游戏状态。

我的函数evaluateBoard(Board b)接受一个板并计算参数板的板状态值。

本质上,evaluateBoard() 为我提供了作为最佳移动值的字母表的最终 int 结果值。但是,我看不到 evaluateBoard() 返回导致最终得分的移动的方法。即使我要返回一些包含分数值和片段信息的对象,我也不确定如何在树的顶部获得给我最终最佳分数的片段的信息。

有谁知道我如何访问/返回给出最佳分值的最佳动作的信息?我是否错过了 mini max 算法中的一个关键元素和/或我是否必须以不同的方式实现 alphabeta()?

编辑:

例如,假设 minimax 从以下移动中返回最佳分数:e4、e5、nf3、nc6。我所拥有的将返回棋盘情况的数值。我怎样才能返回“e4”?E4 是导致最高值的移动。

谢谢。

4

1 回答 1

2

极小极大算法通过探索可能移动的树来工作,即使您没有明确使用树。因此,除了它的值之外,您的函数所需要的只是返回最佳移动。

你可以这样做:

ScoredMove alphabeta(Board board, String player, Move move) {
  board.applyMove(move);
  if (board.gameOver())
  {
    score = board.scoreForPlayer(player);
    return ScoredMove(score, move);
  }

  if (player == "player1") {
    next_player = "player2";
  } else {
    next_player = "player1";
  }

  ScoredMove best_move = null;
  for (next_move in board.movesForPlayer(next_player)) {
    ScoredMove scored = alphabeta(board, next_player, next_move)
    if (best_move == null || best_move.score < scored.score) {
      best_move = scored;
    }
  }
  board.removeMove(move);
  return best_move;
}
于 2014-07-19T03:55:56.563 回答