Java minmax算法返回空移动,并且无法正确地撤消移动

这里是我的minmax算法的代码:

private static void minmax(){
    Move move = max(4, true, null);
    //System.out.println(move);
    board.makeMove(move.getFromPoint(), move.getToPoint());
}

private static Move max(int depth, boolean player, Move passedMove){
    if(depth == 0) return passedMove.setScore(board.boardVal());
    Move max = new Move(Integer.MIN_VALUE);
    for(int i = 0; i < board.getMoves(player).size(); i++){
        Move move = board.getMoves(player).get(i);
        board.makeMove(move.getFromPoint(), move.getToPoint());
        //System.out.println(board);
        Move scoreMove = min(depth - 1, !player, move);
        board.undo();
        if(scoreMove.getScore() > max.getScore()) max = new Move(move.getFromPoint(), move.getToPoint(), scoreMove.getScore());
    }
    return max;
}

private static Move min(int depth, boolean player, Move passedMove){
    if(depth == 0) return passedMove.setScore(-board.boardVal());
    Move min = new Move(Integer.MAX_VALUE);
    for(int i = 0; i < board.getMoves(player).size(); i++){
        Move move = board.getMoves(player).get(i);
        board.makeMove(move.getFromPoint(), move.getToPoint());
        //System.out.println(board);
        Move scoreMove = max(depth - 1, !player, move);
        board.undo();
        if(scoreMove.getScore() < min.getScore()) min = new Move(move.getFromPoint(), move.getToPoint(), scoreMove.getScore());
    }
    return min;
}

移动是一个对象,用于存储从位置,到位置和与移动后的棋盘值相关的分数。 Move move = max(4, true, null); 将一个空值赋给“移动”(我不明白为什么),并且在这些方法运行后,它使板子处于一种奇怪的状态,似乎这些移动没有正确地被撤消,但我已经反复检查过这个功能。

这是一个运行代码的例子(在注释掉board.makeMove(move.getFromPoint(), move.getToPoint());行以防止空指针异常之后):

8 [R] [N] [B] [Q] [K] [B] [N] [R] 
7 [P] [P] [P] [P] [P] [P] [P] [P] 
6 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
5 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
4 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
3 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
2 [P] [P] [P] [P] [P] [P] [P] [P] 
1 [R] [N] [B] [Q] [K] [B] [N] [R] 
   a   b   c   d   e   f   g   h

a2,a4
8 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
7 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
6 [ ] [ ] [ ] [ ] [ ] [ ] [N] [ ] 
5 [ ] [ ] [N] [ ] [ ] [ ] [R] [K] 
4 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
3 [ ] [ ] [ ] [ ] [ ] [ ] [B] [B] 
2 [ ] [P] [ ] [ ] [ ] [ ] [ ] [ ] 
1 [P] [ ] [R] [P] [P] [P] [P] [P] 
   a   b   c   d   e   f   g   h

它只留下计算机的棋子,移除所有玩家的棋子。

移动类:

public class Move {
    private Point fromPoint;
    private Point toPoint;
    private int score;

    public Move(Point fromPoint, Point toPoint, int score){
        this.fromPoint = fromPoint;
        this.toPoint = toPoint;
        this.score = score;
    }

    public Move(Point fromPoint, Point toPoint){
        this(fromPoint, toPoint, 0);
    }

    public Move(int score){
        this(null, null, score);
    }

    public Point getFromPoint() {
        return fromPoint;
    }

    public Point getToPoint() {
        return toPoint;
    }

    public int getScore() {
        return score;
    }

    public Move setScore(int score){
        this.score = score;
        return this;
    }

    @Override
    public String toString(){
        return "(" + fromPoint.y + ", " + fromPoint.x + ")" + " (" + toPoint.y + ", " + toPoint.x + ") " + score;
    }
}

Board类建立在二维数组的堆栈上,这里是移动和撤消方法:

public void makeMove(Point fromPoint, Point toPoint){
    Piece[][] boardNode = new Piece[board.peek().length][];
    for(int i = 0; i < boardNode.length; i++){ //this was changed
        boardNode[i] = board.peek()[i].clone();
    }
    boardNode[fromPoint.y][fromPoint.x].setPoint(toPoint);
    boardNode[toPoint.y][toPoint.x] = boardNode[fromPoint.y][fromPoint.x];
    boardNode[fromPoint.y][fromPoint.x] = null;
    board.push(boardNode);
}

public void undo(){
    board.pop();
}

public ArrayList<Move> getMoves(boolean color){
    ArrayList<Move> moves = new ArrayList<>();
    for(Piece[] row : board.peek()){
        for(Piece piece : row){
            if(piece != null) {
                for (Point point : piece.moves()) {
                    if (piece.getColor() == color && straightLine(piece.getPoint(), point) && (board.peek()[point.y][point.x] == null || board.peek()[point.y][point.x].getColor() != color)) moves.add(new Move(piece.getPoint(), point));
                }
            }
        }
    }
    return moves;
}

我希望程序能够返回一个最佳移动(导致棋盘状态得分最高的移动)并离开棋盘,就像在minmax方法执行之前一样。 这两种情况都没有发生,该方法返回null,并且板完全更改。

在Board类中更改makeMove()方法以正确复制板后,我现在正在使用该方法中的NullPointerExceptions:

