-
Notifications
You must be signed in to change notification settings - Fork 1
/
zhash.go
72 lines (61 loc) · 1.77 KB
/
zhash.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
/*
* File: src/github.com/Ken1JF/ah/zhash.go
* Project: abst-hier
*
* Created by Ken Friedenbach on 12/24/2010.
* Copyright 2010-2014 Ken Friedenbach. All rights reserved.
*
* This file supports Zobrist hashing of Go boards.
*
* Currently uses a primary hash code of 32 bits, and no secondary hash code.
* If too many clashss are encountered, the following could be tried:
* Use a primary hash code of 64-N bits.
* Use a secondary code of N bits.
* For N=19, the secondary code could be XOR of B and W stones on each row/col.
*/
package ah
import (
"math/rand"
)
type ZobristCode uint32
var ZSeed int64
var BlackZKey [MaxBoardSize][MaxBoardSize]ZobristCode
var WhiteZKey [MaxBoardSize][MaxBoardSize]ZobristCode
var KoZKey [MaxBoardSize][MaxBoardSize]ZobristCode
// var BlackToPlayZKey ZobristCode don't need, redundant
var WhiteToPlayZKey ZobristCode
// should be private, but printed in zhash_test.go
var KeyCount int64
var checkKeys map[ZobristCode]int64
const checkKeysNeeded int = 1085
func newZobristKey() ZobristCode {
var nzc ZobristCode
var present bool
present = true
for present {
nzc = ZobristCode((rand.Uint32() << 24) ^ (rand.Uint32() << 16) ^ (rand.Uint32() << 8) ^ rand.Uint32())
_, present = checkKeys[nzc]
if present {
panic("Zobrist Duplicate Keys")
}
}
KeyCount += 1
checkKeys[nzc] = KeyCount
return nzc
}
// Initialize the Zobrist Keys
func init() {
checkKeys = make(map[ZobristCode]int64, checkKeysNeeded)
ZSeed = (2 * 3 * 5 * 7 * 11) - 1
rand.Seed(ZSeed)
var i, j uint8
for i = 0; i < MaxBoardSize; i += 1 {
for j = 0; j < MaxBoardSize; j += 1 {
BlackZKey[i][j] = newZobristKey()
WhiteZKey[i][j] = newZobristKey()
KoZKey[i][j] = newZobristKey()
}
}
// BlackToPlayZKey = newZobristKey()
WhiteToPlayZKey = newZobristKey()
}