MoveGen { public static boolean givesCheck(Position pos, Move m) { boolean wtm = pos.whiteMove; int oKingSq = pos.getKingSq(!wtm); int oKing = wtm ? Piece.BKING : Piece.WKING; int p = Piece.makeWhite(m.promoteTo == Piece.EMPTY ? pos.getPiece(m.from) : m.promoteTo); int d1 = BitBoard.getDirection(m.to, oKingSq); switch (d1) { case 8: case -8: case 1: case -1: if ((p == Piece.WQUEEN) || (p == Piece.WROOK)) if (MoveGen.nextPiece(pos, m.to, d1) == oKing) return true; break; case 9: case 7: case -9: case -7: if ((p == Piece.WQUEEN) || (p == Piece.WBISHOP)) { if (MoveGen.nextPiece(pos, m.to, d1) == oKing) return true; } else if (p == Piece.WPAWN) { if (((d1 > 0) == wtm) && (pos.getPiece(m.to + d1) == oKing)) return true; } break; default: if (d1 != 0) { if (p == Piece.WKNIGHT) return true; } } int d2 = BitBoard.getDirection(m.from, oKingSq); if ((d2 != 0) && (d2 != d1) && (MoveGen.nextPiece(pos, m.from, d2) == oKing)) { int p2 = MoveGen.nextPieceSafe(pos, m.from, -d2); switch (d2) { case 8: case -8: case 1: case -1: if ((p2 == (wtm ? Piece.WQUEEN : Piece.BQUEEN)) || (p2 == (wtm ? Piece.WROOK : Piece.BROOK))) return true; break; case 9: case 7: case -9: case -7: if ((p2 == (wtm ? Piece.WQUEEN : Piece.BQUEEN)) || (p2 == (wtm ? Piece.WBISHOP : Piece.BBISHOP))) return true; break; } } if ((m.promoteTo != Piece.EMPTY) && (d1 != 0) && (d1 == d2)) { switch (d1) { case 8: case -8: case 1: case -1: if ((p == Piece.WQUEEN) || (p == Piece.WROOK)) if ((d1 != 0) && (MoveGen.nextPiece(pos, m.from, d1) == oKing)) return true; break; case 9: case 7: case -9: case -7: if ((p == Piece.WQUEEN) || (p == Piece.WBISHOP)) { if ((d1 != 0) && (MoveGen.nextPiece(pos, m.from, d1) == oKing)) return true; } break; } } if (p == Piece.WKING) { if (m.to - m.from == 2) { if (MoveGen.nextPieceSafe(pos, m.from, -1) == oKing) return true; if (MoveGen.nextPieceSafe(pos, m.from + 1, wtm ? 8 : -8) == oKing) return true; } else if (m.to - m.from == -2) { if (MoveGen.nextPieceSafe(pos, m.from, 1) == oKing) return true; if (MoveGen.nextPieceSafe(pos, m.from - 1, wtm ? 8 : -8) == oKing) return true; } } else if (p == Piece.WPAWN) { if (pos.getPiece(m.to) == Piece.EMPTY) { int dx = Position.getX(m.to) - Position.getX(m.from); if (dx != 0) { int epSq = m.from + dx; int d3 = BitBoard.getDirection(epSq, oKingSq); switch (d3) { case 9: case 7: case -9: case -7: if (MoveGen.nextPiece(pos, epSq, d3) == oKing) { int p2 = MoveGen.nextPieceSafe(pos, epSq, -d3); if ((p2 == (wtm ? Piece.WQUEEN : Piece.BQUEEN)) || (p2 == (wtm ? Piece.WBISHOP : Piece.BBISHOP))) return true; } break; case 1: if (MoveGen.nextPiece(pos, Math.max(epSq, m.from), d3) == oKing) { int p2 = MoveGen.nextPieceSafe(pos, Math.min(epSq, m.from), -d3); if ((p2 == (wtm ? Piece.WQUEEN : Piece.BQUEEN)) || (p2 == (wtm ? Piece.WROOK : Piece.BROOK))) return true; } break; case -1: if (MoveGen.nextPiece(pos, Math.min(epSq, m.from), d3) == oKing) { int p2 = MoveGen.nextPieceSafe(pos, Math.max(epSq, m.from), -d3); if ((p2 == (wtm ? Piece.WQUEEN : Piece.BQUEEN)) || (p2 == (wtm ? Piece.WROOK : Piece.BROOK))) return true; } break; } } } } return false; }  final MoveList pseudoLegalMoves(Position pos); final MoveList checkEvasions(Position pos); final MoveList pseudoLegalCapturesAndChecks(Position pos); final MoveList pseudoLegalCaptures(Position pos); static boolean inCheck(Position pos); static boolean givesCheck(Position pos, Move m); static boolean canTakeKing(Position pos); static boolean sqAttacked(Position pos, int sq); static void removeIllegal(Position pos, MoveList moveList); final void returnMoveList(MoveList moveList);  }
@Test public void testGivesCheck() throws ChessParseError { System.out.println("givesCheck"); Position pos = new Position(); UndoInfo ui = new UndoInfo(); pos.setPiece(TextIO.getSquare("e3"), Piece.WKING); pos.setPiece(TextIO.getSquare("e8"), Piece.BKING); pos.setPiece(TextIO.getSquare("c2"), Piece.WROOK); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Rc8"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Rc6"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Rc7"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Re2"))); pos.setPiece(TextIO.getSquare("c2"), Piece.EMPTY); pos.setPiece(TextIO.getSquare("e2"), Piece.WROOK); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Kd3"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Kd4"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Ke4"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Kf2"))); pos.setPiece(TextIO.getSquare("e4"), Piece.WBISHOP); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Bd5"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Bc6"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Kd3"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Re1"))); pos = TextIO.readFEN("4k3/3p4/8/8/4B3/2K5/4R3/8 w - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Bc6"))); pos = TextIO.readFEN("4k3/8/5K2/8/6N1/8/8/8 w - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Ke6"))); assertTrue(!MoveGen.givesCheck(pos, new Move(TextIO.getSquare("f6"), TextIO.getSquare("e7"), Piece.EMPTY))); pos = TextIO.readFEN("8/2k5/8/4N3/8/2K3B1/8/8 w - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Kc4"))); pos.setPiece(TextIO.getSquare("g3"), Piece.WROOK); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos.setPiece(TextIO.getSquare("g3"), Piece.WQUEEN); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos.setPiece(TextIO.getSquare("g3"), Piece.WKNIGHT); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos.setPiece(TextIO.getSquare("g3"), Piece.WPAWN); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos.setPiece(TextIO.getSquare("c3"), Piece.EMPTY); pos.setPiece(TextIO.getSquare("g3"), Piece.WKING); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos = TextIO.readFEN("8/2k5/3p4/4N3/8/2K3B1/8/8 w - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos = TextIO.readFEN("8/2k5/8/4N3/8/6q1/2K5/8 w - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos = TextIO.readFEN("8/2k5/8/4N3/8/8/2K5/8 w - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "Nf7"))); pos = TextIO.readFEN("2nk4/3P4/8/8/3R4/8/2K5/8 w - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc8N"))); pos = TextIO.readFEN("8/2k5/2p5/1P1P4/8/2K5/8/8 w - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc6"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "d6"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "bxc6"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "b6"))); pos = TextIO.readFEN("8/8/R1PkP2R/8/8/2K5/8/8 w - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c7"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "e7"))); pos = TextIO.readFEN("8/1P6/2kP4/8/8/2K5/8/8 w - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "d7"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "b8Q"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "b8N"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "b8R"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "b8B"))); pos = TextIO.readFEN("8/2P1P3/2k5/8/8/2K5/8/8 w - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "e8Q"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "e8N"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "e8R"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "e8B"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c8Q"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c8N"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c8R"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c8B"))); pos = TextIO.readFEN("8/8/8/8/5k2/8/8/R3K2R w KQ - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O-O"))); pos = TextIO.readFEN("8/8/8/8/6k1/8/8/R3K2R w KQ - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); pos = TextIO.readFEN("8/8/8/8/3k4/8/8/R3K2R w KQ - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O-O"))); pos = TextIO.readFEN("8/8/8/8/5k2/8/5P2/R3K2R w KQ - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); pos = TextIO.readFEN("8/8/8/8/8/8/8/R3K2k w Q - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O-O"))); pos = TextIO.readFEN("8/8/8/8/8/8/8/2k1K2R w K - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); pos.setPiece(TextIO.getSquare("d1"), Piece.WKNIGHT); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); pos = TextIO.readFEN("8/1kp5/8/3P4/8/8/8/4K3 b - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "c5"), ui); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc6"))); pos = TextIO.readFEN("3k4/2p5/8/3P4/8/8/3R4/4K3 b - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "c5"), ui); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc6"))); pos = TextIO.readFEN("5k2/2p5/8/3P4/8/B7/8/4K3 b - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "c5"), ui); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc6"))); pos = TextIO.readFEN("5k2/2p5/8/3P4/1P6/B7/8/4K3 b - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "c5"), ui); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc6"))); pos = TextIO.readFEN("8/2p5/8/R2P1k2/8/8/8/4K3 b - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "c5"), ui); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "dxc6"))); pos = TextIO.readFEN("8/2p5/8/R4k2/1K6/8/8/8 b - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c5"))); pos = TextIO.readFEN("8/2p5/8/R4k2/2K5/8/8/8 b - - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c5"))); pos = TextIO.readFEN("8/2p5/8/R4k2/3K4/8/8/8 b - - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "c5"))); pos = TextIO.readFEN("r3k2r/8/8/5K2/8/8/8/8 b kq - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O-O"))); pos = TextIO.readFEN("r3k2r/8/8/6K1/8/8/8/8 b kq - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O"))); pos = TextIO.readFEN("r3k2r/8/8/2K5/8/8/8/8 b kq - 0 1"); assertTrue(!MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O-O"))); pos = TextIO.readFEN("r3k2r/8/8/3K4/8/8/8/8 b kq - 0 1"); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "O-O-O"))); pos = TextIO.readFEN("8/8/4k3/8/4p3/8/5PK1/8 w - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "f4"), ui); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "exf3"))); pos = TextIO.readFEN("8/8/4k3/8/K3p1r1/8/5P2/8 w - - 0 1"); pos.makeMove(TextIO.stringToMove(pos, "f4"), ui); assertTrue(MoveGen.givesCheck(pos, TextIO.stringToMove(pos, "exf3"))); }