【问题标题】:Minimax algorithm for Othello not working correctly奥赛罗的 Minimax 算法无法正常工作
【发布时间】:2013-11-25 15:33:53
【问题描述】:
public class OthelloJPlayer extends OthelloPlayer {
  @Override
  public OthelloMove getMove(OthelloState state) {
    int bestchoice = 0;
    int bestscore = Integer.MIN_VALUE;
    boolean maximizingPlayer = true;

    // generate the list of moves:
    List<OthelloMove> moves = state.generateMoves();

    if (moves.isEmpty()) {
      // If there are no possible moves, just return "pass":
      return null;
    } else {
      // turn moves to states
      List<OthelloState> states = new ArrayList<OthelloState>();

      for (int i = 0; i < moves.size(); i++) {
        states.add(state.applyMoveCloning(moves.get(i)));
      }

      for (int i = 0; i < states.size(); i++) {
        // uses minmax to determine best move.
        int score = (MinMax(3, states.get(i), maximizingPlayer));

        if (score > bestscore) {
          bestscore = score;
          bestchoice = i;

        }
      }
    }

    return moves.get(bestchoice);
  }

  // min max algorithm
  public int MinMax(int depth, OthelloState game_board, boolean maximizingPlayer) {
    List<OthelloMove> moves;

    if (depth == 0) {
      int score = game_board.score();

      return score;
    }

    if (maximizingPlayer) {
      int bestvalue = Integer.MIN_VALUE;
      // gets other players moves
      moves = game_board.generateMoves(1);

      if (moves.isEmpty()) {
        int score = game_board.score();

        return score;

      } else {
        for (int i = 0; i < moves.size(); i++) {
          OthelloState new_game_board = new OthelloState(8);
          new_game_board = game_board.applyMoveCloning(moves.get(i));

          int returned_score = MinMax(depth - 1, new_game_board, false);
          bestvalue = max(bestvalue, returned_score);
        }
      }
      return bestvalue;
    } else {
      int bestvalue = Integer.MAX_VALUE;
      // gets your moves
      moves = game_board.generateMoves(0);

      if (moves.isEmpty()) {
        int score = game_board.score();

        return score;
      } else {
        for (int i = 0; i < moves.size(); i++) {
          OthelloState new_game_board = new OthelloState(8);
          new_game_board = game_board.applyMoveCloning(moves.get(i));

          int returned_score = MinMax(depth - 1, new_game_board, true);
          bestvalue = min(bestvalue, returned_score);
        }
      }

      return bestvalue;
    }
  }
}

我的极小极大算法似乎没有返回最佳移动。当我使用 minimax 代理的代理与执行随机移动的代理对抗时,它有时会失败。从我的感知来看,一切看起来都不错,有人可以检查我的逻辑,我一定遗漏了一些东西。启发式是分数。正分意味着你赢了 负分意味着其他玩家赢了。

【问题讨论】:

  • 另请参阅此问题:stackoverflow.com/questions/9511814/…
  • 在一个分支因子与这个一样高的游戏中,您通常只能在树中探索几个关卡。因此,可以做出次优的移动。 Minimax(有或没有修剪)绝不可能避免这种情况。唯一能保证最优的移动是那些在树的底部“可见”时的移动,通常是在游戏结束时。

标签: java minimax


【解决方案1】:

你有很多问题。

  1. 您的getMove 方法实际上是搜索的根,它是一个最大节点。因此,它应该使用maximizingPlayer = false 调用MinMax

  2. 当您调用MinMax 时,您需要交换玩家。现在,您只需从 max -> max -> min -> min -> min... 开始,因为您使用 truefalse 常量。将您的调用(对于最小和最大情况)更改为 MinMax(depth - 1, new_game_board, !maximizingPlayer)

  3. 确保game_board.score() 从最大玩家的角度进行评估。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多