-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzobrist.go
286 lines (246 loc) · 6.09 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package chess
import (
"encoding/hex"
"errors"
"strconv"
"strings"
)
// ZobristHasher provides methods to generate Zobrist hashes for chess positions
type ZobristHasher struct {
enPassantRank int
enPassantFile int
pawnNearby bool
hasError bool
}
// Hash represents a Zobrist hash as a byte slice
type Hash []byte
// emptyHash is the initial hash value
var emptyHash = parseHexString("0000000000000000")
// NewChessHasher creates a new instance of ChessHasher
// Deprecated: Use NewZobristHasher instead
func NewChessHasher() *ZobristHasher {
return &ZobristHasher{
enPassantRank: -1,
enPassantFile: -1,
pawnNearby: false,
hasError: false,
}
}
// NewZobristHasher creates a new instance of ZobristHasher
func NewZobristHasher() *ZobristHasher {
return &ZobristHasher{
enPassantRank: -1,
enPassantFile: -1,
pawnNearby: false,
hasError: false,
}
}
// parseHexString converts a hex string to a byte slice efficiently
func parseHexString(s string) Hash {
// Ensure the input has an even length
if len(s)%2 != 0 {
return nil // Handle invalid input
}
// Use a preallocated buffer for zero allocations
result := make([]byte, len(s)/2)
_, err := hex.Decode(result, []byte(s))
if err != nil {
return nil // Handle invalid hex string
}
return result
}
// createHexString converts a byte slice to a hex string efficiently
func createHexString(h Hash) string {
return hex.EncodeToString(h)
}
func xorArrays(a, b Hash) {
length := len(a)
if len(b) < length {
length = len(b)
}
for i := 0; i < length; i++ {
a[i] ^= b[i] // XOR in place, avoiding new slice allocation
}
}
func xorArraysInto(a, b, out Hash) {
length := len(a)
if len(b) < length {
length = len(b)
}
if len(out) < length {
panic("output buffer too small")
}
for i := 0; i < length; i++ {
out[i] = a[i] ^ b[i]
}
}
// xorHash performs an in-place XOR operation on a hash
func (ch *ZobristHasher) xorHash(arr Hash, num int) {
// Get the precomputed Polyglot hash as a byte slice
polyglotHash := GetPolyglotHashBytes(num)
// Perform in-place XOR
xorArrays(arr, polyglotHash)
}
// parseEnPassant processes the en passant square
func (ch *ZobristHasher) parseEnPassant(s string) {
if s == "-" {
return
}
if len(s) != 2 {
ch.hasError = true
return
}
file := int(s[0] - 'a')
rank := int(s[1] - '1')
if file < 0 || file > 7 || rank < 0 || rank > 7 {
ch.hasError = true
return
}
ch.enPassantFile = file
ch.enPassantRank = rank
}
// hashSide computes the hash for the side to move
func (ch *ZobristHasher) hashSide(arr Hash, color Color) Hash {
if color == White {
ch.xorHash(arr, 780)
}
return arr
}
// hashCastling updates hash based on castling rights
func (ch *ZobristHasher) hashCastling(arr Hash, s string) Hash {
if s == "-" {
return arr
}
if strings.Contains(s, "K") {
ch.xorHash(arr, 768)
}
if strings.Contains(s, "Q") {
ch.xorHash(arr, 769)
}
if strings.Contains(s, "k") {
ch.xorHash(arr, 770)
}
if strings.Contains(s, "q") {
ch.xorHash(arr, 771)
}
return arr
}
// hashPieces computes hash for the piece positions
func (ch *ZobristHasher) hashPieces(arr Hash, s string) Hash {
ranks := strings.Split(s, "/")
if len(ranks) != 8 {
ch.hasError = true
return arr
}
for i := 0; i < 8; i++ {
file := 0
rank := 7 - i
for j := 0; j < len(ranks[i]); j++ {
piece := ranks[i][j]
switch piece {
case 'p':
ch.xorHash(arr, 8*rank+file)
if ch.enPassantRank == 2 && rank == 3 && ch.enPassantFile > 0 && file == ch.enPassantFile-1 {
ch.pawnNearby = true
}
if ch.enPassantRank == 2 && rank == 3 && ch.enPassantFile < 7 && file == ch.enPassantFile+1 {
ch.pawnNearby = true
}
file++
case 'P':
ch.xorHash(arr, 64*1+8*rank+file)
if ch.enPassantRank == 5 && rank == 4 && ch.enPassantFile > 0 && file == ch.enPassantFile-1 {
ch.pawnNearby = true
}
if ch.enPassantRank == 5 && rank == 4 && ch.enPassantFile < 7 && file == ch.enPassantFile+1 {
ch.pawnNearby = true
}
file++
case 'n':
ch.xorHash(arr, 64*2+8*rank+file)
file++
case 'N':
ch.xorHash(arr, 64*3+8*rank+file)
file++
case 'b':
ch.xorHash(arr, 64*4+8*rank+file)
file++
case 'B':
ch.xorHash(arr, 64*5+8*rank+file)
file++
case 'r':
ch.xorHash(arr, 64*6+8*rank+file)
file++
case 'R':
ch.xorHash(arr, 64*7+8*rank+file)
file++
case 'q':
ch.xorHash(arr, 64*8+8*rank+file)
file++
case 'Q':
ch.xorHash(arr, 64*9+8*rank+file)
file++
case 'k':
ch.xorHash(arr, 64*10+8*rank+file)
file++
case 'K':
ch.xorHash(arr, 64*11+8*rank+file)
file++
case '1', '2', '3', '4', '5', '6', '7', '8':
file += int(piece - '0')
default:
ch.hasError = true
return arr
}
}
if file != 8 {
ch.hasError = true
}
}
return arr
}
// HashPosition computes a Zobrist hash for a chess position in FEN notation
func (ch *ZobristHasher) HashPosition(fen string) (string, error) {
ch.hasError = false
ch.enPassantRank = -1
ch.enPassantFile = -1
ch.pawnNearby = false
// FEN should have at least 4 parts
parts := strings.SplitN(fen, " ", 5)
if len(parts) < 4 {
return "", errors.New("invalid FEN format")
}
pieces, color, castling, enPassant := parts[0], parts[1], parts[2], parts[3]
// Quick validation without regex
if len(color) != 1 || (color[0] != 'w' && color[0] != 'b') {
return "", errors.New("invalid side to move")
}
if len(castling) > 4 {
return "", errors.New("invalid castling rights")
}
hash := make(Hash, len(emptyHash))
copy(hash, emptyHash)
ch.parseEnPassant(enPassant)
hash = ch.hashPieces(hash, pieces)
if ch.pawnNearby {
ch.xorHash(hash, 772+ch.enPassantFile)
}
hash = ch.hashSide(hash, ColorFromString(color))
hash = ch.hashCastling(hash, castling)
if ch.hasError {
return "", errors.New("invalid piece placement")
}
return createHexString(hash), nil
}
func ZobristHashToUint64(hash string) uint64 {
// Ensure the input is exactly 16 hex digits
if len(hash) != 16 {
return 0
}
// Convert directly using `strconv.ParseUint`
result, err := strconv.ParseUint(hash, 16, 64)
if err != nil {
return 0
}
return result
}