用于奥赛罗的Minimax算法无法正常工作

时间:2013-11-25 15:33:53

标签: java minimax

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算法似乎没有返回最佳移动。当我使用minimax代理的代理对代理执行随机移动时,它会丢失。从我的感知一切看起来没关系,有人请检查我的逻辑我必须遗漏一些东西。启发式是得分。正分表示您赢得负分值意味着其他玩家获胜。

1 个答案:

答案 0 :(得分:0)

你有很多问题。

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

  2. 当您调用MinMax时,您需要交换玩家。现在,你只需要从max - &gt;最大 - &gt; min - &gt; min - &gt; min ...因为你使用truefalse常量。将您的调用(针对最小和最大案例)更改为MinMax(depth - 1, new_game_board, !maximizingPlayer)

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