/
game.go
480 lines (427 loc) · 11.9 KB
/
game.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
/*
* File: src/github.com/Ken1JF/sgf/game.go
* Project: abst-hier
*
* Created by Ken Friedenbach on 2/11/2010.
* Copyright 2010-2014 Ken Friedenbach. All rights reserved.
*
* This file implements the data structures for storing a game,
* in a format suitable for reading/writing in .sgf format.
*/
package sgf
import (
"github.com/Ken1JF/ah"
"strconv"
)
// This Komi structure allows the notation KM[?] which indicates that a komi
// was given, but the value is unknown. The default is set == false.
// If KM[xxx] appears in an SGF file, set == true.
// If known == true, then val is the komi value. Otherwise, val is 0.0.
// KM[0] is used to indicate that no komi was given.
type Komi struct {
val float32
set bool
known bool
}
// This Result structure supports the addition of comments in the RE[] field.
// Many of the comments differ only in the separators used,
// or the number of moves known. So this structure allows the specification of
// a number that occurs in the comment, and a left separator: either "{" or "(".
// The boolean "both" indicates that a matching right separator was found.
type Result struct {
val []byte
com []byte
n int
sep byte
both bool
}
// Set functions for SGF properties:
func (gam *GameTree) SetFF(f []byte) {
gam.fF = f
}
func (gam *GameTree) IsFF4() (ret bool) {
if gam.fF == nil {
ret = false
} else {
i, _ := strconv.Atoi(string(gam.fF))
ret = (i == 4)
}
return ret
}
func (gam *GameTree) SetST(s []byte) {
gam.sT = s
}
func (gam *GameTree) SetPB(p []byte) {
gam.pB = p
}
func (gam *GameTree) GetPB() []byte {
return gam.pB
}
func (gam *GameTree) SetBR(p []byte) {
gam.bR = p
}
func (gam *GameTree) GetBR() []byte {
return gam.bR
}
func (gam *GameTree) SetBT(p []byte) {
gam.bT = p
}
func (gam *GameTree) GetPW() []byte {
return gam.pW
}
func (gam *GameTree) SetPW(p []byte) {
gam.pW = p
}
func (gam *GameTree) SetWR(p []byte) {
gam.wR = p
}
func (gam *GameTree) GetWR() []byte {
return gam.wR
}
func (gam *GameTree) SetWT(p []byte) {
gam.wT = p
}
func (gam *GameTree) SetDT(p []byte) {
gam.dT = p
}
func (gam *GameTree) SetPC(p []byte) {
gam.pC = p
}
func (gam *GameTree) SetRU(p []byte) {
gam.rU = p
}
func (gam *GameTree) SetRE(v []byte, b []byte, num int, ch byte, two bool) {
// TODO: set Board value
// set SGF details
gam.rE.val = v
gam.rE.com = b
gam.rE.n = num
gam.rE.sep = ch
gam.rE.both = two
}
func (gam *GameTree) SetGC(p []byte) {
gam.gC = p
}
func (gam *GameTree) SetEV(p []byte) {
gam.eV = p
}
func (gam *GameTree) SetRO(p []byte) {
gam.rO = p
}
func (gam *GameTree) SetAP(p []byte) {
gam.aP = p
}
func (gam *GameTree) SetAN(p []byte) {
gam.aN = p
}
func (gam *GameTree) SetCP(p []byte) {
gam.cP = p
}
func (gam *GameTree) SetSO(p []byte) {
gam.sO = p
}
func (gam *GameTree) SetUS(p []byte) {
gam.uS = p
}
func (gam *GameTree) SetKM(p float32, k bool) {
// Set the Board value
if k {
gam.SetKomi(p)
}
// Set the SGF details
gam.kM.set = true
gam.kM.known = k
if k {
gam.kM.val = p
}
}
func (gam *GameTree) SetTM(p float32) {
gam.tM = p
}
func (gam *GameTree) DoAB(p ah.NodeLoc, doPlay bool) (err ah.ErrorList) {
movType := ah.Unocc
gam.aB = append(gam.aB, p)
bp := &gam.Graphs[ah.PointLevel].Nodes[p]
cur := bp.GetNodeLowState()
if ah.IsOccupied(ah.PointStatus(cur)) {
// TODO: check for white and add AB_B?
movType = ah.AB_W
} else {
movType = ah.AB_U
}
if doPlay {
gam.ChangeNodeState(ah.PointLevel, p, ah.NodeStatus(ah.Black), true)
}
_ = gam.AddMove(p, movType, 0, ah.NilNodeLoc)
if doPlay {
gam.EachAdjNode(ah.PointLevel, p,
func(adjNl ah.NodeLoc) {
newSt := gam.Graphs[ah.PointLevel].CompHigh(&(gam.AbstHier), ah.PointLevel, adjNl, uint16(ah.Unocc))
gam.ChangeNodeState(ah.PointLevel, adjNl, ah.NodeStatus(newSt), true)
})
}
return err
}
func (gam *GameTree) DoAE(p ah.NodeLoc, doPlay bool) (err ah.ErrorList) {
movType := ah.Unocc
// gam.aE = append(gam.aE, p)
bp := &gam.Graphs[ah.PointLevel].Nodes[p]
cur := bp.GetNodeLowState()
if ah.PointStatus(cur) == ah.Black {
movType = ah.AE_B
} else {
// TODO: check for white and add AE_E?
movType = ah.AE_W
}
if doPlay {
newSt := gam.Graphs[ah.PointLevel].CompHigh(&(gam.AbstHier), ah.PointLevel, p, uint16(ah.Unocc))
gam.ChangeNodeState(ah.PointLevel, p, ah.NodeStatus(newSt), true)
}
_ = gam.AddMove(p, movType, 0, ah.NilNodeLoc)
return err
}
func (gam *GameTree) DoAW(p ah.NodeLoc, doPlay bool) (err ah.ErrorList) {
movType := ah.Unocc
gam.aW = append(gam.aW, p)
bp := &gam.Graphs[ah.PointLevel].Nodes[p]
cur := bp.GetNodeLowState()
if ah.IsOccupied(ah.PointStatus(cur)) {
// TODO: check for black and add AW_W?
movType = ah.AW_B
} else {
movType = ah.AW_U
}
if doPlay {
gam.ChangeNodeState(ah.PointLevel, p, ah.NodeStatus(ah.White), true)
}
_ = gam.AddMove(p, movType, 0, ah.NilNodeLoc)
if doPlay {
gam.EachAdjNode(ah.PointLevel, p,
func(adjNl ah.NodeLoc) {
newSt := gam.Graphs[ah.PointLevel].CompHigh(&(gam.AbstHier), ah.PointLevel, adjNl, uint16(ah.Unocc))
gam.ChangeNodeState(ah.PointLevel, adjNl, ah.NodeStatus(newSt), true)
})
}
return err
}
func (gam *GameTree) DoB(nl ah.NodeLoc, doPlay bool) (movN int, err ah.ErrorList) {
movN, err = gam.DoBoardMove(nl, ah.Black, doPlay)
return movN, err
}
func (gam *GameTree) DoW(nl ah.NodeLoc, doPlay bool) (movN int, err ah.ErrorList) {
movN, err = gam.DoBoardMove(nl, ah.White, doPlay)
return movN, err
}
func (gam *GameTree) SetOH(p []byte) {
gam.oH = p
}
func (gam *GameTree) GetOH() []byte {
return gam.oH
}
func (gam *GameTree) SetHA(p int) {
gam.SetHandicap(p)
}
func (gam *GameTree) GetHA() int {
return gam.GetHandicap()
}
// PlaceHandicap sets the handicap stones, and returns the list of points
func (gam *GameTree) PlaceHandicap(n int, siz int) (pts []uint8) {
var lin, mid int
// var nl ah.NodeLoc
// var play bool = true
place := func(c int, r int) {
nl := ah.MakeNodeLoc(ah.ColValue(c), ah.RowValue(r))
gam.DoAB(nl, true)
pts = append(pts, SGFCoords(nl, gam.IsFF4())...)
}
lin = 3
if siz < 13 {
lin = 2
}
mid = (siz - 1) / 2
switch n {
case 0:
case 2:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
case 3:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
case 4:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
place(lin, lin) // UpperLeft
case 5:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
place(lin, lin) // UpperLeft
place(mid, mid) // mid point
case 6:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
place(lin, lin) // UpperLeft
place(lin, mid) // mid point Left side
place(siz-(lin+1), mid) // mid point Right side
case 7:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
place(lin, lin) // UpperLeft
place(lin, mid) // mid point Left side
place(siz-(lin+1), mid) // mid point Right side
place(mid, mid) // mid point
case 8:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
place(lin, lin) // UpperLeft
place(lin, mid) // mid point Left side
place(siz-(lin+1), mid) // mid point Right side
place(mid, lin) // mid point Top side
place(mid, siz-(lin+1)) // mid point Bottom side
case 9:
place(siz-(lin+1), lin) // UpperRight
place(lin, siz-(lin+1)) // LowerLeft
place(siz-(lin+1), siz-(lin+1)) // LowerRight
place(lin, lin) // UpperLeft
place(lin, mid) // mid point Left side
place(siz-(lin+1), mid) // mid point Right side
place(mid, lin) // mid point Top side
place(mid, siz-(lin+1)) // mid point Bottom side
place(mid, mid) // mid point
}
return pts
}
func (gam *GameTree) DoAR(p []byte) {
// TODO: implement Arrows
// string format is:
// t1:h1t2:h2 ... tN:hN
// where ti is the tail of arrow i
// and hi is the head of arrow i
}
func (gam *GameTree) DoLN(p []byte) {
// TODO: implement Lines
// string format is:
// t1:h1t2:h2 ... tN:hN
// where ti is the start of line i
// and hi is the end of line i
}
// CheckProperties is called after parsing an SGF file.
func (gam *GameTree) CheckProperties(gogod bool) (errstr string) {
// Check FF[]
if (gam.fF == nil) && gogod { // treat as an error for GoGoD feedback
errstr = "No FF "
}
// Check SZ[]
cSz, _ := gam.GetSize()
if (cSz == 0) && gogod { // treat as an error for GoGoD feedback
errstr = errstr + "No SZ"
}
// TODO: Check GM[]
// Check HA[] with AB[], AW[], and mov1.
// Assumes B is taking handicap, and W playing first.
// (This logic is complicated by at least one GoGoD game,
// where B gets HA, and makes first move, on another handicap point.
// Check how many, and consider "correcting" the game record(s).)
// TODO: check PL?
handi := gam.GetHandicap()
mov1, set := gam.GetMov1()
if handi > 0 {
if set {
if mov1 == ah.Black {
if handi != (len(gam.aW)-len(gam.aB)) &&
(handi != (len(gam.aB) - len(gam.aW) + 1)) {
errstr = errstr + "HA not equal AW - AB (1.B)"
}
} else { // mov1 == ah.White
if handi != (len(gam.aB) - len(gam.aW)) {
errstr = errstr + "HA not equal AB - AW (1.W)"
}
}
} else { // assume Black to play first
if handi != (len(gam.aB) - len(gam.aW)) {
errstr = errstr + "HA not equal AB - AW (1.B?)"
}
// else ok
}
} else { // hA == 0
if len(gam.aB) != 0 {
if len(gam.aW) != len(gam.aB) {
if set {
if mov1 == ah.Black {
if len(gam.aB)+1 != len(gam.aW) {
errstr = errstr + "AB+1 not equal AW"
}
// else ok
} else { // mov1 == ah.White
if len(gam.aB) != len(gam.aW)+1 {
errstr = errstr + "AB not equal AW + 1"
}
// else ok
}
} else {
errstr = errstr + "AB not equal AW (mov1 not set)"
}
}
} else if len(gam.aW) != 0 {
errstr = errstr + "AW not zero"
}
}
// TODO: check OH (GoGoD specific property) for consistency with HA, and ranks.
// TODO: check RE with evaluation of final position.
return errstr
}
// GetMove returns the move at a node
func (gamT *GameTree) GetMove(n TreeNode) (nl ah.NodeLoc, c ah.PointStatus, err ah.ErrorList) {
if n.TNodType == BlackMoveNode {
nl, c = ah.NodeLoc(n.propListOrNodeLoc), ah.Black
} else if n.TNodType == WhiteMoveNode {
nl, c = ah.NodeLoc(n.propListOrNodeLoc), ah.White
} else if n.TNodType == InteriorNode {
OK := false
lastProp := n.propListOrNodeLoc
if lastProp != nilPropIdx {
pl := gamT.propertyValues[lastProp].NextProp
prop := gamT.propertyValues[pl]
process := func(prop PropertyValue) {
if prop.PropType == B_idx {
OK = true
c = ah.Black
nl, err = SGFPoint(prop.StrValue)
} else if prop.PropType == W_idx {
OK = true
c = ah.White
nl, err = SGFPoint(prop.StrValue)
} else {
pl = prop.NextProp
}
}
// pprocess first prop.
process(prop)
if !OK {
for (pl != lastProp) && (err == nil) {
pl := gamT.propertyValues[pl].NextProp
prop := gamT.propertyValues[pl]
// pprocess next prop.
process(prop)
if OK {
break
}
}
}
}
if !OK {
err.Add(ah.NoPos, "sgf/GetMove: move property not found in interior node")
}
} else {
// TODO: support moves with other properties
nl = ah.IllegalNodeLoc
c = ah.Unocc
err.Add(ah.NoPos, "sgf/GetMove: not a move node or interior node")
}
return nl, c, err
}