如何获得实际的举措,而不是从小型最大算法移动值 [英] How to get actual move rather than move value from mini max algorithm

查看:196
本文介绍了如何获得实际的举措,而不是从小型最大算法移动值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个极大极小算法α+β修剪的国际象棋。

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

这是我所见过的例子,极大极小算法将返回重新presents的最好成绩或董事会的状态,这将导致从最好的移动一个int值。

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?

例如,我的字母a()以下伪...

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

在我的实现极大极小/字母a的,我有重新presents国际象棋棋盘和棋子可以将其移动到重新present不同板的纹理/游戏状态一局的对象。

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.

我的功能 evaluateBoard(板B)接受一个董事会,并计算参数董事会董事会状态值。

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

从本质上讲evaluateBoard()给了我字母a的最终诠释结果值()的最佳举措的价值。不过,我没有看到一个方式evaluateBoard()返回,导致最后得分的举动。即使我返回一些物体保持件的分值和信息,我不确定如何我能得到的资料片,在这给了我最后的最好成绩了树的顶端。

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.

有谁知道我可以访问/回报的最好举动提供了最好的得分值的信息? 我失去了在小型最大算法的关键要素和/或做我必须执行字母a()不同?

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?

编辑:

例如,假设极小返回的最好成绩从以下动作: E4,E5,NF 3,NC6。我有将回到董事会情况的数值。我怎样才能返回E4? E4是导致最高值的举动。

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.

感谢。

推荐答案

极小极大算法通过探索可能的行动树,即使你没有明确地使用一棵树。因此,所有的需要的是你的函数返回的最佳举措,除了它的价值。

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.

您可以做这样的事情:

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

这篇关于如何获得实际的举措,而不是从小型最大算法移动值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