MoveGen { public static void removeIllegal(Position pos, MoveList moveList) { int length = 0; UndoInfo ui = new UndoInfo(); boolean isInCheck = inCheck(pos); final long occupied = pos.whiteBB | pos.blackBB; int kSq = pos.getKingSq(pos.whiteMove); long kingAtks = BitBoard.rookAttacks(kSq, occupied) | BitBoard.bishopAttacks(kSq, occupied); int epSquare = pos.getEpSquare(); if (isInCheck) { kingAtks |= pos.pieceTypeBB[pos.whiteMove ? Piece.BKNIGHT : Piece.WKNIGHT]; for (int mi = 0; mi < moveList.size; mi++) { Move m = moveList.m[mi]; boolean legal; if ((m.from != kSq) && ((kingAtks & (1L<<m.to)) == 0) && (m.to != epSquare)) { legal = false; } else { pos.makeMove(m, ui); pos.setWhiteMove(!pos.whiteMove); legal = !inCheck(pos); pos.setWhiteMove(!pos.whiteMove); pos.unMakeMove(m, ui); } if (legal) moveList.m[length++].copyFrom(m); } } else { for (int mi = 0; mi < moveList.size; mi++) { Move m = moveList.m[mi]; boolean legal; if ((m.from != kSq) && ((kingAtks & (1L<<m.from)) == 0) && (m.to != epSquare)) { legal = true; } else { pos.makeMove(m, ui); pos.setWhiteMove(!pos.whiteMove); legal = !inCheck(pos); pos.setWhiteMove(!pos.whiteMove); pos.unMakeMove(m, ui); } if (legal) moveList.m[length++].copyFrom(m); } } moveList.size = length; }  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 testRemoveIllegal() throws ChessParseError { System.out.println("removeIllegal"); Position pos = TextIO.readFEN("8/3k4/8/2n1rpP1/1P6/1NB5/2QP4/R3K2R w KQ f6 0 1"); List<String> strMoves = getMoveList(pos, true); assertTrue(strMoves.contains("c2e4")); assertTrue(strMoves.contains("c3e5")); assertTrue(strMoves.contains("e1d1")); assertTrue(strMoves.contains("e1f1")); assertTrue(strMoves.contains("e1f2")); assertEquals(5, strMoves.size()); pos = TextIO.readFEN("4k3/8/8/2KPp1r1/8/8/8/8 w - e6 0 2"); strMoves = getMoveList(pos, true); assertTrue(!strMoves.contains("d5e6")); assertEquals(7, strMoves.size()); pos = TextIO.readFEN("8/6p1/4p3/2k1Pp1B/4KP1p/6rP/8/8 w - f6 0 55"); strMoves = getMoveList(pos, true); assertTrue(strMoves.contains("e5f6")); assertEquals(1, strMoves.size()); }