| | 1 | | using Rudim.Board; |
| | 2 | |
|
| | 3 | | namespace 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 |
| 1 | 12 | | ZobristTable = new ulong[14, 64]; |
| 28 | 13 | | for (int piece = 0; piece < 13; piece++) |
| | 14 | | { |
| 1690 | 15 | | for (int square = 0; square < 64; square++) |
| | 16 | | { |
| 832 | 17 | | ZobristTable[piece, square] = Random.NextULong(); |
| | 18 | | } |
| | 19 | | } |
| | 20 | |
|
| | 21 | | // Both not needed? |
| 1 | 22 | | ZobristTable[13, 0] = Random.NextULong(); // White to move |
| 1 | 23 | | ZobristTable[13, 1] = Random.NextULong(); // Black to move |
| | 24 | |
|
| 1 | 25 | | ZobristTable[13, 2] = Random.NextULong(); |
| 1 | 26 | | ZobristTable[13, 3] = Random.NextULong(); |
| 1 | 27 | | ZobristTable[13, 4] = Random.NextULong(); |
| 1 | 28 | | ZobristTable[13, 5] = Random.NextULong(); |
| 1 | 29 | | } |
| | 30 | |
|
| | 31 | | public static ulong GetBoardHash(BoardState boardState) |
| | 32 | | { |
| 86 | 33 | | ulong currentHash = 0; |
| | 34 | |
|
| | 35 | | // Go through piece + loop Lsb() instead of squares? |
| 11180 | 36 | | for (int square = 0; square < 64; square++) |
| | 37 | | { |
| 5504 | 38 | | int piece = boardState.GetPieceOn((Square)square); |
| 5504 | 39 | | if (piece != -1) |
| | 40 | | { |
| 2474 | 41 | | currentHash ^= ZobristTable[piece, square]; |
| | 42 | | } |
| | 43 | | } |
| | 44 | |
|
| 86 | 45 | | currentHash = HashSideToMove(boardState, currentHash); |
| | 46 | |
|
| 86 | 47 | | currentHash = HashCastlingRights(boardState, currentHash); |
| | 48 | |
|
| 86 | 49 | | currentHash = HashEnPassant(boardState, currentHash); |
| | 50 | |
|
| 86 | 51 | | return currentHash; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | public static ulong HashCastlingRights(BoardState boardState, ulong currentHash) |
| | 55 | | { |
| 11002034 | 56 | | currentHash ^= ZobristTable[13, (int)boardState.Castle]; |
| 11002034 | 57 | | return currentHash; |
| | 58 | | } |
| | 59 | |
|
| | 60 | | private static ulong HashSideToMove(BoardState boardState, ulong currentHash) |
| | 61 | | { |
| 86 | 62 | | currentHash ^= boardState.SideToMove == Side.White ? ZobristTable[13, 0] : ZobristTable[13, 1]; |
| 86 | 63 | | return currentHash; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public static ulong FlipSideToMoveHashes(BoardState boardState, ulong currentHash) |
| | 67 | | { |
| 5741848 | 68 | | currentHash ^= ZobristTable[13, 0]; |
| 5741848 | 69 | | currentHash ^= ZobristTable[13, 1]; |
| 5741848 | 70 | | return currentHash; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public static ulong HashEnPassant(BoardState boardState, ulong currentHash) |
| | 74 | | { |
| 11242908 | 75 | | if (boardState.EnPassantSquare != Square.NoSquare) |
| | 76 | | { |
| 241569 | 77 | | currentHash ^= ZobristTable[12, (int)boardState.EnPassantSquare]; |
| | 78 | | } |
| | 79 | |
|
| 11242908 | 80 | | return currentHash; |
| | 81 | | } |
| | 82 | | } |
| | 83 | | } |