/
zobrist.go
210 lines (189 loc) · 4.58 KB
/
zobrist.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package chess
import (
"math/rand"
)
var piecesZC [12][64]uint64
var castleRightsZC [4]uint64
var enPassantZC [16]uint64
var whiteTurnZC uint64
func initZobrist() {
whiteTurnZC = rand.Uint64()
for i := 0; i < 12; i++ {
for j := 0; j < 64; j++ {
piecesZC[i][j] = rand.Uint64()
}
}
for i := 0; i < 4; i++ {
castleRightsZC[i] = rand.Uint64()
}
for i := 0; i < 16; i++ {
enPassantZC[i] = rand.Uint64()
}
}
func generateZobristHash(pos *Position) uint64 {
var hash uint64 = 0
turn := pos.turn
/* Turn */
if turn == White {
hash ^= whiteTurnZC
}
/* Castle */
cc := pos.castleRights.CanCastle
if cc(White, KingSide) {
hash ^= castleRightsZC[0]
}
if cc(White, QueenSide) {
hash ^= castleRightsZC[1]
}
if cc(Black, KingSide) {
hash ^= castleRightsZC[2]
}
if cc(Black, QueenSide) {
hash ^= castleRightsZC[3]
}
/* En passant */
enPassant := pos.enPassantSquare
if enPassant != NoSquare {
if turn == Black {
/* Next mov Black -> Current pos White -> White en passant square */
hash ^= enPassantZC[enPassant-16]
} else {
/* Next mov White -> Current pos Black -> Black en passant square */
hash ^= enPassantZC[enPassant-40+8]
}
}
/* Board */
piece := pos.board.Piece
for sq := 0; sq < 64; sq++ {
p := piece(Square(sq))
if p != NoPiece {
hash ^= piecesZC[int8(p)-1][sq]
}
}
return hash
}
func UpdateZobristHash(pos *Position, mov *Move) uint64 {
hash := pos.hash
turn := pos.turn
srcSq := mov.S1
dstSq := mov.S2
piece := pos.board.Piece
hasTag := mov.HasTag
posEnPassantSquare := pos.enPassantSquare
movEnPassantSquare := pos.updateEnPassantSquare(mov)
/* Switch turn */
hash ^= whiteTurnZC
/* Remove our piece in S1 */
ourP := piece(srcSq)
hash ^= piecesZC[int8(ourP)-1][srcSq]
/* Add our promoted piece in S2 */
var ourPromoP Piece
promo := mov.promo
if promo != NoPieceType {
ourPromoP = getPiece(promo, turn)
} else {
ourPromoP = ourP
}
hash ^= piecesZC[int8(ourPromoP)-1][dstSq]
/* Capture */
if hasTag(Capture) {
/* Remove captured piece */
hash ^= piecesZC[int8(piece(dstSq))-1][dstSq]
}
if oldCR, newCR := pos.castleRights, pos.updateCastleRights(mov); newCR != oldCR {
/* Remove old castle rights */
oldCanCastle := oldCR.CanCastle
if oldCanCastle(White, KingSide) {
hash ^= castleRightsZC[0]
}
if oldCanCastle(White, QueenSide) {
hash ^= castleRightsZC[1]
}
if oldCanCastle(Black, KingSide) {
hash ^= castleRightsZC[2]
}
if oldCanCastle(Black, QueenSide) {
hash ^= castleRightsZC[3]
}
/* Add new castle rights */
newCanCastle := newCR.CanCastle
if newCanCastle(White, KingSide) {
hash ^= castleRightsZC[0]
}
if newCanCastle(White, QueenSide) {
hash ^= castleRightsZC[1]
}
if newCanCastle(Black, KingSide) {
hash ^= castleRightsZC[2]
}
if newCanCastle(Black, QueenSide) {
hash ^= castleRightsZC[3]
}
}
/* Castle */
hasKingSideCastle := hasTag(KingSideCastle)
hasQueenSideCastle := hasTag(QueenSideCastle)
if turn == White {
rookBoard := piecesZC[int8(WhiteRook)-1]
if hasKingSideCastle {
/* Remove rook in H1 */
hash ^= rookBoard[H1]
/* Add rook in F1 */
hash ^= rookBoard[F1]
} else if hasQueenSideCastle {
/* Remove rook in A1 */
hash ^= rookBoard[A1]
/* Add rook in D1 */
hash ^= rookBoard[D1]
}
} else {
rookBoard := piecesZC[int8(BlackRook)-1]
if hasKingSideCastle {
/* Remove rook in H8 */
hash ^= rookBoard[H8]
/* Add rook in F8 */
hash ^= rookBoard[F8]
} else if hasQueenSideCastle {
/* Remove rook in A8 */
hash ^= rookBoard[A8]
/* Add rook in D8 */
hash ^= rookBoard[D8]
}
}
/* Remove old en passant square*/
if posEnPassantSquare != NoSquare {
if turn == White {
/* White mov -> Black old en passant square */
hash ^= enPassantZC[posEnPassantSquare-40+8]
} else {
/* Black mov -> White old en passant square */
hash ^= enPassantZC[posEnPassantSquare-16]
}
}
/* Add new en passant square */
if movEnPassantSquare != NoSquare {
if turn == Black {
/* Black mov -> new Black en passant square */
hash ^= enPassantZC[movEnPassantSquare-40+8]
} else {
/* White mov -> new White en passant square */
hash ^= enPassantZC[movEnPassantSquare-16]
}
}
/* En passant */
if hasTag(EnPassant) {
if turn == White {
/* White mov ->
Remove black pawn in same file as en passant square but previous rank */
hash ^= piecesZC[int8(BlackPawn)-1][posEnPassantSquare-8]
} else {
/* Black mov ->
Remove white pawn in same file as en passant square but next rank */
hash ^= piecesZC[int8(WhitePawn)-1][posEnPassantSquare+8]
}
}
return hash
}
func init() {
initZobrist()
}