-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZobristKey.cs
109 lines (99 loc) · 4.57 KB
/
ZobristKey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Text;
namespace SrcChess {
/// <summary>
/// Zobrist key implementation.
/// </summary>
public class ZobristKey {
/// <summary>Random value for each piece/position</summary>
static private Int64[] m_pi64RndTable;
//*********************************************************
//
/// <summary>
/// Static constructor. Use to create the random value for each case of the board.
/// </summary>
//
//*********************************************************
static ZobristKey() {
Random rnd;
long lPart1;
long lPart2;
long lPart3;
long lPart4;
rnd = new Random(0);
m_pi64RndTable = new Int64[64 * 16];
for (int i = 0; i < 64 * 16; i++) {
lPart1 = (long)rnd.Next(65536);
lPart2 = (long)rnd.Next(65536);
lPart3 = (long)rnd.Next(65536);
lPart4 = (long)rnd.Next(65536);
m_pi64RndTable[i] = (lPart1 << 48) | (lPart2 << 32) | (lPart3 << 16) | lPart4;
}
}
//*********************************************************
//
/// <summary>
/// Update the Zobrist key using the specified move
/// </summary>
/// <param name="i64ZobristKey">Zobrist key</param>
/// <param name="iPos"> Piece position</param>
/// <param name="eOldPiece"> Old value</param>
/// <param name="eNewPiece"> New value</param>
//
//*********************************************************
public static long UpdateZobristKey(long i64ZobristKey, int iPos, ChessBoard.PieceE eOldPiece, ChessBoard.PieceE eNewPiece) {
int iBaseIndex;
iBaseIndex = iPos << 4;
i64ZobristKey ^= m_pi64RndTable[iBaseIndex + ((int)eOldPiece)] ^
m_pi64RndTable[iBaseIndex + ((int)eNewPiece)];
return(i64ZobristKey);
}
//*********************************************************
//
/// <summary>
/// Update the Zobrist key using the specified move
/// </summary>
/// <param name="i64ZobristKey">Zobrist key</param>
/// <param name="iPos1"> Piece position</param>
/// <param name="eOldPiece1"> Old value</param>
/// <param name="eNewPiece1"> New value</param>
/// <param name="iPos2"> Piece position</param>
/// <param name="eOldPiece2"> Old value</param>
/// <param name="eNewPiece2"> New value</param>
//
//*********************************************************
public static long UpdateZobristKey(long i64ZobristKey,
int iPos1,
ChessBoard.PieceE eOldPiece1,
ChessBoard.PieceE eNewPiece1,
int iPos2,
ChessBoard.PieceE eOldPiece2,
ChessBoard.PieceE eNewPiece2) {
int iBaseIndex1;
int iBaseIndex2;
iBaseIndex1 = iPos1 << 4;
iBaseIndex2 = iPos2 << 4;
i64ZobristKey ^= m_pi64RndTable[iBaseIndex1 + ((int)eOldPiece1)] ^
m_pi64RndTable[iBaseIndex1 + ((int)eNewPiece1)] ^
m_pi64RndTable[iBaseIndex2 + ((int)eOldPiece2)] ^
m_pi64RndTable[iBaseIndex2 + ((int)eNewPiece2)];
return(i64ZobristKey);
}
//*********************************************************
//
/// <summary>
/// Compute the zobrist key for a board
/// </summary>
/// <param name="peBoard"> Board</param>
//
//*********************************************************
public static long ComputeBoardZobristKey(ChessBoard.PieceE[] peBoard) {
long lRetVal = 0;
for (int iIndex = 0; iIndex < 64; iIndex++) {
lRetVal ^= m_pi64RndTable[(iIndex << 4) + (int)peBoard[iIndex]];
}
return(lRetVal);
}
}
}