-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.go
186 lines (150 loc) · 4.47 KB
/
tree.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
package sgf
import (
"fmt"
"strconv"
)
// GetRoot travels up the tree, examining each node's parent until it finds the
// root node, which it returns.
func (self *Node) GetRoot() *Node {
node := self
for node.parent != nil {
node = node.parent
}
return node
}
// GetEnd travels down the tree from the node, until it reaches a node with zero
// children. It returns that node. Note that, if GetEnd is called on a node that
// is not on the main line, the result will not be on the main line either, but
// will instead be the end of the current branch.
func (self *Node) GetEnd() *Node {
node := self
for len(node.children) > 0 {
node = node.children[0]
}
return node
}
// GetLine returns a list of all nodes between the root and the node, inclusive.
func (self *Node) GetLine() []*Node {
var ret []*Node
node := self
for node != nil {
ret = append(ret, node)
node = node.parent
}
// Reverse the slice...
for left, right := 0, len(ret) - 1; left < right; left, right = left + 1, right - 1 {
ret[left], ret[right] = ret[right], ret[left]
}
return ret
}
// MakeMainLine adjusts the tree structure so that the main line leads to this
// node.
func (self *Node) MakeMainLine() {
node := self
for node.parent != nil {
for i, sibling := range node.parent.children {
if sibling == node {
node.parent.children[i] = node.parent.children[0]
node.parent.children[0] = node
break
}
}
node = node.parent
}
}
// SubtreeSize returns the number of nodes in a node's subtree, including
// itself.
func (self *Node) SubtreeSize() int {
count := 1
for _, child := range self.children {
count += child.SubtreeSize()
}
return count
}
// TreeSize returns the number of nodes in the whole tree.
func (self *Node) TreeSize() int {
return self.GetRoot().SubtreeSize()
}
// SubtreeNodes returns a slice of every node in a node's subtree, including
// itself.
func (self *Node) SubtreeNodes() []*Node {
ret := []*Node{self}
for _, child := range self.children {
ret = append(ret, child.SubtreeNodes()...)
}
return ret
}
// TreeNodes returns a slice of every node in the whole tree.
func (self *Node) TreeNodes() []*Node {
return self.GetRoot().SubtreeNodes()
}
// SubTreeKeyValueCount returns the number of keys and values in a node's
// subtree, including itself.
func (self *Node) SubTreeKeyValueCount() (int, int) {
keys := self.KeyCount()
vals := 0
for _, key := range self.AllKeys() {
vals += self.ValueCount(key)
}
for _, child := range self.children {
k, v := child.SubTreeKeyValueCount()
keys += k; vals += v
}
return keys, vals
}
// TreeKeyValueCount returns the number of keys and values in the whole tree.
func (self *Node) TreeKeyValueCount() (int, int) {
return self.GetRoot().SubTreeKeyValueCount()
}
// RootBoardSize travels up the tree to the root, and then finds the board size,
// which it returns as an integer. If no SZ property is present, it returns 19.
func (self *Node) RootBoardSize() int {
root := self.GetRoot()
sz_string, _ := root.GetValue("SZ")
sz, _ := strconv.Atoi(sz_string)
if sz < 1 { return 19 }
if sz > 52 { return 52 } // SGF limit
return sz
}
// RootHandicap travels up the tree to the root, and then finds the handicap,
// which it returns as an integer. If no HA property is present, it returns 0.
func (self *Node) RootHandicap() int {
root := self.GetRoot()
ha_string, _ := root.GetValue("HA")
ha, _ := strconv.Atoi(ha_string)
return ha
}
// RootKomi travels up the tree to the root, and then finds the komi, which it
// returns as a float64. If no KM property is present, it returns 0.
func (self *Node) RootKomi() float64 {
root := self.GetRoot()
km_string, _ := root.GetValue("KM")
km, _ := strconv.ParseFloat(km_string, 64)
return km
}
// Dyer returns the Dyer Signature of the entire tree.
func (self *Node) Dyer() string {
vals := map[int]string{20: "??", 40: "??", 60: "??", 31: "??", 51: "??", 71: "??"}
move_count := 0
node := self.GetRoot()
size := node.RootBoardSize()
for {
for _, key := range []string{"B", "W"} {
mv, ok := node.GetValue(key) // Assuming just 1, as per SGF specs.
if ok {
move_count++
if move_count == 20 || move_count == 40 || move_count == 60 ||
move_count == 31 || move_count == 51 || move_count == 71 {
if ValidPoint(mv, size) {
vals[move_count] = mv
}
}
}
}
node = node.MainChild()
if node == nil || move_count > 71 {
break
}
}
return fmt.Sprintf("%s%s%s%s%s%s", vals[20], vals[40], vals[60], vals[31], vals[51], vals[71])
}