如何防止StackOverflowError?

时间:2018-09-22 17:37:35

标签: java exception chess

我很长时间都在制作国际象棋程序。我创建了一个称为Checkmate的类来检测支票,非法举动,Checkmate和Stalemate。到目前为止,我刚刚实现了“检查”和“非法”移动。但是,每当我运行该程序时,都会导致StackOverflowError。

/**
 * <code>String player</code> contains the constant of the currently playing player.
 * This method will first find out the position of the opponent's King and store it in <code>Point opponentKing</code>.
 * After that, it will find all the currently playing player's pieces. It will find the possible moves of each of the piece.
 * If the <code>opponentKing</code> is found within the possible moves of any piece, it will return "true, else it will return "false".
 * @return "true" if it is check. "false" if it is not a check.
 */
public boolean getIsCheck(String[][] board) {
    boolean isCheck = false;
    Point opponentKing = new Point(0,0);
    outer: for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if(!board[i][j].substring(1).equals(player) && board[i][j].substring(0,1).equals(Board.KING)) {
                opponentKing = new Point(i, j);
                break outer;
            }
        }
    }
    List<Point> friendlyPieces = new ArrayList<>();
    for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++)
            if (board[i][j].substring(1).equals(player)
                    && !board[i][j].equals(Board.EMPTY))
                friendlyPieces.add(new Point(i, j));

    outer: for(Point p : friendlyPieces) {
        List<Point> availableMoves = new ArrayList<>();
        switch (board[p.x][p.y].substring(0,1)){
            case Board.PAWN: availableMoves = new Pawn(player).getCheckMoves(p); break;
            case Board.ROOK: availableMoves = new Rook(player).getAvailableMoves(p); break;
            case Board.KNIGHT: availableMoves = new Knight(player).getAvailableMoves(p); break;
            case Board.BISHOP: availableMoves = new Bishop(player).getAvailableMoves(p); break;
            case Board.KING: availableMoves = new King(player).getAvailableMoves(p); break;
            case Board.QUEEN: availableMoves = new Queen(player).getAvailableMoves(p); break;
        }
        for (Point point: availableMoves) {
            if(point.equals(opponentKing)) {
                isCheck = true;
                break outer;
            }
        }
    }
    return isCheck;
}

这是用于查找棋盘的当前位置是否处于国王检查状态的代码。

public boolean getIsIllegal(Point start, Point end, Pieces piece){
    String[][] board = Board.board;
    board[start.x][start.y] = Board.EMPTY;
    board[end.x][end.y] = piece.getPiece() + piece.getPlayer();
    return getIsCheck(board);
}

这是同一类Checkmate中的getIsIllegal()。

每当我从棋盘上选择棋子时。它给了我以下错误:-

Exception in thread "main" java.lang.StackOverflowError
    at Pieces.Pawn.getCheckMoves(Pawn.java:56)
    at Win.Checkmate.getIsCheck(Checkmate.java:60)
    at Win.Checkmate.getIsIllegal(Checkmate.java:81)
    at Pieces.Knight.getAvailableMoves(Knight.java:30)
    at Win.Checkmate.getIsCheck(Checkmate.java:62)
    at Win.Checkmate.getIsIllegal(Checkmate.java:81)
    at Pieces.Knight.getAvailableMoves(Knight.java:34)
    at Win.Checkmate.getIsCheck(Checkmate.java:62)
    at Win.Checkmate.getIsIllegal(Checkmate.java:81)

以此类推。

if (x<7 && y<6 && isEmpty(x + 1, y + 2) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x + 1, y + 2));
    if (x>0 && y<6 && isEmpty(x - 1, y + 2) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x - 1, y + 2));
    if (x<6 && y>0 && isEmpty(x + 2, y - 1) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x + 2, y - 1));
    if (x<6 && y<7 && isEmpty(x + 2, y + 1) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x + 2, y + 1));
    if (x<7 && y>1 && isEmpty(x + 1, y - 2) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x + 1, y - 2));
    if (x>0 && y>1 && isEmpty(x - 1, y - 2) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x - 1, y - 2));
    if (x>1 && y>0 && isEmpty(x - 2, y - 1) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x - 2, y - 1));
    if (x>1 && y<7 && isEmpty(x - 2, y + 1) && !check.getIsIllegal(point, new Point(x, y), this)) availableMoves.add(new Point(x - 2, y + 1));

上面的代码是我为在当前板上获得骑士可用动作而编写的代码行。这是getIsCheck()和getIsIllegal()之间不断循环的地方。请参阅“例外”以进一步说明。

1 个答案:

答案 0 :(得分:0)

虽然棋子的移动是不合法的,但棋子还是会检查国王!例如:白色Kc2,Rb2-黑色Kh2,Ba3:

enter image description here

在移动Kc2-c1之后,Rb2会进行检查,尽管Rb2不能移动。

更改可用和非法举动的定义:可用举动可能是非法的。因此,getAvailableMoves()不会调用getIsIllegal()。因此,Rb2-h2是可用的移动,方法isCheck()起作用。

现在,要采取法律行动,您必须像这样编写代码:

...
Pieces piece;
switch (board[p.x][p.y].substring(0,1)){
    case Board.PAWN: piece = new Pawn(player); break;
    case Board.ROOK: piece = new Rook(player); break;
    case Board.KNIGHT: piece = new Knight(player); break;
    case Board.BISHOP: piece = new Bishop(player); break;
    case Board.KING: piece = new King(player); break;
    case Board.QUEEN: piece = new Queen(player); break;
} 

List<Point> availableMoves = piece.getAvailableMoves(p);
List<Point> legalMoves = new ArrayList<>();

for (Point point : availableMoves ) {
    if (!getIsIllegal(p, point, piece))
        legalMoves.add(point);
}
...