forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgf.h
More file actions
134 lines (103 loc) · 3.58 KB
/
Copy pathsgf.h
File metadata and controls
134 lines (103 loc) · 3.58 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
// 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.
#ifndef CC_SGF_H_
#define CC_SGF_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "cc/color.h"
#include "cc/coord.h"
#include "cc/logging.h"
#include "cc/move.h"
#include "cc/platform/utils.h"
namespace minigo {
namespace sgf {
constexpr char kProgramIdentifier[] = "Minigo";
// Abstract syntax tree for an SGF file.
// The Ast class just holds the structure and contents of the tree and doesn't
// infer any meaning from the property IDs or values.
class Ast {
public:
struct Property {
std::string ToString() const;
std::string id;
std::vector<std::string> values;
};
struct Node {
std::string ToString() const;
const Property* FindProperty(absl::string_view id) const;
std::vector<Property> properties;
};
struct Tree {
std::string ToString() const;
std::vector<Node> nodes;
std::vector<Tree> children;
};
// Parses the SGF file.
MG_WARN_UNUSED_RESULT bool Parse(std::string contents);
// Returns a non-empty string containing error information if the most recent
// call to Parse returned false.
const std::string& error() const { return error_; }
const std::vector<Tree>& trees() const { return trees_; }
private:
std::string error_;
std::vector<Tree> trees_;
std::string contents_;
};
// TODO(tommadams): Replace sgf::MoveWithComment with sgf::Node.
// A single move with a (possibly empty) comment.
struct MoveWithComment {
MoveWithComment() = default;
MoveWithComment(Move move, std::string comment)
: move(move), comment(std::move(comment)) {}
MoveWithComment(Color color, Coord c, std::string comment)
: move(color, c), comment(std::move(comment)) {}
// MoveWithComment is convertible to a Move for ease of use.
operator Move() const { return move; }
Move move;
std::string comment;
bool operator==(const MoveWithComment& other) const {
return move == other.move && comment == other.comment;
}
};
std::ostream& operator<<(std::ostream& ios, const MoveWithComment& move);
struct Node {
Node(Move move, std::string comment)
: move(move), comment(std::move(comment)) {}
// Returns a flattened copy of the main line moves: the chain of moves formed
// by this node and its left-most descendants.
std::vector<Move> ExtractMainLine() const;
const Move move;
std::string comment;
std::vector<std::unique_ptr<Node>> children;
};
struct CreateSgfOptions {
std::string black_name = kProgramIdentifier;
std::string white_name = kProgramIdentifier;
std::string ruleset = "Chinese";
float komi = 7.5;
std::string result;
std::string game_comment;
};
// Returns a valid SGF file for the given move sequence.
std::string CreateSgfString(absl::Span<const MoveWithComment> moves,
const CreateSgfOptions& options);
// Extracts the complete game trees from an SGF AST.
std::vector<std::unique_ptr<Node>> GetTrees(const Ast& ast);
} // namespace sgf
} // namespace minigo
#endif // CC_SGF_H_