MiniMax chess algoritm返回不良动作

问题描述 投票:0回答:1

我在为国际象棋游戏实施MiniMax算法时遇到了问题。它的大多数部分似乎都有效,但它要么从未做出好的动作,要么在评估(基于两个玩家活动部分的得分)中出现问题。例如,如果我设置了检查(例如傻瓜的配偶),ai会做一些随机而不是杀死国王的事情。我真的不能说出我做错了什么。

评估电路板的类StandardBoardEvaluator似乎在经过一些测试后工作,因此问题很可能出现在MiniMax实现中。游戏由一个类板组成,它具有2D阵列和我自己的Square类的8x8对象,它本身具有对Piece的引用(可以是null,或者任何典型的棋子)。在算法中,我不断地将新的Board实例作为搜索树的一部分,这就是我在Board和Square中制作这些“深度克隆”构造函数的原因,所以这似乎不是问题所在。像这样:

public Board(Board originalBoard) {
        this.turnIsWhite = originalBoard.getTurnIsWhite();
        winner = null;
        squares = new Square[8][8];

        for (int rank=0; rank<squares.length; rank++) {
            for(int file=0; file<squares[rank].length; file++) {
                squares[rank][file] = new Square(originalBoard.getSquare(posStringFromFileRank(rank, file)));
            }
        }
    }

public Square(Square originalSquare) {
        this.pos = new String(originalSquare.getPos());
        this.piece = originalSquare.getPiece();
    }

我有一个典型的命令类MovePiece,用于移动棋子。这使用另一个类MoveCheck来检查移动命令是否合法。 MovePiece返回一个布尔值,表示移动是否合法。这两个类都经过严格测试并且正在运行,因此我认为问题不在这些类中。

这是算法:

public class MiniMax implements MoveStrategy{
    BoardEveluator bV;
    MoveGenerator mGen;
    int depth;

    public MiniMax(int depth){
        bV = new StandardBoardEvaluator();
        mGen = new MoveGenerator();
        this.depth = depth;
    }

    @Override
    public MovePiece execute(Board board) {
        MovePiece bestMove = null;
        int lowestValue = Integer.MAX_VALUE;
        int highestValue = Integer.MIN_VALUE;
        int currentValue = 0;

        String color = (board.getTurnIsWhite() ? "white" : "black");
        System.out.println(color + " is evaluation best move with MiniMax depth " + depth);
        List<MovePiece> allPossibleMoves = mGen.getLegalMoves(board, board.getTurnIsWhite());

        for (MovePiece mp : allPossibleMoves){
            Board tempBoard = new Board(board);
            mp.setBoard(tempBoard);
            if (mp.execute()){
                currentValue = tempBoard.getTurnIsWhite() ? min(tempBoard, depth -1) : max(tempBoard, depth -1);
                if (board.getTurnIsWhite() && currentValue >= highestValue){
                    highestValue = currentValue;
                    bestMove = mp;
                }
                else if (!board.getTurnIsWhite() && currentValue <= lowestValue){
                    lowestValue = currentValue;
                    bestMove = mp;
                }
                mp.unexecute();
            }
        }
        return bestMove;
    }



