-
Notifications
You must be signed in to change notification settings - Fork 10
/
move.go
486 lines (424 loc) · 11 KB
/
move.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package move
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/domino14/word-golib/tilemapping"
"github.com/rs/zerolog/log"
)
// MoveType is a type of move; a play, an exchange, pass, etc.
type MoveType uint8
const (
MoveTypePlay MoveType = iota
MoveTypeExchange
MoveTypePass
MoveTypeChallenge
MoveTypePhonyTilesReturned
MoveTypeChallengeBonus
MoveTypeUnsuccessfulChallengePass
MoveTypeEndgameTiles
MoveTypeLostTileScore
MoveTypeLostScoreOnTime
MoveTypeUnset
)
// Move is a move. It can have a score, position, equity, etc. It doesn't
// have to be a scoring move.
type Move struct {
// The ordering here should only be changed if it makes the structure smaller.
// This Move should be kept as small as possible.
tiles tilemapping.MachineWord
leave tilemapping.MachineWord
score int
rowStart int
colStart int
tilesPlayed int
equity float64
action MoveType
vertical bool
alph *tilemapping.TileMapping
}
var reVertical, reHorizontal *regexp.Regexp
func init() {
reVertical = regexp.MustCompile(`^(?P<col>[A-Za-z])(?P<row>[0-9]+)$`)
reHorizontal = regexp.MustCompile(`^(?P<row>[0-9]+)(?P<col>[A-Za-z])$`)
}
func (m *Move) equalPositions(o *Move, alsoCheckTransposition bool) bool {
if m.rowStart == o.rowStart && m.colStart == o.colStart && m.vertical == o.vertical {
return true
}
if alsoCheckTransposition {
if m.rowStart == o.colStart && m.colStart == o.rowStart && m.vertical != o.vertical {
return true
}
}
return false
}
func (m *Move) Equals(o *Move, alsoCheckTransposition, ignoreLeave bool) bool {
if m.score != o.score {
return false
}
if m.action != o.action {
return false
}
if m.tilesPlayed != o.tilesPlayed {
return false
}
if !m.equalPositions(o, alsoCheckTransposition) {
return false
}
if len(m.tiles) != len(o.tiles) {
return false
}
if !ignoreLeave && len(m.leave) != len(o.leave) {
return false
}
for idx, i := range m.tiles {
if o.tiles[idx] != i {
return false
}
}
if !ignoreLeave {
for idx, i := range m.leave {
if o.leave[idx] != i {
return false
}
}
}
return true
}
func (m *Move) Set(tiles tilemapping.MachineWord, leave tilemapping.MachineWord, score int,
rowStart, colStart, tilesPlayed int, vertical bool, action MoveType,
alph *tilemapping.TileMapping) {
m.tiles = tiles
m.leave = leave
m.score = score
m.rowStart = rowStart
m.colStart = colStart
m.tilesPlayed = tilesPlayed
m.vertical = vertical
// everything else can be calculated.
m.action = action
m.alph = alph
}
func (m *Move) SetLeave(leave tilemapping.MachineWord) {
m.leave = leave
}
func (m *Move) SetAction(action MoveType) {
m.action = action
}
func (m *Move) SetAlphabet(alph *tilemapping.TileMapping) {
m.alph = alph
}
func (m *Move) SetScore(s int) {
m.score = s
}
// CopyFrom performs a copy of other.
func (m *Move) CopyFrom(other *Move) {
m.action = other.action
if cap(m.tiles) < len(other.tiles) {
m.tiles = make([]tilemapping.MachineLetter, len(other.tiles))
}
m.tiles = m.tiles[:len(other.tiles)]
if cap(m.leave) < len(other.leave) {
m.leave = make([]tilemapping.MachineLetter, len(other.leave))
}
m.leave = m.leave[:len(other.leave)]
copy(m.tiles, other.tiles)
copy(m.leave, other.leave)
m.alph = other.alph
m.score = other.score
m.rowStart = other.rowStart
m.colStart = other.colStart
m.tilesPlayed = other.tilesPlayed
m.vertical = other.vertical
m.equity = other.equity
}
// String provides a string just for debugging purposes.
func (m *Move) String() string {
switch m.action {
case MoveTypePlay:
return fmt.Sprintf(
"<%p action: play word: %v %v score: %v tp: %v leave: %v equity: %.3f>",
m,
m.BoardCoords(), m.TilesString(), m.score,
m.tilesPlayed, m.LeaveString(), m.equity)
case MoveTypePass:
return fmt.Sprintf("<%p action: pass leave: %v equity: %.3f>",
m,
m.LeaveString(), m.equity)
case MoveTypeExchange:
return fmt.Sprintf(
"<%p action: exchange %v score: %v tp: %v leave: %v equity: %.3f>",
m,
m.TilesStringExchange(), m.score, m.tilesPlayed,
m.LeaveString(), m.equity)
case MoveTypeChallenge:
return fmt.Sprintf("<%p action: challenge leave: %v equity: %.3f>",
m,
m.LeaveString(), m.equity)
}
return "<Unhandled move>"
}
func (m *Move) SetEmpty() {
m.action = MoveTypeUnset
}
func (m *Move) MoveTypeString() string {
// Return the moveype as a string
switch m.action {
case MoveTypePlay:
return "Play"
case MoveTypePass:
return "Pass"
case MoveTypeExchange:
return "Exchange"
case MoveTypeChallenge:
return "Challenge"
}
return fmt.Sprint("UNHANDLED")
}
func (m *Move) TilesString() string {
return m.tiles.UserVisiblePlayedTiles(m.alph)
}
func (m *Move) TilesStringExchange() string {
return m.tiles.UserVisible(m.alph)
}
func (m *Move) LeaveString() string {
return m.leave.UserVisible(m.alph)
}
// ShortDescription provides a short description, useful for logging or
// user display.
func (m *Move) ShortDescription() string {
switch m.action {
case MoveTypePlay:
return fmt.Sprintf("%3v %s", m.BoardCoords(), m.TilesString())
case MoveTypePass:
return "(Pass)"
case MoveTypeExchange:
return fmt.Sprintf("(exch %s)", m.TilesStringExchange())
case MoveTypeChallenge:
return "(Challenge!)"
}
return fmt.Sprint("UNHANDLED")
}
// FullRack returns the entire rack that the move was made from. This
// can be calculated from the tiles it uses and the leave.
func (m *Move) FullRack() string {
rack := []tilemapping.MachineLetter{}
for _, ml := range m.tiles {
switch {
case ml.IsBlanked():
rack = append(rack, 0)
case ml == 0:
if m.action == MoveTypeExchange {
// Only if you exchange the blank
rack = append(rack, 0)
}
// Otherwise, don't add this to the rack representation. It
// is a played-through marker.
default:
rack = append(rack, ml)
}
}
for _, ml := range m.leave {
rack = append(rack, ml)
}
sort.Slice(rack, func(i, j int) bool {
return rack[i] < rack[j]
})
return tilemapping.MachineWord(rack).UserVisible(m.Alphabet())
}
func (m *Move) Action() MoveType {
return m.action
}
// TilesPlayed returns the number of tiles played by this move.
func (m *Move) TilesPlayed() int {
return m.tilesPlayed
}
func (m *Move) BingoPlayed() bool {
return (m.action == MoveTypePlay) && (m.tilesPlayed == 7)
}
// NewScoringMove creates a scoring *Move and returns it.
func NewScoringMove(score int, tiles tilemapping.MachineWord,
leave tilemapping.MachineWord, vertical bool, tilesPlayed int,
alph *tilemapping.TileMapping, rowStart int, colStart int) *Move {
move := &Move{
action: MoveTypePlay, score: score, tiles: tiles, leave: leave, vertical: vertical,
tilesPlayed: tilesPlayed, alph: alph,
rowStart: rowStart, colStart: colStart,
}
return move
}
// NewScoringMoveSimple takes in user-visible strings. Consider moving to this
// (it is a little slower, though, so maybe only for tests)
func NewScoringMoveSimple(score int, coords string, word string, leave string,
alph *tilemapping.TileMapping) *Move {
row, col, vertical := FromBoardGameCoords(coords)
tiles, err := tilemapping.ToMachineWord(word, alph)
if err != nil {
log.Error().Err(err).Msg("")
return nil
}
leaveMW, err := tilemapping.ToMachineWord(leave, alph)
if err != nil {
log.Error().Err(err).Msg("")
return nil
}
tilesPlayed := 0
for _, t := range tiles {
if t != 0 {
tilesPlayed++
}
}
move := &Move{
action: MoveTypePlay,
score: score,
tiles: tiles,
leave: leaveMW,
vertical: vertical,
tilesPlayed: tilesPlayed,
alph: alph,
rowStart: row,
colStart: col,
}
return move
}
// NewExchangeMove creates an exchange.
func NewExchangeMove(tiles tilemapping.MachineWord, leave tilemapping.MachineWord,
alph *tilemapping.TileMapping) *Move {
move := &Move{
action: MoveTypeExchange,
score: 0,
tiles: tiles,
leave: leave,
tilesPlayed: len(tiles), // tiles exchanged, really..
alph: alph,
}
return move
}
func NewBonusScoreMove(t MoveType, tiles tilemapping.MachineWord, score int) *Move {
move := &Move{
action: t,
score: score,
tiles: tiles,
}
return move
}
func NewLostScoreMove(t MoveType, rack tilemapping.MachineWord, score int) *Move {
move := &Move{
action: t,
tiles: rack,
score: -score,
}
return move
}
func NewUnsuccessfulChallengePassMove(leave tilemapping.MachineWord, alph *tilemapping.TileMapping) *Move {
return &Move{
action: MoveTypeUnsuccessfulChallengePass,
leave: leave,
alph: alph,
}
}
// Alphabet is the alphabet used by this move
func (m *Move) Alphabet() *tilemapping.TileMapping {
return m.alph
}
// Equity is the equity of this move.
func (m *Move) Equity() float64 {
return m.equity
}
// SetEquity sets the equity of this move. It is calculated outside this package.
func (m *Move) SetEquity(e float64) {
m.equity = e
}
func (m *Move) Score() int {
return m.score
}
func (m *Move) Leave() tilemapping.MachineWord {
return m.leave
}
func (m *Move) Tiles() tilemapping.MachineWord {
return m.tiles
}
func (m *Move) PlayLength() int {
return len(m.tiles)
}
func (m *Move) CoordsAndVertical() (int, int, bool) {
return m.rowStart, m.colStart, m.vertical
}
func (m *Move) BoardCoords() string {
return ToBoardGameCoords(m.rowStart, m.colStart, m.vertical)
}
// ToBoardGameCoords onverts the row, col, and orientation of the play to
// a coordinate like 5F or G4.
func ToBoardGameCoords(row int, col int, vertical bool) string {
colCoords := string(rune('A' + col))
rowCoords := strconv.Itoa(int(row + 1))
var coords string
if vertical {
coords = colCoords + rowCoords
} else {
coords = rowCoords + colCoords
}
return coords
}
// FromBoardGameCoords does the inverse operation of ToBoardGameCoords above.
func FromBoardGameCoords(c string) (int, int, bool) {
vMatches := reVertical.FindStringSubmatch(c)
var row, col int
var vertical bool
if len(vMatches) == 3 {
// It's vertical
row, _ = strconv.Atoi(vMatches[2])
col = int(strings.ToUpper(vMatches[1])[0] - 'A')
vertical = true
return row - 1, col, vertical
}
hMatches := reHorizontal.FindStringSubmatch(c)
if len(hMatches) == 3 {
row, _ = strconv.Atoi(hMatches[1])
col = int(strings.ToUpper(hMatches[2])[0] - 'A')
vertical = false
return row - 1, col, vertical
}
return 0, 0, false
}
// NewPassMove creates a pass with the given leave.
func NewPassMove(leave tilemapping.MachineWord, alph *tilemapping.TileMapping) *Move {
return &Move{
action: MoveTypePass,
leave: leave,
alph: alph,
}
}
// NewChallengeMove creates a challenge with the given leave.
func NewChallengeMove(leave tilemapping.MachineWord, alph *tilemapping.TileMapping) *Move {
return &Move{
action: MoveTypeChallenge,
leave: leave,
alph: alph,
}
}
func MinimallyEqual(m1 *Move, m2 *Move) bool {
if m1.Action() != m2.Action() {
return false
}
if m1.TilesPlayed() != m2.TilesPlayed() {
return false
}
if len(m1.Tiles()) != len(m2.Tiles()) {
return false
}
r1, c1, v1 := m1.CoordsAndVertical()
r2, c2, v2 := m2.CoordsAndVertical()
if r1 != r2 || c1 != c2 || v1 != v2 {
return false
}
for idx, i := range m1.Tiles() {
if m2.Tiles()[idx] != i {
return false
}
}
return true
}