-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.go
423 lines (387 loc) · 10.7 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
417
418
419
420
421
422
423
// board.go
// Copyright (C) 2024 Vilhjálmur Þorsteinsson / Miðeind ehf.
// This file implements Board, Square and Tile structs
// and their associated operations
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package skrafl
import (
"fmt"
"strings"
)
const zero = int('0')
// BoardSize is the size of the Board
const BoardSize = 15
// Word multiplication factors on a standard board
var WORD_MULTIPLIERS_STANDARD = [BoardSize]string{
"311111131111113",
"121111111111121",
"112111111111211",
"111211111112111",
"111121111121111",
"111111111111111",
"111111111111111",
"311111121111113",
"111111111111111",
"111111111111111",
"111121111121111",
"111211111112111",
"112111111111211",
"121111111111121",
"311111131111113",
}
// Letter multiplication factors on a standard board
var LETTER_MULTIPLIERS_STANDARD = [BoardSize]string{
"111211111112111",
"111113111311111",
"111111212111111",
"211111121111112",
"111111111111111",
"131113111311131",
"112111212111211",
"111211111112111",
"112111212111211",
"131113111311131",
"111111111111111",
"211111121111112",
"111111212111111",
"111113111311111",
"111211111112111",
}
// Word multiplication factors on an Explo board
var WORD_MULTIPLIERS_EXPLO = [BoardSize]string{
"311111131111113",
"111111112111111",
"111111111211111",
"111211111111111",
"111121111111111",
"111112111111211",
"111111211111121",
"311111121111113",
"121111112111111",
"112111111211111",
"111111111121111",
"111111111112111",
"111112111111111",
"111111211111111",
"311111131111113",
}
// Letter multiplication factors on an Explo board
var LETTER_MULTIPLIERS_EXPLO = [BoardSize]string{
"111121111112111",
"131112111111131",
"112111311111211",
"111111121131112",
"211111111113111",
"121111111211111",
"113111112111111",
"111211111112111",
"111111211111311",
"111112111111121",
"111311111111112",
"211131121111111",
"112111113111211",
"131111111211131",
"111211111121111",
}
// colIds are the column identifiers of a board
var colIds = [BoardSize]string{
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
}
// rowIds are the row identifiers of a board
var rowIds = [BoardSize]string{
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
}
// Board represents the board as a matrix of Squares,
// and caches an adjacency matrix for each Square,
// consisting of pointers to adjacent Squares
type Board struct {
Type string // 'standard' or 'explo'
Squares [BoardSize][BoardSize]Square
Adjacents [BoardSize][BoardSize]AdjSquares
// The number of tiles on the board
NumTiles int
}
// Indices into AdjSquares
const (
ABOVE = 0
LEFT = 1
RIGHT = 2
BELOW = 3
)
// AdjSquares is a list of four Square pointers,
// with a nil if the corresponding adjacent Square does not exist
type AdjSquares [4]*Square
// Tile is a tile from the Bag
type Tile struct {
Letter rune
Meaning rune // Meaning of blank tile (if Letter=='?')
Score int // The nominal score of the tile
PlayedBy int // Which player played the tile
}
// Square is a Board square or Rack slot that can hold a Tile
type Square struct {
Tile *Tile
LetterMultiplier int
WordMultiplier int
Row int // Board row 0..14, or -1 if rack square
Col int // Board column 0..14, or rack square 0..6
}
// String represents a Square as a string. An empty
// Square is indicated by a dot ('.').
func (square *Square) String() string {
if square.Tile == nil {
// Empty square
return "."
}
if square.Tile.Letter == '?' && square.Row >= 0 {
// Blank tile on the board: show its meaning
return string(square.Tile.Meaning)
}
// If a blank tile is in the rack, show '?'
return string(square.Tile.Letter)
}
// String represents a Tile as a string
func (tile *Tile) String() string {
if tile == nil {
return "."
}
return string(tile.Letter)
}
// Return the coordinate of the start square for this board type
func (board *Board) StartSquare() Coordinate {
if board == nil || board.Type == "standard" {
return Coordinate{7, 7} // H8
} else {
return Coordinate{3, 3} // D4
}
}
// Return true if the board has a tile in the start square
func (board *Board) HasStartTile() bool {
startSquare := board.StartSquare()
sq := board.Sq(startSquare.Row, startSquare.Col)
return sq != nil && sq.Tile != nil
}
// Sq returns a pointer to a Board square
func (board *Board) Sq(row, col int) *Square {
if board == nil || row < 0 || row >= BoardSize ||
col < 0 || col >= BoardSize {
return nil
}
return &board.Squares[row][col]
}
// TileAt returns a pointer to the Tile in a given Square
func (board *Board) TileAt(row, col int) *Tile {
if board == nil || row < 0 || row >= BoardSize ||
col < 0 || col >= BoardSize {
return nil
}
return board.Squares[row][col].Tile
}
// Place a tile in a board square, if it is empty
func (board *Board) PlaceTile(row, col int, tile *Tile) bool {
sq := board.Sq(row, col)
if sq == nil {
return false
}
sq.Tile = tile
board.NumTiles++
return true
}
// String represents a Board as a string
func (board *Board) String() string {
var sb strings.Builder
sb.WriteString(" ")
for i := 0; i < BoardSize; i++ {
// Print the column id right-justified in a 2-character field,
// plus a space, making the column 3 characters wide
sb.WriteString(fmt.Sprintf("%2s ", colIds[i]))
}
sb.WriteString("\n")
for i := 0; i < BoardSize; i++ {
sb.WriteString(fmt.Sprintf("%s ", rowIds[i]))
for j := 0; j < BoardSize; j++ {
sb.WriteString(fmt.Sprintf(" %v ", board.Sq(i, j)))
}
sb.WriteString("\n")
}
return sb.String()
}
// NumAdjacentTiles returns the number of tiles on the
// Board that are adjacent to the given coordinate
func (board *Board) NumAdjacentTiles(row, col int) int {
adj := &board.Adjacents[row][col]
var count = 0
for _, sq := range adj {
if sq != nil && sq.Tile != nil {
count++
}
}
return count
}
// Fragment returns a list of the tiles that extend from the square
// at row, col in the direction specified (ABOVE/BELOW/LEFT/RIGHT).
func (board *Board) Fragment(row, col int, direction int) []*Tile {
if row < 0 || col < 0 || row >= BoardSize || col >= BoardSize {
return nil
}
if direction < ABOVE || direction > BELOW {
return nil
}
frag := make([]*Tile, 0, BoardSize-1)
for {
sq := board.Adjacents[row][col][direction]
if sq == nil || sq.Tile == nil {
break
}
frag = append(frag, sq.Tile)
row, col = sq.Row, sq.Col
}
return frag
}
// WordFragment returns the word formed by the tile sequence emanating
// from the given square in the indicated direction, not including the
// square itself.
func (board *Board) WordFragment(row, col int, direction int) (result []rune) {
frag := board.Fragment(row, col, direction)
lenFrag := len(frag)
r := make([]rune, lenFrag)
if direction == LEFT || direction == ABOVE {
// We need to reverse the order of the fragment
for ix, tile := range frag {
// Assign the tile meaning to r in reverse order
r[lenFrag-1-ix] = tile.Meaning
}
} else {
// The fragment is in correct reading order
for ix, tile := range frag {
// Append the tile's meaning to the result
r[ix] = tile.Meaning
}
}
return r
}
// CrossScore returns the sum of the scores of the tiles crossing
// the given tile, either horizontally or vertically. If there are no
// crossings, returns false, 0. (Note that true, 0 is a valid return
// value, if a crossing has only blank tiles.)
func (board *Board) CrossScore(row, col int, horizontal bool) (hasCrossing bool, score int) {
var direction int
// The C ternary operator is sorely missed :-(
if horizontal {
direction = LEFT
} else {
direction = ABOVE
}
for _, tile := range board.Fragment(row, col, direction) {
score += tile.Score
hasCrossing = true
}
if horizontal {
direction = RIGHT
} else {
direction = BELOW
}
for _, tile := range board.Fragment(row, col, direction) {
score += tile.Score
hasCrossing = true
}
return // hasCrossing, score
}
// CrossWords returns the word fragments above and below, or to the left and right of, the
// given co-ordinate on the board.
func (board *Board) CrossWords(row, col int, horizontal bool) (left, right []rune) {
var direction int
// The C ternary operator is sorely missed :-(
if horizontal {
direction = LEFT
} else {
direction = ABOVE
}
hfrag := board.Fragment(row, col, direction)
lenHfrag := len(hfrag)
left = make([]rune, lenHfrag)
for ix, tile := range hfrag {
// Assign the tile meaning to left in reverse order
left[lenHfrag-1-ix] = tile.Meaning
}
if horizontal {
direction = RIGHT
} else {
direction = BELOW
}
vfrag := board.Fragment(row, col, direction)
lenVfrag := len(vfrag)
right = make([]rune, lenVfrag)
for ix, tile := range vfrag {
// Assign the tile meaning to right in forward order
right[ix] = tile.Meaning
}
return // left, right
}
// Init initializes an empty board
func (board *Board) Init(boardType string) {
// Select the correct multipliers for the board type
var letterMultipliers *[BoardSize]string
var wordMultipliers *[BoardSize]string
if boardType == "standard" {
letterMultipliers = &LETTER_MULTIPLIERS_STANDARD
wordMultipliers = &WORD_MULTIPLIERS_STANDARD
} else if boardType == "explo" {
letterMultipliers = &LETTER_MULTIPLIERS_EXPLO
wordMultipliers = &WORD_MULTIPLIERS_EXPLO
} else {
panic(fmt.Sprintf("Unknown board type: %s", boardType))
}
board.Type = boardType
for i := 0; i < BoardSize; i++ {
for j := 0; j < BoardSize; j++ {
sq := board.Sq(i, j)
sq.Row = i
sq.Col = j
sq.LetterMultiplier = int(letterMultipliers[i][j]) - zero
sq.WordMultiplier = int(wordMultipliers[i][j]) - zero
}
}
// Initialize the cached matrix of adjacent square lists
for row := 0; row < BoardSize; row++ {
for col := 0; col < BoardSize; col++ {
var adj = &board.Adjacents[row][col]
if row > 0 {
// Square above
adj[ABOVE] = board.Sq(row-1, col)
}
if row < BoardSize-1 {
// Square below
adj[BELOW] = board.Sq(row+1, col)
}
if col > 0 {
// Square to the left
adj[LEFT] = board.Sq(row, col-1)
}
if col < BoardSize-1 {
// Square to the right
adj[RIGHT] = board.Sq(row, col+1)
}
}
}
}
func NewBoard(boardType string) *Board {
board := &Board{}
board.Init(boardType)
return board
}