-
Notifications
You must be signed in to change notification settings - Fork 0
/
zobrist.go
237 lines (208 loc) · 5.3 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
package chess
import (
"fmt"
"regexp"
"strings"
)
// ChessHasher provides methods to generate Zobrist hashes for chess positions
type ChessHasher 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
func NewChessHasher() *ChessHasher {
return &ChessHasher{
enPassantRank: -1,
enPassantFile: -1,
pawnNearby: false,
hasError: false,
}
}
// parseHexString converts a hex string to a byte slice
func parseHexString(s string) Hash {
result := make([]byte, len(s)/2)
for i := 0; i < len(s); i += 2 {
var value byte
_, _ = fmt.Sscanf(s[i:i+2], "%02x", &value)
result[i/2] = value
}
return result
}
// createHexString converts a byte slice to a hex string
func createHexString(h Hash) string {
var sb strings.Builder
for _, b := range h {
_, _ = fmt.Fprintf(&sb, "%02x", b)
}
return sb.String()
}
// xorArrays performs bitwise XOR between two byte slices
func xorArrays(a, b Hash) Hash {
length := len(a)
if len(b) < length {
length = len(b)
}
result := make(Hash, length)
for i := 0; i < length; i++ {
result[i] = a[i] ^ b[i]
}
return result
}
// xorHash performs XOR operation with a polyglot hash value
func (ch *ChessHasher) xorHash(arr Hash, num int) Hash {
return xorArrays(arr, parseHexString(GetPolyglotHashes()[num]))
}
// parseEnPassant processes the en passant square
func (ch *ChessHasher) 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 *ChessHasher) hashSide(arr Hash, color Color) Hash {
if color == White {
return ch.xorHash(arr, 780)
}
return arr
}
// hashCastling updates hash based on castling rights
func (ch *ChessHasher) hashCastling(arr Hash, s string) Hash {
if s == "-" {
return arr
}
if strings.Contains(s, "K") {
arr = ch.xorHash(arr, 768)
}
if strings.Contains(s, "Q") {
arr = ch.xorHash(arr, 769)
}
if strings.Contains(s, "k") {
arr = ch.xorHash(arr, 770)
}
if strings.Contains(s, "q") {
arr = ch.xorHash(arr, 771)
}
return arr
}
// hashPieces computes hash for the piece positions
func (ch *ChessHasher) 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':
arr = 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':
arr = 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':
arr = ch.xorHash(arr, 64*2+8*rank+file)
file++
case 'N':
arr = ch.xorHash(arr, 64*3+8*rank+file)
file++
case 'b':
arr = ch.xorHash(arr, 64*4+8*rank+file)
file++
case 'B':
arr = ch.xorHash(arr, 64*5+8*rank+file)
file++
case 'r':
arr = ch.xorHash(arr, 64*6+8*rank+file)
file++
case 'R':
arr = ch.xorHash(arr, 64*7+8*rank+file)
file++
case 'q':
arr = ch.xorHash(arr, 64*8+8*rank+file)
file++
case 'Q':
arr = ch.xorHash(arr, 64*9+8*rank+file)
file++
case 'k':
arr = ch.xorHash(arr, 64*10+8*rank+file)
file++
case 'K':
arr = 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 *ChessHasher) HashPosition(fen string) (string, error) {
ch.hasError = false
ch.enPassantRank = -1
ch.enPassantFile = -1
ch.pawnNearby = false
// Validate FEN format
validFEN := regexp.MustCompile(`^([rnbqkpRNBQKP1-8]+\/){7}[rnbqkpRNBQKP1-8]+ [wb] [KQkq-]{1,4} [a-h1-8-]{1,2} \d+ \d+$`)
if !validFEN.MatchString(fen) {
return "", fmt.Errorf("invalid FEN format")
}
parts := strings.Fields(strings.TrimSpace(fen))
if len(parts) < 4 {
return "", fmt.Errorf("incomplete FEN string")
}
pieces, color, castling, enPassant := parts[0], parts[1], parts[2], parts[3]
hash := make(Hash, len(emptyHash))
copy(hash, emptyHash)
ch.parseEnPassant(enPassant)
hash = ch.hashPieces(hash, pieces)
if ch.pawnNearby {
hash = ch.xorHash(hash, 772+ch.enPassantFile)
}
hash = ch.hashSide(hash, ColorFromString(color))
hash = ch.hashCastling(hash, castling)
if ch.hasError {
return "", fmt.Errorf("invalid position")
}
return createHexString(hash), nil
}