-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
279 lines (212 loc) · 6.06 KB
/
utils.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
package sgf
import (
"fmt"
"os"
"strconv"
"strings"
)
const alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// NewTree returns a root node for a game of the given board size, with various
// sensible properties.
func NewTree(size int) *Node {
// Creates a new root node with standard properties.
if size < 1 || size > 52 {
panic(fmt.Sprintf("NewTree(): invalid size %v", size))
}
node := NewNode(nil)
node.SetValue("GM", "1")
node.SetValue("FF", "4")
node.SetValue("SZ", strconv.Itoa(size))
return node
}
// AdjacentPoints returns a slice of all points (formatted as SGF coordinates,
// e.g. "dd") that are adjacent to the given point, on the given board size.
func AdjacentPoints(p string, size int) []string {
x, y, onboard := ParsePoint(p, size)
if onboard == false {
return nil
}
var ret []string
if x > 0 {
ret = append(ret, byte_to_string(alpha[x - 1]) + byte_to_string(p[1])) // Left
}
if x < size - 1 {
ret = append(ret, byte_to_string(alpha[x + 1]) + byte_to_string(p[1])) // Right
}
if y > 0 {
ret = append(ret, byte_to_string(p[0]) + byte_to_string(alpha[y - 1])) // Up
}
if y < size - 1 {
ret = append(ret, byte_to_string(p[0]) + byte_to_string(alpha[y + 1])) // Down
}
return ret
}
func byte_to_string(b byte) string { // One cannot do string(b) because string() turns integers into utf-8 strings,
return string([]byte{b}) // therefore if b > 127 it will necessarily return a string of length >= 2
}
// ParsePoint takes an SGF coordinate (e.g. "dd") and a board size, and returns
// the x and y values (zeroth-indexed) of that point, as well as a boolean value
// indicating whether the coordinates were on the board. If they were not, the
// coordinates returned are always -1, -1.
func ParsePoint(p string, size int) (x, y int, onboard bool) {
// e.g. "cd" --> 2,3
// Any string that does not yield an onboard coordinate
// is considered a pass.
if len(p) != 2 {
return -1, -1, false
}
x = -1
y = -1
if p[0] >= 'a' && p[0] <= 'z' { x = int(p[0]) - 97 }
if p[1] >= 'a' && p[1] <= 'z' { y = int(p[1]) - 97 }
if p[0] >= 'A' && p[0] <= 'Z' { x = int(p[0]) - 39 }
if p[1] >= 'A' && p[1] <= 'Z' { y = int(p[1]) - 39 }
onboard = x >= 0 && x < size && y >= 0 && y < size
if onboard == false {
return -1, -1, false
} else {
return x, y, true
}
}
// ValidPoint takes an SGF coordinate (e.g. "dd") and a board size, and returns
// a boolean indicating whether the coordinate is on the board. Internally, the
// library considers all moves that fail this test to be pass-moves.
func ValidPoint(p string, size int) bool {
_, _, onboard := ParsePoint(p, size)
return onboard
}
// Point generates an SGF coordinate (e.g. "dd") from x and y values. The
// arguments are considered zeroth-indexed.
func Point(x, y int) string {
if x < 0 || x >= 52 || y < 0 || y >= 52 {
return ""
}
return byte_to_string(alpha[x]) + byte_to_string(alpha[y])
}
// HandicapPoints returns a slice of SGF coordinates (e.g. "dd") that are
// Black's handicap stones, for the specified board size and handicap (max
// handicap: 9). The tygem argument indicates whether the 3rd stone in an H3
// game should be in the top left. Works poorly for very small board sizes.
func HandicapPoints(size, handicap int, tygem bool) []string {
if size < 4 || handicap < 2 {
return nil
}
if handicap > 9 {
handicap = 9
}
d := 1; if size >= 7 { d = 2 }; if size >= 13 { d = 3 }
z := size
var ret []string
if handicap >= 2 {
ret = append(ret, Point(z - d - 1, d))
ret = append(ret, Point(d, z - d - 1))
}
if handicap >= 3 {
if tygem {
ret = append(ret, Point(d, d))
} else {
ret = append(ret, Point(z - d - 1, z - d - 1))
}
}
if handicap >= 4 {
if tygem {
ret = append(ret, Point(z - d - 1, z - d - 1))
} else {
ret = append(ret, Point(d, d))
}
}
if size % 2 == 0 {
return ret
}
if handicap == 5 || handicap == 7 || handicap == 9 {
ret = append(ret, Point(z / 2, z / 2))
}
if handicap >= 6 {
ret = append(ret, Point(d, z / 2))
ret = append(ret, Point(z - d - 1, z / 2))
}
if handicap >= 8 {
ret = append(ret, Point(z / 2, d))
ret = append(ret, Point(z / 2, z - d - 1))
}
return ret
}
// IsStarPoint takes an SGF coordinate (e.g. "dd") and a board size, and returns
// true if it would be considered a star (hoshi) point.
func IsStarPoint(p string, size int) bool {
starpoints := HandicapPoints(size, 9, false)
for _, hoshi := range starpoints {
if p == hoshi {
return true
}
}
return false
}
// ParsePointList takes an SGF rectangle (e.g. "dd:fg") and a board size, and
// returns a slice containing all points indicated.
func ParsePointList(s string, size int) []string {
if len(s) != 5 || s[2] != ':' {
return nil
}
first := s[:2]
second := s[3:]
x1, y1, onboard1 := ParsePoint(first, size)
x2, y2, onboard2 := ParsePoint(second, size)
if onboard1 == false || onboard2 == false {
return nil
}
if x1 > x2 {
x1, x2 = x2, x1
}
if y1 > y2 {
y1, y2 = y2, y1
}
var ret []string
for x := x1; x <= x2; x++ { // <= is correct
for y := y1; y <= y2; y++ { // <= is correct
ret = append(ret, Point(x, y))
}
}
return ret
}
// ParseGTP takes a GTP formatted string (e.g. "D16") and a board size, and
// returns the SGF coordinate (e.g. "dd") or "" if invalid.
func ParseGTP(s string, size int) string {
if len(s) < 2 || len(s) > 3 {
return ""
}
s = strings.ToUpper(s)
if s[0] < 'A' || s[0] > 'Z' {
return ""
}
if s[1] < '0' || s[1] > '9' {
return ""
}
if len(s) == 3 && (s[2] < '0' || s[2] > '9') {
return ""
}
x := int(s[0]) - 65
if x >= 8 { // Adjust for missing "I"
x--
}
up, _ := strconv.Atoi(s[1:])
y := size - up
if x < 0 || x >= size || y < 0 || y >= size {
return ""
}
return Point(x, y)
}
// LoadArgOrQuit loads the filename given in os.Args[n] and returns the root
// node. If this is not possible, the program exits.
func LoadArgOrQuit(n int) *Node {
if len(os.Args) <= n {
fmt.Print("LoadArgOrQuit(): no such arg\n")
os.Exit(1)
}
node, err := Load(os.Args[n])
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
return node
}