forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoord.cc
More file actions
143 lines (121 loc) · 3.37 KB
/
Copy pathcoord.cc
File metadata and controls
143 lines (121 loc) · 3.37 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
// 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.
#include "cc/coord.h"
#include <utility>
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
namespace minigo {
constexpr uint16_t Coord::kPass;
constexpr uint16_t Coord::kInvalid;
constexpr char Coord::kKgsColumns[];
namespace {
Coord TryParseKgs(absl::string_view str) {
if (str == "pass") {
return Coord::kPass;
}
if (str == "resign") {
return Coord::kResign;
}
auto col_char = str[0];
if (col_char < 'A' || col_char > 'T' || col_char == 'I') {
return Coord::kInvalid;
}
int col = col_char < 'I' ? col_char - 'A' : 8 + col_char - 'J';
auto row_str = str.substr(1);
int row;
if (!absl::SimpleAtoi(row_str, &row) || row <= 0 || row > kN) {
return Coord::kInvalid;
}
return {kN - row, col};
}
Coord TryParseSgf(absl::string_view str) {
if (str.empty()) {
return Coord::kPass;
}
if (str.size() != 2) {
return Coord::kInvalid;
}
int col = str[0] - 'a';
int row = str[1] - 'a';
if (row < 0 || row >= kN || col < 0 || col >= kN) {
return Coord::kInvalid;
}
return {row, col};
}
Coord TryParseString(absl::string_view str) {
auto result = TryParseKgs(str);
if (result != Coord::kInvalid) {
return result;
}
return TryParseSgf(str);
}
} // namespace
Coord Coord::FromKgs(absl::string_view str, bool allow_invalid) {
auto c = TryParseKgs(str);
MG_CHECK(allow_invalid || c != Coord::kInvalid);
return c;
}
Coord Coord::FromSgf(absl::string_view str, bool allow_invalid) {
auto c = TryParseSgf(str);
MG_CHECK(allow_invalid || c != Coord::kInvalid);
return c;
}
Coord Coord::FromString(absl::string_view str, bool allow_invalid) {
auto c = TryParseString(str);
MG_CHECK(allow_invalid || c != Coord::kInvalid);
return c;
}
std::string Coord::ToKgs() const {
if (*this == kPass) {
return "pass";
} else if (*this == kResign) {
return "resign";
} else if (*this == kInvalid) {
return "invalid";
}
int row = value_ / kN;
int col = value_ % kN;
absl::string_view col_str = kKgsColumns;
return absl::StrCat(col_str.substr(col, 1), kN - row);
}
std::string Coord::ToSgf() const {
if (*this == kPass) {
return "";
} else if (*this == kInvalid) {
return "invalid";
}
// We should not be writing resign moves to SGF files.
MG_CHECK(*this != kResign);
int row = value_ / kN;
int col = value_ % kN;
char buffer[2];
buffer[0] = 'a' + col;
buffer[1] = 'a' + row;
return {buffer, 2};
}
std::ostream& operator<<(std::ostream& os, Coord c) {
if (c == Coord::kPass) {
return os << "pass";
} else if (c == Coord::kResign) {
return os << "resign";
} else if (c == Coord::kInvalid) {
return os << "invalid";
} else {
uint16_t value = c;
int row = value / kN;
int col = value % kN;
return os << Coord::kKgsColumns[col] << (kN - row);
}
}
} // namespace minigo