Evaluate { private int pieceSquareEval(Position pos) { int score = 0; final int wMtrl = pos.wMtrl; final int bMtrl = pos.bMtrl; final int wMtrlPawns = pos.wMtrlPawns; final int bMtrlPawns = pos.bMtrlPawns; { final int t1 = qV + 2 * rV + 2 * bV; final int t2 = rV; { final int k1 = pos.psScore1[Piece.WKING]; final int k2 = pos.psScore2[Piece.WKING]; final int t = bMtrl - bMtrlPawns; score += interpolate(t, t2, k2, t1, k1); } { final int k1 = pos.psScore1[Piece.BKING]; final int k2 = pos.psScore2[Piece.BKING]; final int t = wMtrl - wMtrlPawns; score -= interpolate(t, t2, k2, t1, k1); } } { final int t1 = qV + 2 * rV + 2 * bV; final int t2 = rV; int wp1 = pos.psScore1[Piece.WPAWN]; int wp2 = pos.psScore2[Piece.WPAWN]; if ((wp1 != 0) || (wp2 != 0)) { final int tw = bMtrl - bMtrlPawns; score += interpolate(tw, t2, wp2, t1, wp1); } int bp1 = pos.psScore1[Piece.BPAWN]; int bp2 = pos.psScore2[Piece.BPAWN]; if ((bp1 != 0) || (bp2 != 0)) { final int tb = wMtrl - wMtrlPawns; score -= interpolate(tb, t2, bp2, t1, bp1); } } { final int t1 = qV + 2 * rV + bV + nV + 6 * pV; final int t2 = nV + 8 * pV; int n1 = pos.psScore1[Piece.WKNIGHT]; int n2 = pos.psScore2[Piece.WKNIGHT]; if ((n1 != 0) || (n2 != 0)) { score += interpolate(bMtrl, t2, n2, t1, n1); } n1 = pos.psScore1[Piece.BKNIGHT]; n2 = pos.psScore2[Piece.BKNIGHT]; if ((n1 != 0) || (n2 != 0)) { score -= interpolate(wMtrl, t2, n2, t1, n1); } } { score += pos.psScore1[Piece.WBISHOP]; score -= pos.psScore1[Piece.BBISHOP]; } { final long occupied = pos.whiteBB | pos.blackBB; score += pos.psScore1[Piece.WQUEEN]; long m = pos.pieceTypeBB[Piece.WQUEEN]; while (m != 0) { int sq = BitBoard.numberOfTrailingZeros(m); long atk = BitBoard.rookAttacks(sq, occupied) | BitBoard.bishopAttacks(sq, occupied); wAttacksBB |= atk; score += queenMobScore[Long.bitCount(atk & ~(pos.whiteBB | bPawnAttacks))]; bKingAttacks += Long.bitCount(atk & bKingZone) * 2; m &= m-1; } score -= pos.psScore1[Piece.BQUEEN]; m = pos.pieceTypeBB[Piece.BQUEEN]; while (m != 0) { int sq = BitBoard.numberOfTrailingZeros(m); long atk = BitBoard.rookAttacks(sq, occupied) | BitBoard.bishopAttacks(sq, occupied); bAttacksBB |= atk; score -= queenMobScore[Long.bitCount(atk & ~(pos.blackBB | wPawnAttacks))]; wKingAttacks += Long.bitCount(atk & wKingZone) * 2; m &= m-1; } } { int r1 = pos.psScore1[Piece.WROOK]; if (r1 != 0) { final int nP = bMtrlPawns / pV; final int s = r1 * Math.min(nP, 6) / 6; score += s; } r1 = pos.psScore1[Piece.BROOK]; if (r1 != 0) { final int nP = wMtrlPawns / pV; final int s = r1 * Math.min(nP, 6) / 6; score -= s; } } return score; } Evaluate(); final int evalPos(Position pos);  }
@Test public void testPieceSquareEval() throws ChessParseError { System.out.println("pieceSquareEval"); Position pos = TextIO.readFEN(TextIO.startPosFEN); int score = evalWhite(pos); assertEquals(0, score); UndoInfo ui = new UndoInfo(); pos.makeMove(TextIO.stringToMove(pos, "e4"), ui); score = evalWhite(pos); assertTrue(score > 0); pos.makeMove(TextIO.stringToMove(pos, "e5"), ui); score = evalWhite(pos); assertEquals(0, score); assertTrue(moveScore(pos, "Nf3") > 0); pos.makeMove(TextIO.stringToMove(pos, "Nf3"), ui); assertTrue(moveScore(pos, "Nc6") < 0); pos.makeMove(TextIO.stringToMove(pos, "Nc6"), ui); assertTrue(moveScore(pos, "Bb5") > 0); pos.makeMove(TextIO.stringToMove(pos, "Bb5"), ui); pos.makeMove(TextIO.stringToMove(pos, "Nge7"), ui); assertTrue(moveScore(pos, "Qe2") > 0); score = evalWhite(pos); pos.makeMove(TextIO.stringToMove(pos, "Bxc6"), ui); pos.makeMove(TextIO.stringToMove(pos, "Nxc6"), ui); int score2 = evalWhite(pos); assertTrue(score2 < score); pos = TextIO.readFEN("5k2/4nppp/p1n5/1pp1p3/4P3/2P1BN2/PP3PPP/3R2K1 w - - 0 1"); assertTrue(moveScore(pos, "Rd7") > 0); assertTrue(moveScore(pos, "Rd8") <= 0); pos.setPiece(TextIO.getSquare("a1"), Piece.WROOK); assertTrue(moveScore(pos, "Rac1") > 0); pos = TextIO.readFEN("r4rk1/pppRRppp/1q4b1/n7/8/2N3B1/PPP1QPPP/6K1 w - - 0 1"); score = evalWhite(pos); assertTrue(score > 100); }