Search code examples
javachess

Chess/Java: "Moved Out Of Check" Check in Java


So I've been working on this chess app for about a week now (a few hours a day)-- and I seem to have hit a snag. All of the pieces move as they should, with collision and all-- I have promotion and castling working, and I can successfully check for being in check.

However, I can't seem to check if a move legally gets out of check. My current approach is to move the piece-- check if the person is still in check, and if not then the move is legal.

It seems that when I do this, however, it doesn't correctly calculate the check after the faux-move.

Any help would be great.

public abstract class Piece {
private Type type = Type.BLANK;
private int locX, locY, preX, preY;
private Player player;
private boolean moved = false;

Piece(Type type, int locX, int locY, Player player) {
    this.locX = locX;
    this.locY = locY;
    this.player = player;
    this.type = type;
}

public void movePiece(int x, int y) {
    if (player.isTurn()) {
        if (isLegalMove(x, y)) {
            if (checkCollision(x, y)) {
                if (clearsCheck(x, y)) {
                    preX = locX;
                    preY = locY;
                    setLoc(x, y);
                    specialPreMove(x, y);
                    moved = true;
                    Chess.chessBoard.bitBoard[preX][preY] = null;
                    if (Chess.chessBoard.bitBoard[x][y] != null) {
                        Chess.chessBoard.bitBoard[x][y].getPlayer().pieces.remove(Chess.chessBoard.bitBoard[x][y]);
                    }
                    Chess.chessBoard.bitBoard[x][y] = this;
                    specialPostMove(x, y);
                    Chess.chessBoard.getGui().repaint();
                    Chess.changeTurns();
                }
            }
        }
    }
}

protected void specialPreMove(int x, int y) {}
protected void specialPostMove(int x, int y) {}

protected abstract boolean checkCollision(int x, int y);

protected abstract boolean isLegalMove(int x, int y);

private boolean clearsCheck(int x, int y) {
    boolean checkCk = false;
    preX = locX;
    preY = locY;

    setLoc(x, y);
    Piece locPiece = null;

    Chess.chessBoard.bitBoard[preX][preY] = null;
    if (Chess.chessBoard.bitBoard[x][y] != null) {
        locPiece = Chess.chessBoard.bitBoard[x][y];
        Chess.chessBoard.bitBoard[x][y].getPlayer().pieces.remove(Chess.chessBoard.bitBoard[x][y]);
        System.out.println("Piece there: " + locPiece);
    }
    Chess.chessBoard.bitBoard[x][y] = this;
    System.out.println(Chess.chessBoard.bitBoard[x][y]);
    Chess.chessBoard.getGui().repaint();

    Chess.checkCheck();
    checkCk = !player.inCheck();

    setLoc(preX, preY);
    Chess.chessBoard.bitBoard[preX][preY] = this;
    if (locPiece != null) {
        Chess.chessBoard.bitBoard[x][y] = locPiece;
        Chess.chessBoard.bitBoard[x][y].getPlayer().pieces.add(Chess.chessBoard.bitBoard[x][y]);
    } else {
        Chess.chessBoard.bitBoard[x][y] = null;
    }

    System.out.println(checkCk);
    return checkCk;
}

public ArrayList<Move> getLegalMoves() {
    ArrayList<Move> moves = new ArrayList<Move>();
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 8; col++) {
            if (isLegalMove(row, col) && checkCollision(row, col)) {
                moves.add(new Move(row, col));
            }
        }
    }

    return moves;
}

is the piece class, and then the check method

public static void checkCheck() {
    for (Move m : player1.getLegalMoves()) {
        if (m.getX() == player2.getKing().getLocX()
                && m.getY() == player2.getKing().getLocY()) {
            player2.setCheck(true);
            System.out.println("IN CHECK PLAYER 2");
            break;
        }
    }

    for (Move m : player2.getLegalMoves()) {
        if (m.getX() == player1.getKing().getLocX()
                && m.getY() == player1.getKing().getLocY()) {
            player1.setCheck(true);
            System.out.println("IN CHECK PLAYER 1");
            break;
        }
    }
}

and then here is some other stuff that is useful

public class Player {

private Color color;
private Direction direction;
private boolean turn;
private boolean check = false;
public ArrayList<Piece> pieces = new ArrayList<>();

public Player(Color color, Direction direction) {
    this.color = color;
    this.turn = false;
    this.direction = direction;
}

public ArrayList<Move> getLegalMoves() {
    ArrayList<Move> moves = new ArrayList<>();
    for (Piece p : pieces) {
        for (Move m : p.getLegalMoves()) {
            moves.add(m);
        }
    }

    return moves;
}

public Piece getKing() {
    for (Piece p : pieces) {
        if (p.getType() == Type.KING) {
            return p;
        }
    }

    return null;
}

Solution

  • Looks like once you set a player in check you're not setting his status later on to unchecked.

    public static void checkCheck() {
      boolean isInCheck = false;
      for (Move m : player1.getLegalMoves()) {
                if (m.getX() == player2.getKing().getLocX()
                && m.getY() == player2.getKing().getLocY()) {
                    isInCheck = true;
                    System.out.println("IN CHECK PLAYER 2");
                    break;
                }
        }
            player2.setCheck(isInCheck);
    
        isInCheck = false;
        for (Move m : player2.getLegalMoves()) {
            if (m.getX() == player1.getKing().getLocX()
            && m.getY() == player1.getKing().getLocY()) {
                isInCheck = true;
                System.out.println("IN CHECK PLAYER 1");
                break;
            }
        }
        player1.setCheck(isInCheck);
    }