8 [R] [N] [B] [Q] [K] [B] [N] [R] 
7 [P] [P] [P] [P] [P] [P] [P] [P] 
6 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
5 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
4 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
3 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 
2 [P] [P] [P] [P] [P] [P] [P] [P] 
1 [R] [N] [B] [Q] [K] [B] [N] [R] 
   a   b   c   d   e   f   g   h

a2,a4
Exception in thread "main" java.lang.NullPointerException
    at Board.makeMove(Board.java:45)
    at ChessMain.min(ChessMain.java:87)
    at ChessMain.max(ChessMain.java:75)
    at ChessMain.min(ChessMain.java:89)
    at ChessMain.max(ChessMain.java:75)
    at ChessMain.minmax(ChessMain.java:63)
    at ChessMain.play(ChessMain.java:58)
    at ChessMain.main(ChessMain.java:15)

...

public abstract class Piece {
    private boolean color;
    private Point getPoint;

    public Piece(int x, int y, boolean color){
        this.color = color;
        getPoint = new Point(x, y);
    }

    abstract int getValue();

    public String toString(){
        return "";
    }

    public void setPoint(Point point){
        this.getPoint = point;
    }

    public Point getPoint(){ return getPoint; }

    public boolean takable(Point point){ return containsPoint(moves(), point); }

    private static boolean containsPoint(ArrayList<Point> list, Point point){
        for(Point listPoint: list){
            if(listPoint.x == point.x && listPoint.y == point.y) return  true;
        }
        return false;
    }

    abstract ArrayList<Point> moves();

    public boolean getColor(){ return color; }
}

下面是一个扩展Piece的对象的例子:

import java.util.ArrayList;

public class Bishop extends Piece{

    public Bishop(int x, int y, boolean color) {
        super(x, y, color);
    }

    public ArrayList<Point> moves(){
        ArrayList<Point> possibleMoves = new ArrayList<>();
        int x = 1;
        while(getPoint().x + x <= 7 && getPoint().y + x <= 7){
            possibleMoves.add(new Point(getPoint().x + x, getPoint().y + x));
            x++;
        }
        x = 1;
        while(getPoint().x - x >= 0 && getPoint().y + x <= 7){
            possibleMoves.add(new Point(getPoint().x - x, getPoint().y + x));
            x++;
        }
        x = 1;
        while(getPoint().x - x >= 0 && getPoint().y - x >= 0){
            possibleMoves.add(new Point(getPoint().x - x, getPoint().y - x));
            x++;
        }
        x = 1;
        while(getPoint().x + x <= 7 && getPoint().y - x >= 0){
            possibleMoves.add(new Point(getPoint().x + x, getPoint().y - x));
            x++;
        }
        return possibleMoves;
    }

    @Override
    public String toString(){ return "B"; }

    public int getValue(){ return 3;}
}

让我知道更多的信息会有所帮助。

任何帮助表示赞赏。


我可以看到一个问题:

public void makeMove(Point fromPoint, Point toPoint){
    Piece[][] boardNode = board.peek(); // WARNING!!!
    // Instead, make a copy of the previous board state
    // so  you don't change all board states when you want to change one.
    boardNode[fromPoint.y][fromPoint.x].setPoint(toPoint);
    boardNode[toPoint.y][toPoint.x] = boardNode[fromPoint.y][fromPoint.x];
    boardNode[fromPoint.y][fromPoint.x] = null;
    board.push(boardNode); // WARNING!!!
}

您的board在堆栈的每个级别使用相同的参考。

当你调用Piece[][] boardNode = board.peek(); ... board.push(boardNode); Piece[][] boardNode = board.peek(); ... board.push(boardNode); 你真的只是对所有的棋盘状态使用相同的参考,并修改其中的一个将修改所有这些参考的对象..我认为这不是你想要的。

这可能是您问题的根源,如果除此之外还有更多问题,请更新您的问题。


更新:

克隆棋盘状态的二维阵列仍然是个问题。 您克隆了数组,但不包括其中的Piece ,因此,当您更新该块的位置时,它会更新该板的所有状态所引用的对象。

首先,让Piece实现Cloneable

public abstract class Piece implements Cloneable {
    // ...
    @Override
    public abstract Piece clone();

然后让所有的小孩Pieces实际克隆自己,例如Pawn做到这一点:

@Override
public Pawn clone() {
    return new Pawn(getPoint.x, getPoint.y, color);
}

然后这个来克隆你的二维数组:

public void makeMove(Point fromPoint, Point toPoint) {
    final Piece[][] prevBoard = board.peek();
    final int width = prevBoard.length;
    Piece[][] boardNode = new Piece[width][];
    for (int i = 0; i < width; i++) {
        final int height = prevBoard[i].length;
        boardNode[i] = new Piece[height];
        for (int j = 0; j < height; j++) {
            Piece p = prevBoard[i][j];
            if (p == null) {
                boardNode[i][j] = null;
            } else {
                boardNode[i][j] = p.clone();
            }
        }
    }

这似乎让你在正确的轨道上,但现在似乎对手正在移动球员片断,或类似的东西。

其余的祝你好运!

链接地址: http://www.djcxy.com/p/56391.html

上一篇: Java minmax algorithm returning null move and failing to correctly undo moves

下一篇: Java tictactoe problems with minmax algorithm