首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Othello不正确工作的Minimax算法

Othello不正确工作的Minimax算法
EN

Stack Overflow用户
提问于 2013-11-25 15:33:53
回答 1查看 2.3K关注 0票数 0
代码语言:javascript
复制
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代理的代理与执行随机移动的代理交互时,它有时会松开。从我的洞察力来看,一切看起来都还好,有人能帮我检查一下我的逻辑吗?我一定是遗漏了什么。启发式就是得分。一个积极的分数意味着你赢了一个负数意味着另一个球员赢了。

EN

回答 1

Stack Overflow用户

发布于 2013-11-25 15:41:42

你有很多问题。

  1. 您的getMove方法实际上是搜索的根,它是一个最大节点。因此,它应该使用MinMax调用maximizingPlayer = false
  2. 当您调用MinMax时,您需要交换播放器。现在,你只要从最大-> max -> min -> min -> min.因为您使用truefalse常量。将您的调用(对于最小和最大的情况)更改为MinMax(depth - 1, new_game_board, !maximizingPlayer)
  3. 确保game_board.score()从最大播放器的角度进行评估。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20196935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档