forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposition.ts
More file actions
163 lines (139 loc) · 4.65 KB
/
Copy pathposition.ts
File metadata and controls
163 lines (139 loc) · 4.65 KB
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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Color, Move, Nullable, Point, movesEqual, otherColor, stonesEqual, toKgs} from './base'
import {partialUpdate} from './util'
namespace Annotation {
export enum Shape {
Dot,
}
}
interface Annotation {
p: Point;
shape: Annotation.Shape;
colors: string[];
}
class Position {
n = 0;
q: Nullable<number> = null;
moveNum: number;
search: Move[] = [];
// A map of variations.
// The pricinpal variation is keyed by "pv".
// The current tree search is keyed by "search".
// All other variations are keyed by their KGS string.
variations = new Map<string, Move[]>();
annotations: Annotation[] = [];
childN: Nullable<number[]> = null;
childQ: Nullable<number[]> = null;
// children[0] is the main line. Subsequent children are variations.
children: Position[] = [];
// captures[0] is the number of stones that black has captured.
// captures[1] is the number of stones that white has captured.
captures: number[] = [0, 0];
comment = "";
constructor(public id: string,
public parent: Nullable<Position>,
public stones: Color[],
public lastMove: Nullable<Move>,
public toPlay: Color,
public gameOver: boolean,
public isMainLine: boolean) {
this.moveNum = parent != null ? parent.moveNum + 1 : 0;
if (lastMove != null && lastMove != 'pass' && lastMove != 'resign') {
this.annotations.push({
p: lastMove,
shape: Annotation.Shape.Dot,
colors: ['#ef6c02'],
});
}
}
addChild(id: string, move: Move, stones: Color[], gameOver: boolean) {
// If the position already has a child with the given move, verify that the
// stones are equal and return the existing child.
for (let child of this.children) {
if (child.lastMove == null) {
throw new Error('Child node shouldn\'t have a null lastMove');
}
if (movesEqual(child.lastMove, move)) {
if (!stonesEqual(stones, child.stones)) {
throw new Error(`Position has child ${toKgs(move)} with different stones`);
}
return child;
}
}
// Create a new child.
let isMainLine = this.isMainLine && this.children.length == 0;
let child = new Position(id, this, stones, move, otherColor(this.toPlay),
gameOver, isMainLine);
this.children.push(child);
return child;
}
update(update: Position.Update) {
// Update simple properties.
const props = ['n', 'q', 'childN', 'childQ'];
partialUpdate(update, this, props);
// Variations need special handling.
if (update.variations != null) {
for (let key in update.variations) {
this.variations.set(key, update.variations[key]);
}
// If the update has a principal variation also update the variation
// at it's first move.
if ("pv" in update.variations) {
let pv = update.variations["pv"];
if (pv.length > 0) {
this.variations.set(toKgs(pv[0]), pv);
}
}
}
}
// Returns a copy of the complete variation that this position is a part of,
// starting from the root, down to the last move in the line.
// The varation of the root node is its main line.
getFullLine() {
// Get all ancestors.
let result: Position[] = [];
let node: Nullable<Position>
for (node = this.parent; node != null; node = node.parent) {
result.push(node);
}
result.reverse();
// Append descendants.
for (node = this; node != null; node = node.children[0]) {
result.push(node);
}
return result;
}
}
namespace Position {
export interface Update {
// Number of reads under this position.
n?: number;
// This position's Q score.
q?: number;
// A map of variations.
// The pricinpal variation is keyed by "pv".
// The current tree search is keyed by "search".
// All other variations are keyed by their KGS string.
variations?: {[index: string]: Move[]};
// Child visit counts.
childN?: number[];
// Child Q scores.
childQ?: number[];
}
}
export {
Annotation,
Position,
};