コード例 #1
0
ファイル: Evaluate.cs プロジェクト: Chessforeva/Csharp4chess
        private int kingSafetyKPPart(Position pos)
        {
            ulong key = pos.pawnZobristHash() ^ pos.kingZobristHash();
            KingSafetyHashData ksh = kingSafetyHash[(int)key & (kingSafetyHash.Length - 1)];
            if (ksh.key != key) {
            int score = 0;
            ulong wPawns = pos.pieceTypeBB[Piece.WPAWN];
            ulong bPawns = pos.pieceTypeBB[Piece.BPAWN];
            {
                int safety = 0;
                int halfOpenFiles = 0;
                if (Position.getY(pos.wKingSq) < 2) {
                    ulong shelter = 1UL << Position.getX(pos.wKingSq);
                    shelter |= ((shelter & BitBoard.maskBToHFiles) >> 1) |
                               ((shelter & BitBoard.maskAToGFiles) << 1);
                    shelter <<= 8;
                    safety += 3 * BITS.bitCount(wPawns & shelter);
                    safety -= 2 * BITS.bitCount(bPawns & (shelter | (shelter << 8)));
                    shelter <<= 8;
                    safety += 2 * BITS.bitCount(wPawns & shelter);
                    shelter <<= 8;
                    safety -= BITS.bitCount(bPawns & shelter);

                    ulong wOpen = BitBoard.southFill(shelter) & (~BitBoard.southFill(wPawns)) & 0xff;
                    if (wOpen != 0) {
                        halfOpenFiles += 25 * BITS.bitCount(wOpen & 0xe7);
                        halfOpenFiles += 10 * BITS.bitCount(wOpen & 0x18);
                    }
                    ulong bOpen = BitBoard.southFill(shelter) & (~BitBoard.southFill(bPawns)) & 0xff;
                    if (bOpen != 0) {
                        halfOpenFiles += 25 * BITS.bitCount(bOpen & 0xe7);
                        halfOpenFiles += 10 * BITS.bitCount(bOpen & 0x18);
                    }
                    safety = Math.Min(safety, 8);
                }
                int kSafety = (safety - 9) * 15 - halfOpenFiles;
                score += kSafety;
            }
            {
                int safety = 0;
                int halfOpenFiles = 0;
                if (Position.getY(pos.bKingSq) >= 6) {
                    ulong shelter = 1UL << (56 + Position.getX(pos.bKingSq));
                    shelter |= ((shelter & BitBoard.maskBToHFiles) >> 1) |
                               ((shelter & BitBoard.maskAToGFiles) << 1);
                    shelter >>= 8;
                    safety += 3 * BITS.bitCount(bPawns & shelter);
                    safety -= 2 * BITS.bitCount(wPawns & (shelter | (shelter >> 8)));
                    shelter >>= 8;
                    safety += 2 * BITS.bitCount(bPawns & shelter);
                    shelter >>= 8;
                    safety -= BITS.bitCount(wPawns & shelter);

                    ulong wOpen = BitBoard.southFill(shelter) & (~BitBoard.southFill(wPawns)) & 0xff;
                    if (wOpen != 0) {
                        halfOpenFiles += 25 * BITS.bitCount(wOpen & 0xe7);
                        halfOpenFiles += 10 * BITS.bitCount(wOpen & 0x18);
                    }
                    ulong bOpen = BitBoard.southFill(shelter) & (~BitBoard.southFill(bPawns)) & 0xff;
                    if (bOpen != 0) {
                        halfOpenFiles += 25 * BITS.bitCount(bOpen & 0xe7);
                        halfOpenFiles += 10 * BITS.bitCount(bOpen & 0x18);
                    }
                    safety = Math.Min(safety, 8);
                }
                int kSafety = (safety - 9) * 15 - halfOpenFiles;
                score -= kSafety;
            }
            ksh.key = key;
            ksh.score = score;
            }
            return ksh.score;
        }