forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cc
More file actions
154 lines (135 loc) · 4.34 KB
/
Copy pathgame.cc
File metadata and controls
154 lines (135 loc) · 4.34 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
// 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/game.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "cc/logging.h"
namespace minigo {
std::ostream& operator<<(std::ostream& os, const Game::Options& options) {
os << "resign_threshold:" << options.resign_threshold
<< " resign_enabled:" << options.resign_enabled << " komi:" << options.komi
<< " ignore_repeated_moves:" << options.ignore_repeated_moves;
return os;
}
std::string Game::FormatScore(float score) {
return absl::StrFormat("%c+%.1f", score > 0 ? 'B' : 'W', std::abs(score));
}
Game::Game(std::string black_name, std::string white_name,
const Game::Options& options)
: options_(options),
black_name_(std::move(black_name)),
white_name_(std::move(white_name)) {
MG_CHECK(options_.resign_threshold < 0);
}
void Game::NewGame() {
game_over_ = false;
moves_.clear();
comment_.clear();
}
void Game::AddComment(const std::string& comment) {
if (comment_.empty()) {
comment_ = comment;
} else {
absl::StrAppend(&comment_, "\n", comment);
}
}
void Game::AddMove(Color color, Coord c, const Position::Stones& stones,
std::string comment, float Q,
const std::array<float, kNumMoves>& search_pi,
std::vector<std::string> models) {
if (!moves_.empty() && moves_.back()->color == color &&
moves_.back()->c == c) {
MG_CHECK(options_.ignore_repeated_moves)
<< "Repeated call to AddMove with same (color, coord) (" << color
<< ", " << c << ") and ignore_repeated_moves is false";
return;
}
MG_CHECK(!game_over_);
moves_.push_back(absl::make_unique<Move>());
auto* move = moves_.back().get();
move->color = color;
move->c = c;
move->Q = Q;
move->comment = std::move(comment);
move->models = std::move(models);
move->search_pi = search_pi;
move->stones = stones;
}
void Game::UndoMove() {
MG_CHECK(!moves_.empty());
moves_.pop_back();
game_over_ = false;
}
void Game::SetGameOverBecauseOfPasses(float score) {
MG_CHECK(!game_over_);
game_over_ = true;
game_over_reason_ = GameOverReason::kBothPassed;
result_ = score < 0 ? -1 : score > 0 ? 1 : 0;
result_string_ = FormatScore(score);
}
void Game::SetGameOverBecauseOfResign(Color winner) {
MG_CHECK(!game_over_);
game_over_ = true;
game_over_reason_ = GameOverReason::kOpponentResigned;
if (winner == Color::kBlack) {
result_ = 1;
result_string_ = "B+R";
} else {
result_ = -1;
result_string_ = "W+R";
}
}
void Game::SetGameOverBecauseMoveLimitReached(float score) {
MG_CHECK(!game_over_);
game_over_ = true;
game_over_reason_ = GameOverReason::kMoveLimitReached;
result_ = score < 0 ? -1 : score > 0 ? 1 : 0;
result_string_ = FormatScore(score);
}
void Game::GetStoneHistory(
int move, int num_moves,
std::vector<const Position::Stones*>* history) const {
history->clear();
history->reserve(num_moves);
MG_CHECK(move >= 0);
MG_CHECK(move < static_cast<int>(moves_.size()));
for (int i = 0; i < num_moves && move - i >= 0; ++i) {
history->push_back(&moves_[move - i]->stones);
}
}
bool Game::FindBleakestMove(int* move, float* q) const {
if (!game_over_) {
MG_LOG(ERROR) << "game isn't over";
return false;
}
if (options_.resign_enabled || moves_.empty()) {
return false;
}
// Find the move at which the game looked the bleakest from the perspective
// of the winner.
float bleakest_eval = moves_[0]->Q * result_;
size_t bleakest_move = 0;
for (size_t i = 1; i < moves_.size(); ++i) {
float eval = moves_[i]->Q * result_;
if (eval < bleakest_eval) {
bleakest_eval = eval;
bleakest_move = i;
}
}
*move = static_cast<int>(bleakest_move);
*q = bleakest_eval;
return true;
}
} // namespace minigo