< Summary

Information
Class: Rudim.Common.Zobrist
Assembly: Rudim
File(s): /home/runner/work/rudim/rudim/Rudim/Common/Zobrist.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 83
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%44100%
GetBoardHash(...)100%44100%
HashCastlingRights(...)100%11100%
HashSideToMove(...)100%22100%
FlipSideToMoveHashes(...)100%11100%
HashEnPassant(...)100%22100%

File(s)

/home/runner/work/rudim/rudim/Rudim/Common/Zobrist.cs

#LineLine coverage
 1using Rudim.Board;
 2
 3namespace Rudim.Common
 4{
 5    public static class Zobrist
 6    {
 7        public static readonly ulong[,] ZobristTable;
 8
 9        static Zobrist()
 10        {
 11            // 12 piece types (6 for each color) and 64 squares, and extra - en passant, + edge cases below
 112            ZobristTable = new ulong[14, 64];
 2813            for (int piece = 0; piece < 13; piece++)
 14            {
 169015                for (int square = 0; square < 64; square++)
 16                {
 83217                    ZobristTable[piece, square] = Random.NextULong();
 18                }
 19            }
 20
 21            // Both not needed?
 122            ZobristTable[13, 0] = Random.NextULong(); // White to move
 123            ZobristTable[13, 1] = Random.NextULong(); // Black to move
 24
 125            ZobristTable[13, 2] = Random.NextULong();
 126            ZobristTable[13, 3] = Random.NextULong();
 127            ZobristTable[13, 4] = Random.NextULong();
 128            ZobristTable[13, 5] = Random.NextULong();
 129        }
 30
 31        public static ulong GetBoardHash(BoardState boardState)
 32        {
 8633            ulong currentHash = 0;
 34
 35            // Go through piece + loop Lsb() instead of squares?
 1118036            for (int square = 0; square < 64; square++)
 37            {
 550438                int piece = boardState.GetPieceOn((Square)square);
 550439                if (piece != -1)
 40                {
 247441                    currentHash ^= ZobristTable[piece, square];
 42                }
 43            }
 44
 8645            currentHash = HashSideToMove(boardState, currentHash);
 46
 8647            currentHash = HashCastlingRights(boardState, currentHash);
 48
 8649            currentHash = HashEnPassant(boardState, currentHash);
 50
 8651            return currentHash;
 52        }
 53
 54        public static ulong HashCastlingRights(BoardState boardState, ulong currentHash)
 55        {
 1100203456            currentHash ^= ZobristTable[13, (int)boardState.Castle];
 1100203457            return currentHash;
 58        }
 59
 60        private static ulong HashSideToMove(BoardState boardState, ulong currentHash)
 61        {
 8662            currentHash ^= boardState.SideToMove == Side.White ? ZobristTable[13, 0] : ZobristTable[13, 1];
 8663            return currentHash;
 64        }
 65
 66        public static ulong FlipSideToMoveHashes(BoardState boardState, ulong currentHash)
 67        {
 574184868            currentHash ^= ZobristTable[13, 0];
 574184869            currentHash ^= ZobristTable[13, 1];
 574184870            return currentHash;
 71        }
 72
 73        public static ulong HashEnPassant(BoardState boardState, ulong currentHash)
 74        {
 1124290875            if (boardState.EnPassantSquare != Square.NoSquare)
 76            {
 24156977                currentHash ^= ZobristTable[12, (int)boardState.EnPassantSquare];
 78            }
 79
 1124290880            return currentHash;
 81        }
 82    }
 83}