/** Determines whether a particular square is attacked by a piece of the given color. */ public boolean isAttackedByColor(Square target, PieceColor color) { MoveLogic simpleChessLogic = new SimpleChessLogic(); Position modPosition; if (this.getPieceAt(target).getColor().invert() != color) { return false; } if (this.getColorToMove() != color) { modPosition = new Position(this); modPosition.setColorToMove(color); } else { modPosition = this; } for (int i = 0; i < Chess.BOARD_SIZE; i++) { for (int j = 0; j < Chess.BOARD_SIZE; j++) { final Square source = new Square(i, j); final Move move = new Move(source, target); if (simpleChessLogic.isLegal(this, move)) { return true; } } } return false; }
/** * Determines whether the king with the move is currently attacked by a piece of the opposite * color. */ public boolean isKingToMoveInCheck() { Position opponentToMovePosition = new Position(this); opponentToMovePosition.setColorToMove(this.getColorToMove().invert()); return opponentToMovePosition.isKingLeftInCheck(); }