How to get actual move rather than move value from mini max algorithm

纵饮孤独 提交于 2019-12-10 10:39:52

问题


I am currently writing a minimax algorithm with alpha beta pruning for Chess.

From all the examples I have seen, minimax algorithm will return an int value that represents that best score or board state that will result from the best move.

My question is how can we return the best move that is associated with the score return value?

For example, my alphabeta() in pseudo below ...

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;
    }
}

In my implementation of minimax / alphabeta, I have a Board object that represents the chess board and pieces can move on it to represent different board textures / game states.

My function evaluateBoard(Board b) takes in a Board and calculates the value for the board state of the parameter Board.

Essentially evaluateBoard() gives me the final int result value of alphabeta() for the value of the best move. However I don't see a way for evaluateBoard() to return the move that resulted the final score. Even if I were to return some Object holding the score value and information of pieces, I am unsure of how I could get information of the piece at the top of the tree that gave me the final best score.

Does anyone know how I can access/return information of the best move that gives the best score value? Am I missing a key element in mini max algorithm and / or do I have to implement alphabeta() differently?

EDIT:

For example, let's say minimax returns the best score from the following moves : e4, e5, nf3, nc6. What I have will return the numerical value of the board situation. How can I return "e4" ? E4 is the move that results in the highest value.

Thanks.


回答1:


The minimax algorithm works by exploring the tree of possible moves, even if you don't explicitly use a tree. So all that is needed is for your function to return the best move in addition to its value.

You can do something like this:

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;
}


来源:https://stackoverflow.com/questions/24836499/how-to-get-actual-move-rather-than-move-value-from-mini-max-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!