Evaluate { private int kingSafety(Position pos) { final int minM = rV + bV; final int m = (pos.wMtrl - pos.wMtrlPawns + pos.bMtrl - pos.bMtrlPawns) / 2; if (m <= minM) return 0; final int maxM = qV + 2 * rV + 2 * bV + 2 * nV; int score = kingSafetyKPPart(pos); if (Position.getY(pos.wKingSq) == 0) { if (((pos.pieceTypeBB[Piece.WKING] & 0x60L) != 0) && ((pos.pieceTypeBB[Piece.WROOK] & 0xC0L) != 0) && ((pos.pieceTypeBB[Piece.WPAWN] & BitBoard.maskFile[6]) != 0) && ((pos.pieceTypeBB[Piece.WPAWN] & BitBoard.maskFile[7]) != 0)) { score -= 6 * 15; } else if (((pos.pieceTypeBB[Piece.WKING] & 0x6L) != 0) && ((pos.pieceTypeBB[Piece.WROOK] & 0x3L) != 0) && ((pos.pieceTypeBB[Piece.WPAWN] & BitBoard.maskFile[0]) != 0) && ((pos.pieceTypeBB[Piece.WPAWN] & BitBoard.maskFile[1]) != 0)) { score -= 6 * 15; } } if (Position.getY(pos.bKingSq) == 7) { if (((pos.pieceTypeBB[Piece.BKING] & 0x6000000000000000L) != 0) && ((pos.pieceTypeBB[Piece.BROOK] & 0xC000000000000000L) != 0) && ((pos.pieceTypeBB[Piece.BPAWN] & BitBoard.maskFile[6]) != 0) && ((pos.pieceTypeBB[Piece.BPAWN] & BitBoard.maskFile[7]) != 0)) { score += 6 * 15; } else if (((pos.pieceTypeBB[Piece.BKING] & 0x600000000000000L) != 0) && ((pos.pieceTypeBB[Piece.BROOK] & 0x300000000000000L) != 0) && ((pos.pieceTypeBB[Piece.BPAWN] & BitBoard.maskFile[0]) != 0) && ((pos.pieceTypeBB[Piece.BPAWN] & BitBoard.maskFile[1]) != 0)) { score += 6 * 15; } } score += (bKingAttacks - wKingAttacks) * 4; final int kSafety = interpolate(m, minM, 0, maxM, score); return kSafety; } Evaluate(); final int evalPos(Position pos);  }
@Test public void testKingSafety() throws ChessParseError { System.out.println("kingSafety"); Position pos = TextIO.readFEN("r3kb1r/p1p1pppp/b2q1n2/4N3/3P4/2N1PQ2/P2B1PPP/R3R1K1 w kq - 0 1"); int s1 = evalWhite(pos); pos.setPiece(TextIO.getSquare("g7"), Piece.EMPTY); pos.setPiece(TextIO.getSquare("b7"), Piece.BPAWN); int s2 = evalWhite(pos); assertTrue(s2 < s1); pos = TextIO.readFEN("rnbqk1nr/pppp1ppp/8/8/1bBpP3/8/PPP2PPP/RNBQK1NR w KQkq - 2 4"); s1 = evalWhite(pos); pos.setPiece(TextIO.getSquare("e1"), Piece.EMPTY); pos.setPiece(TextIO.getSquare("f1"), Piece.WKING); s2 = evalWhite(pos); assertTrue(s2 < s1); pos = TextIO.readFEN("rnbqk1nr/pppp1ppp/8/8/1bBpPB2/8/PPP1QPPP/RN1K2NR w kq - 0 1"); s1 = evalWhite(pos); pos.setPiece(TextIO.getSquare("d1"), Piece.EMPTY); pos.setPiece(TextIO.getSquare("c1"), Piece.WKING); s2 = evalWhite(pos); assertTrue(s2 < s1); }