-
Notifications
You must be signed in to change notification settings - Fork 9
/
board.go
416 lines (371 loc) · 10.9 KB
/
board.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package board
import (
"github.com/domino14/macondo/alphabet"
"github.com/domino14/macondo/gaddag"
"github.com/domino14/macondo/move"
)
type BoardDirection uint8
type WordDirection int
func (bd BoardDirection) String() string {
if bd == HorizontalDirection {
return "(horizontal)"
} else if bd == VerticalDirection {
return "(vertical)"
}
return "none"
}
const (
HorizontalDirection BoardDirection = iota
VerticalDirection
)
const (
LeftDirection WordDirection = -1
RightDirection WordDirection = 1
)
// A GameBoard is the main board structure. It contains all of the Squares,
// with bonuses or filled letters, as well as cross-sets and cross-scores
// for computation. (See Appel & Jacobson paper for definition of the latter
// two terms)
type GameBoard struct {
squares [][]*Square
transposed bool
tilesPlayed int
// squaresBackup is a "backup" of the squares. It is not necessarily
// the most recent one. If we wish to run deep-ply sims we will need
// to be able to go back to the original board often.
squaresBackup [][]*Square
tilesPlayedBackup int
}
// MakeBoard creates a board from a description string.
func MakeBoard(desc []string) *GameBoard {
// Turns an array of strings into the GameBoard structure type.
rows := [][]*Square{}
rowsCopy := [][]*Square{}
for _, s := range desc {
row := []*Square{}
rowCopy := []*Square{}
for _, c := range s {
row = append(row, &Square{letter: alphabet.EmptySquareMarker, bonus: BonusSquare(c)})
rowCopy = append(rowCopy, &Square{letter: alphabet.EmptySquareMarker, bonus: BonusSquare(c)})
}
rows = append(rows, row)
rowsCopy = append(rowsCopy, rowCopy)
}
g := &GameBoard{squares: rows, squaresBackup: rowsCopy}
return g
}
// Dim is the dimension of the board. It assumes the board is square.
func (g *GameBoard) Dim() int {
return len(g.squares)
}
func (g *GameBoard) GetBonus(row int, col int) BonusSquare {
return g.squares[row][col].bonus
}
func (g *GameBoard) GetSquare(row int, col int) *Square {
return g.squares[row][col]
}
func (g *GameBoard) SetLetter(row int, col int, letter alphabet.MachineLetter) {
g.squares[row][col].letter = letter
}
func (g *GameBoard) GetLetter(row int, col int) alphabet.MachineLetter {
return g.GetSquare(row, col).letter
}
func (g *GameBoard) GetCrossSet(row int, col int, dir BoardDirection) CrossSet {
return *g.squares[row][col].getCrossSet(dir) // the actual value
}
func (g *GameBoard) ClearCrossSet(row int, col int, dir BoardDirection) {
g.squares[row][col].getCrossSet(dir).clear()
}
func (g *GameBoard) SetCrossSetLetter(row int, col int, dir BoardDirection,
ml alphabet.MachineLetter) {
g.squares[row][col].getCrossSet(dir).set(ml)
}
func (g *GameBoard) GetCrossScore(row int, col int, dir BoardDirection) int {
return g.squares[row][col].getCrossScore(dir)
}
// Transpose transposes the board, swapping rows and columns.
func (g *GameBoard) Transpose() {
n := g.Dim()
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
g.squares[i][j], g.squares[j][i] = g.squares[j][i], g.squares[i][j]
}
}
g.transposed = !g.transposed
}
func (g *GameBoard) IsTransposed() bool {
return g.transposed
}
// SetAllCrosses sets the cross sets of every square to every acceptable letter.
func (g *GameBoard) SetAllCrosses() {
// Assume square board. This should be an assertion somewhere.
n := g.Dim()
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
g.squares[i][j].hcrossSet.setAll()
g.squares[i][j].vcrossSet.setAll()
}
}
}
// ClearAllCrosses disallows all letters on all squares (more or less).
func (g *GameBoard) ClearAllCrosses() {
n := g.Dim()
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
g.squares[i][j].hcrossSet.clear()
g.squares[i][j].vcrossSet.clear()
}
}
}
// Clear clears the board.
func (g *GameBoard) Clear() {
n := g.Dim()
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
g.squares[i][j].letter = alphabet.EmptySquareMarker
}
}
g.tilesPlayed = 0
// We set all crosses because every letter is technically allowed
// on every cross-set at the very beginning.
g.SetAllCrosses()
g.UpdateAllAnchors()
}
// IsEmpty returns if the board is empty.
func (g *GameBoard) IsEmpty() bool {
return g.tilesPlayed == 0
}
func (g *GameBoard) updateAnchors(row int, col int, vertical bool) {
if vertical {
// This helps simplify the updateAnchorsForMove algorithm.
row, col = col, row
}
// Always reset the anchors before applying anything else.
g.squares[row][col].resetAnchors()
var tileAbove, tileBelow, tileLeft, tileRight, tileHere bool
if row > 0 {
tileAbove = !g.squares[row-1][col].IsEmpty()
}
if col > 0 {
tileLeft = !g.squares[row][col-1].IsEmpty()
}
if row < g.Dim()-1 {
tileBelow = !g.squares[row+1][col].IsEmpty()
}
if col < g.Dim()-1 {
tileRight = !g.squares[row][col+1].IsEmpty()
}
tileHere = !g.squares[row][col].IsEmpty()
if tileHere {
// The current square is not empty. It should only be an anchor
// if it is the rightmost square of a word (actually, squares to
// the left are probably ok, but not the leftmost square. Note
// Gordon does not have this requirement, but the algorithm does
// not work if we don't do this)
if !tileRight {
g.squares[row][col].setAnchor(HorizontalDirection)
}
// Apply the transverse logic too for the vertical anchor.
if !tileBelow {
g.squares[row][col].setAnchor(VerticalDirection)
}
} else {
// If the square is empty, it should only be an anchor if the
// squares to its left and right are empty, and at least one of
// the squares in the top and bottom are NOT empty.
if !tileLeft && !tileRight && (tileAbove || tileBelow) {
g.squares[row][col].setAnchor(HorizontalDirection)
}
// (And apply the transverse logic for the vertical anchor)
if !tileAbove && !tileBelow && (tileLeft || tileRight) {
g.squares[row][col].setAnchor(VerticalDirection)
}
}
}
func (g *GameBoard) UpdateAllAnchors() {
n := g.Dim()
if g.tilesPlayed > 0 {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
g.updateAnchors(i, j, false)
}
}
} else {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
g.squares[i][j].resetAnchors()
}
}
rc := int(n / 2)
// If the board is empty, set just one anchor, in the center square.
g.squares[rc][rc].hAnchor = true
}
}
// IsAnchor returns whether the row/col pair is an anchor in the given
// direction.
func (g *GameBoard) IsAnchor(row int, col int, dir BoardDirection) bool {
return g.squares[row][col].anchor(dir)
}
func (g *GameBoard) posExists(row int, col int) bool {
d := g.Dim()
return row >= 0 && row < d && col >= 0 && col < d
}
// leftAndRightEmpty returns true if the squares at col - 1 and col + 1
// on this row are empty, checking carefully for boundary conditions.
func (g *GameBoard) leftAndRightEmpty(row int, col int) bool {
if g.posExists(row, col-1) {
if !g.squares[row][col-1].IsEmpty() {
return false
}
}
if g.posExists(row, col+1) {
if !g.squares[row][col+1].IsEmpty() {
return false
}
}
return true
}
// wordEdge finds the edge of a word on the board, returning the column.
//
func (g *GameBoard) wordEdge(row int, col int, dir WordDirection) int {
for g.posExists(row, col) && !g.squares[row][col].IsEmpty() {
col += int(dir)
}
return col - int(dir)
}
func (g *GameBoard) traverseBackwardsForScore(row int, col int, bag *alphabet.Bag) int {
score := 0
for g.posExists(row, col) {
ml := g.squares[row][col].letter
if ml == alphabet.EmptySquareMarker {
break
}
score += bag.Score(ml)
col--
}
return score
}
func (g *GameBoard) traverseBackwards(row int, col int, nodeIdx uint32,
checkLetterSet bool, leftMostCol int,
gaddag *gaddag.SimpleGaddag) (uint32, bool) {
// Traverse the letters on the board backwards (left). Return the index
// of the node in the gaddag for the left-most letter, and a boolean
// indicating if the gaddag path was valid.
// If checkLetterSet is true, then we traverse until leftMostCol+1 and
// check the letter set of this node to see if it includes the letter
// at leftMostCol
for g.posExists(row, col) {
ml := g.squares[row][col].letter
if ml == alphabet.EmptySquareMarker {
break
}
if checkLetterSet && col == leftMostCol {
if gaddag.InLetterSet(ml, nodeIdx) {
return nodeIdx, true
}
// Give up early; if we're checking letter sets we only care about
// this column.
return nodeIdx, false
}
nodeIdx = gaddag.NextNodeIdx(nodeIdx, ml.Unblank())
if nodeIdx == 0 {
// There is no path in the gaddag for this word part; this
// can occur if a phony was played and stayed on the board
// and the phony has no extensions for example, or if it's
// a real word with no further extensions.
return nodeIdx, false
}
col--
}
return nodeIdx, true
}
func (g *GameBoard) updateAnchorsForMove(m *move.Move) {
row, col, vertical := m.CoordsAndVertical()
if vertical {
// Transpose the logic, but NOT the board. The updateAnchors function
// assumes the board is not transposed.
col, row = row, col
}
// Update anchors all around the play.
for i := col; i < len(m.Tiles())+col; i++ {
g.updateAnchors(row, i, vertical)
if row > 0 {
g.updateAnchors(row-1, i, vertical)
}
if row < g.Dim()-1 {
g.updateAnchors(row+1, i, vertical)
}
}
if col-1 >= 0 {
g.updateAnchors(row, col-1, vertical)
}
if len(m.Tiles())+col < g.Dim() {
g.updateAnchors(row, col+len(m.Tiles()), vertical)
}
}
func (g *GameBoard) placeMoveTiles(m *move.Move) {
rowStart, colStart, vertical := m.CoordsAndVertical()
var row, col int
for idx, tile := range m.Tiles() {
if tile == alphabet.PlayedThroughMarker {
continue
}
if vertical {
row = rowStart + idx
col = colStart
} else {
col = colStart + idx
row = rowStart
}
g.squares[row][col].letter = tile
}
}
func (g *GameBoard) unplaceMoveTiles(m *move.Move) {
rowStart, colStart, vertical := m.CoordsAndVertical()
var row, col int
for idx, tile := range m.Tiles() {
if tile == alphabet.PlayedThroughMarker {
continue
}
if vertical {
row = rowStart + idx
col = colStart
} else {
col = colStart + idx
row = rowStart
}
g.squares[row][col].letter = alphabet.EmptySquareMarker
}
}
func (g *GameBoard) backupBoard() {
for ridx, r := range g.squares {
for cidx, c := range r {
g.squaresBackup[ridx][cidx].copyFrom(c)
}
}
g.tilesPlayedBackup = g.tilesPlayed
}
// PlayMove plays a move on a board. It must place tiles on the board,
// regenerate cross-sets and cross-points, and recalculate anchors.
func (g *GameBoard) PlayMove(m *move.Move, gd *gaddag.SimpleGaddag, bag *alphabet.Bag,
backup bool) {
// First back up the old board.
if backup {
g.backupBoard()
}
g.placeMoveTiles(m)
// Calculate anchors.
g.updateAnchorsForMove(m)
// Calculate cross-sets.
g.updateCrossSetsForMove(m, gd, bag)
g.tilesPlayed += m.TilesPlayed()
}
// RestoreFromBackup restores the squares of this board from the backupBoard.
func (g *GameBoard) RestoreFromBackup() {
for ridx, r := range g.squaresBackup {
for cidx, c := range r {
g.squares[ridx][cidx].copyFrom(c)
}
}
g.tilesPlayedBackup = g.tilesPlayed
}