    int min (Board board, int depth){
        if (depth == 0 || board.getWinner() != null){
            return bV.eveluate(board);
        }
        int lowestValue = Integer.MAX_VALUE;
        List<MovePiece> legalMoves = mGen.getLegalMoves(board, board.getTurnIsWhite());
        for (MovePiece mp : legalMoves){
            Board tempBoard = new Board(board);
            mp.setBoard(tempBoard);
            if (mp.execute()){
                int currentValue = max(tempBoard, depth - 1);
                if (currentValue <= lowestValue){
                    lowestValue = currentValue;
                }
                mp.unexecute();
            }

        }
        return lowestValue;
    }
    int max (Board board, int depth){
        if (depth == 0 || board.getWinner() != null){
            return bV.eveluate(board);
        }
        int highestValue = Integer.MIN_VALUE;
        List<MovePiece> legalMoves = mGen.getLegalMoves(board, board.getTurnIsWhite());
        for (MovePiece mp : legalMoves){
            Board tempBoard = new Board(board);
            mp.setBoard(tempBoard);
            if (mp.execute()){
                int currentValue = min(tempBoard, depth - 1);
                if (currentValue >= highestValue){
                    highestValue = currentValue;
                }
                mp.unexecute();
            }
        }
        return highestValue;
    }

和评估者类

public class StandardBoardEvaluator implements BoardEveluator {
    private int scorePlayer(Board board, boolean isWhite){
        return pieceValue(board, isWhite) + mobolity(isWhite, board);
    }
    private int mobolity(boolean isWhite, Board board){
        return (int) (board.getActiveSquares(isWhite).size() * 1.5);
    }
    private static int pieceValue(Board board, boolean isWhite){
        int piceValueScore = 0;
        for (Square square : board.getActiveSquares(isWhite)){
            piceValueScore += square.getPiece().getPieceValue();
        }
        return piceValueScore;
    }
    @Override
    public int eveluate(Board board) {
        return scorePlayer(board, true) - scorePlayer(board, false);
    }
}

这是MovePiece类:

private Square from;
    private Square to;
    private Board board;
    private MoveCheck mCheck;
    private RulesCheck rCheck;
    private boolean done = false;
    private Piece killed;

    public MovePiece(Board board, String from, String to) {
        this.board = board;
        this.from = board.getSquare(from);
        this.to = board.getSquare(to);
        mCheck = new MoveCheck();
    }
    public MovePiece(Board board, Square from, Square to) {
        this.board = board;
        this.from = from;
        this.to = to;
        mCheck = new MoveCheck();
        rCheck = new RulesCheck(board);
    }
    public void setBoard(Board board) {
        this.board = board;
    }
    public Board getBoard() {
        return board;
    }

    public Square getFrom() {
        return from;
    }
    public Square getTo() {
        return to;
    }
    public void setFrom(Square from) {
        this.from = from;
    }
    public void setTo(Square to) {
        this.to = to;
    }
    public void setFrom(String from) {
        this.from = board.getSquare(from);
    }
    public void setTo(String to) {
        this.to = board.getSquare(to);
    }
    @Override
    public boolean execute() {
        rCheck = new RulesCheck(board);
        if (done) {
            board.movePiece(from, to);
            return true;
        }
        else if (mCheck.isLegal(board, from, to)){
            if (to.getPiece() != null) {
                killed = to.getPiece();
                rCheck.winCheck(killed);
            }
            board.setGameOutput("Moved " + from.pieceToString() + " at " + from.getPos() + " - to " + to.getPos() + "(" + to.pieceToString() + ")");
            board.movePiece(from, to);
            rCheck.checkPromotion(to);
            done = true;
            return true;
        }
        return false;
    }

    @Override
    public void unexecute() {
        if (to.getPiece().getClass() == Pawn.class)
            ((Pawn) to.getPiece()).decreaseMoves();
        board.movePiece(to, from);
        if (killed != null) {
            to.setPiece(killed);
        }

    }

MoveCheck类只是查看移动是否合法(路径是明确的,目标是敌人还是空的等等),不要认为它与我的问题相关,因为代码经过测试并且有效。

片段值在抽象类Piece的子类(所有类型的片段)中声明为int。典当100分,主教和骑士300分,白车500分,女王900分,国王10万分。

如果有人能帮我解决问题,我将永远感激不尽!如果你需要一些我没有展示的其他代码,请告诉我。

java chess minimax
1个回答
0
投票

你没有分享MovePiece实现既不是主游戏循环,但我在MiniMax.execute方法中识别出两个可能的问题:

currentValue = tempBoard.getTurnIsWhite() ? min(tempBoard, depth -1) : max(tempBoard, depth -1)

根据上面的代码,您假设MinMax播放器将始终为黑色,因为它评估min为白色,max为黑色。对于通用算法,这是一个错误的假设,但不知道它是否适合您。

第二件事是在调用mp.execute()并指定bestMove = mp后调用mp.unexecute(),因此有效地调用bestMove.unexecute(),因为变量指向同一个对象。

请考虑以上建议,如果不能解决问题,请分享上述实施部分。

© www.soinside.com 2019 - 2024. All rights reserved.