forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.cc
More file actions
99 lines (87 loc) · 2.76 KB
/
Copy pathtest_utils.cc
File metadata and controls
99 lines (87 loc) · 2.76 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
// 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/test_utils.h"
#include <utility>
#include <vector>
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "cc/constants.h"
#include "cc/logging.h"
namespace minigo {
namespace {
// Splits a simple board representation into multiple lines, stripping
// whitespace. Lines are padded with '.' to ensure a kN * kN board.
std::vector<std::string> SplitBoardString(absl::string_view str) {
std::vector<std::string> lines;
for (const auto& line : absl::StrSplit(str, '\n')) {
std::string stripped(absl::StripAsciiWhitespace(line));
if (stripped.empty()) {
continue;
}
MG_CHECK(stripped.size() <= kN);
stripped.resize(kN, '.');
lines.push_back(std::move(stripped));
}
MG_CHECK(lines.size() <= kN);
while (lines.size() < kN) {
lines.emplace_back(kN, '.');
}
return lines;
}
} // namespace
std::string CleanBoardString(absl::string_view str) {
return absl::StrJoin(SplitBoardString(str), "\n");
}
TestablePosition::TestablePosition(absl::string_view board_str, Color to_play,
int n)
: Position(&board_visitor, &group_visitor, to_play, n) {
auto stones = ParseBoard(board_str);
for (int i = 0; i < kN * kN; ++i) {
if (stones[i] != Color::kEmpty) {
AddStoneToBoard(i, stones[i]);
}
}
}
std::array<Color, kN * kN> ParseBoard(absl::string_view str) {
std::array<Color, kN * kN> result;
auto lines = SplitBoardString(str);
for (int row = 0; row < kN; ++row) {
for (int col = 0; col < kN; ++col) {
Coord c(row, col);
if (lines[row][col] == 'X') {
result[c] = Color::kBlack;
} else if (lines[row][col] == 'O') {
result[c] = Color::kWhite;
} else {
result[c] = Color::kEmpty;
}
}
}
return result;
}
int CountPendingVirtualLosses(const MctsNode* node) {
int num = 0;
std::vector<const MctsNode*> pending{node};
while (!pending.empty()) {
node = pending.back();
pending.pop_back();
MG_CHECK(node->num_virtual_losses_applied >= 0);
num += node->num_virtual_losses_applied;
for (const auto& p : node->children) {
pending.push_back(p.second.get());
}
}
return num;
}
} // namespace minigo