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
127 lines (98 loc) · 3.4 KB
/
Copy pathsgf.h
File metadata and controls
127 lines (98 loc) · 3.4 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
// 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 <iostream>
#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/move.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;
absl::string_view id;
std::vector<absl::string_view> 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.
__attribute__((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_;
};
// 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 CreateSgfOptions {
std::string black_name = kProgramIdentifier;
std::string white_name = kProgramIdentifier;
std::string ruleset = "Chinese";
float komi = 7.5;
std::string result;
};
// Returns a valid SGF file for the given move sequence.
std::string CreateSgfString(absl::Span<const MoveWithComment> moves,
const CreateSgfOptions& options);
// Extracts the main line series of moves from a SGF AST tree.
std::vector<Move> GetMainLineMoves(const Ast::Tree& tree);
// Extracts the main line series of moves from the first tree in an SGF file.
// Returns an empty vector if the AST has no trees.
inline std::vector<Move> GetMainLineMoves(const Ast& ast) {
if (ast.trees().empty()) {
return {};
}
return GetMainLineMoves(ast.trees()[0]);
}
} // namespace sgf
} // namespace minigo
#endif // CC_SGF_H_