/
hash.go
148 lines (131 loc) · 3.67 KB
/
hash.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
package zobrist
import (
"lukechampine.com/frand"
"github.com/domino14/macondo/game"
"github.com/domino14/macondo/move"
"github.com/domino14/macondo/tilemapping"
)
const bignum = 1<<63 - 2
// generate a zobrist hash for a crossword game position.
// https://en.wikipedia.org/wiki/Zobrist_hashing
type Zobrist struct {
theirTurn uint64
posTable [][]uint64
ourRackTable [][]uint64
theirRackTable [][]uint64
scorelessTurns [3]uint64
boardDim int
}
const MaxLetters = 35
func (z *Zobrist) Initialize(boardDim int) {
z.boardDim = boardDim
z.posTable = make([][]uint64, boardDim*boardDim)
for i := 0; i < boardDim*boardDim; i++ {
z.posTable[i] = make([]uint64, MaxLetters*2)
for j := 0; j < 70; j++ {
z.posTable[i][j] = frand.Uint64n(bignum) + 1
}
}
z.ourRackTable = make([][]uint64, MaxLetters)
for i := 0; i < MaxLetters; i++ {
z.ourRackTable[i] = make([]uint64, game.RackTileLimit)
for j := 0; j < game.RackTileLimit; j++ {
z.ourRackTable[i][j] = frand.Uint64n(bignum) + 1
}
}
z.theirRackTable = make([][]uint64, MaxLetters)
for i := 0; i < MaxLetters; i++ {
z.theirRackTable[i] = make([]uint64, game.RackTileLimit)
for j := 0; j < game.RackTileLimit; j++ {
z.theirRackTable[i][j] = frand.Uint64n(bignum) + 1
}
}
for i := 0; i < 3; i++ {
z.scorelessTurns[i] = frand.Uint64n(bignum) + 1
}
z.theirTurn = frand.Uint64n(bignum) + 1
}
func (z *Zobrist) Hash(squares tilemapping.MachineWord,
ourRack, theirRack *tilemapping.Rack, theirTurn bool, scorelessTurns int) uint64 {
key := uint64(0)
for i, letter := range squares {
if letter == 0 {
continue
}
if letter&0x80 > 0 {
// it's a blank
letter = (letter & (0x7F)) + MaxLetters
}
key ^= z.posTable[i][letter]
}
for i, ct := range ourRack.LetArr {
key ^= z.ourRackTable[i][ct]
}
for i, ct := range theirRack.LetArr {
key ^= z.theirRackTable[i][ct]
}
if theirTurn {
key ^= z.theirTurn
}
key ^= z.scorelessTurns[scorelessTurns]
return key
}
func (z *Zobrist) AddMove(key uint64, m move.PlayMaker, wasOurMove bool,
scorelessTurns, lastScorelessTurns int) uint64 {
// Adding a move:
// For every letter in the move (assume it's only a tile placement move
// or a pass for now):
// - XOR with its position on the board
// - XOR with the "position" on the rack hash
// Then:
// - XOR with p2ToMove since we always alternate
rackTable := z.ourRackTable
if !wasOurMove {
rackTable = z.theirRackTable
}
if m.Type() == move.MoveTypePlay {
row, col, vertical := m.RowStart(), m.ColStart(), m.Vertical()
ri, ci := 0, 1
if vertical {
ri, ci = 1, 0
}
placeholderRack := [MaxLetters]tilemapping.MachineLetter{}
for idx, tile := range m.Tiles() {
newRow := row + (ri * idx)
newCol := col + (ci * idx)
if tile == 0 {
// 0 is a played-through marker if it's part of a move's tiles
continue
}
boardTile := tile
if tile&0x80 > 0 {
// it's a blank
boardTile = (tile & (0x7F)) + MaxLetters
}
key ^= z.posTable[newRow*z.boardDim+newCol][boardTile]
// build up placeholder rack.
tileIdx := tile.IntrinsicTileIdx()
placeholderRack[tileIdx]++
}
for _, tile := range m.Leave() {
placeholderRack[tile]++
}
// now "Play" all the tiles in the rack
for _, tile := range m.Tiles() {
if tile == 0 {
// this is a play-through tile, if it's in a move. Ignore it.
continue
}
tileIdx := tile.IntrinsicTileIdx()
key ^= rackTable[tileIdx][placeholderRack[tileIdx]]
placeholderRack[tileIdx]--
key ^= rackTable[tileIdx][placeholderRack[tileIdx]]
}
}
if lastScorelessTurns != scorelessTurns {
key ^= z.scorelessTurns[lastScorelessTurns]
key ^= z.scorelessTurns[scorelessTurns]
}
key ^= z.theirTurn
return key
}