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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
/*
This file is part of Leela Zero.
Copyright (C) 2017-2018 Marco Calignano
Leela Zero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Leela Zero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QUuid>
#include <QFile>
#include <QTextStream>
#include <QRegularExpression>
#include <QFileInfo>
#include "Game.h"
Game::Game(const Engine& engine) :
QProcess(),
m_engine(engine),
m_isHandicap(false),
m_resignation(false),
m_blackToMove(true),
m_blackResigned(false),
m_passes(0),
m_moveNum(0)
{
m_fileName = QUuid::createUuid().toRfc4122().toHex();
}
bool Game::checkGameEnd() {
return (m_resignation ||
m_passes > 1 ||
m_moveNum > (19 * 19 * 2));
}
void Game::error(int errnum) {
QTextStream(stdout) << "*ERROR*: ";
switch (errnum) {
case Game::NO_LEELAZ:
QTextStream(stdout)
<< "No 'leelaz' binary found." << endl;
break;
case Game::PROCESS_DIED:
QTextStream(stdout)
<< "The 'leelaz' process died unexpected." << endl;
break;
case Game::WRONG_GTP:
QTextStream(stdout)
<< "Error in GTP response." << endl;
break;
case Game::LAUNCH_FAILURE:
QTextStream(stdout)
<< "Could not talk to engine after launching." << endl;
break;
default:
QTextStream(stdout)
<< "Unexpected error." << endl;
break;
}
}
bool Game::eatNewLine() {
char readBuffer[256];
// Eat double newline from GTP protocol
if (!waitReady()) {
error(Game::PROCESS_DIED);
return false;
}
auto readCount = readLine(readBuffer, 256);
if (readCount < 0) {
error(Game::WRONG_GTP);
return false;
}
return true;
}
bool Game::sendGtpCommand(QString cmd) {
write(qPrintable(cmd.append("\n")));
waitForBytesWritten(-1);
if (!waitReady()) {
error(Game::PROCESS_DIED);
return false;
}
char readBuffer[256];
int readCount = readLine(readBuffer, 256);
if (readCount <= 0 || readBuffer[0] != '=') {
QTextStream(stdout) << "GTP: " << readBuffer << endl;
error(Game::WRONG_GTP);
return false;
}
if (!eatNewLine()) {
error(Game::PROCESS_DIED);
return false;
}
return true;
}
void Game::checkVersion(const VersionTuple &min_version) {
write(qPrintable("version\n"));
waitForBytesWritten(-1);
if (!waitReady()) {
error(Game::LAUNCH_FAILURE);
exit(EXIT_FAILURE);
}
char readBuffer[256];
int readCount = readLine(readBuffer, 256);
//If it is a GTP comment just print it and wait for the real answer
//this happens with the winogard tuning
if (readBuffer[0] == '#') {
readBuffer[readCount-1] = 0;
QTextStream(stdout) << readBuffer << endl;
if (!waitReady()) {
error(Game::PROCESS_DIED);
exit(EXIT_FAILURE);
}
readCount = readLine(readBuffer, 256);
}
// We expect to read at last "=, space, something"
if (readCount <= 3 || readBuffer[0] != '=') {
QTextStream(stdout) << "GTP: " << readBuffer << endl;
error(Game::WRONG_GTP);
exit(EXIT_FAILURE);
}
QString version_buff(&readBuffer[2]);
version_buff = version_buff.simplified();
QStringList version_list = version_buff.split(".");
if (version_list.size() < 2) {
QTextStream(stdout)
<< "Unexpected Leela Zero version: " << version_buff << endl;
exit(EXIT_FAILURE);
}
if (version_list.size() < 3) {
version_list.append("0");
}
int versionCount = (version_list[0].toInt() - std::get<0>(min_version)) * 10000;
versionCount += (version_list[1].toInt() - std::get<1>(min_version)) * 100;
versionCount += version_list[2].toInt() - std::get<2>(min_version);
if (versionCount < 0) {
QTextStream(stdout)
<< "Leela version is too old, saw " << version_buff
<< " but expected "
<< std::get<0>(min_version) << "."
<< std::get<1>(min_version) << "."
<< std::get<2>(min_version) << endl;
QTextStream(stdout)
<< "Check https://github.com/gcp/leela-zero for updates." << endl;
exit(EXIT_FAILURE);
}
if (!eatNewLine()) {
error(Game::WRONG_GTP);
exit(EXIT_FAILURE);
}
}
bool Game::gameStart(const VersionTuple &min_version,
const QString &sgf,
const int moves) {
start(m_engine.getCmdLine());
if (!waitForStarted()) {
error(Game::NO_LEELAZ);
return false;
}
// This either succeeds or we exit immediately, so no need to
// check any return values.
checkVersion(min_version);
QTextStream(stdout) << "Engine has started." << endl;
//If there is an sgf file to start playing from then it will contain
//whether there is handicap in use. If there is no sgf file then instead,
//check whether there are any handicap commands to send (these fail
//if the board is not empty).
//Then send the rest of the GTP commands after any SGF has been loaded so
//that they can override any settings loaded from the SGF.
if (!sgf.isEmpty()) {
QFile sgfFile(sgf + ".sgf");
if (!sgfFile.exists()) {
QTextStream(stdout) << "Cannot find sgf file " << sgf << endl;
exit(EXIT_FAILURE);
}
sgfFile.open(QIODevice::Text | QIODevice::ReadOnly);
const auto sgfData = QTextStream(&sgfFile).readAll();
const auto re = QRegularExpression("HA\\[\\d+\\]");
const auto match = re.match(sgfData);
m_isHandicap = match.hasMatch();
sgfFile.close();
if (moves == 0) {
loadSgf(sgf);
} else {
loadSgf(sgf, moves);
}
setMovesCount(moves);
} else {
for (auto command : m_engine.m_commands.filter("handicap")) {
QTextStream(stdout) << command << endl;
if (!sendGtpCommand(command))
{
QTextStream(stdout) << "GTP failed on: " << command << endl;
exit(EXIT_FAILURE);
}
m_isHandicap = true;
m_blackToMove = false;
}
}
const auto re = QRegularExpression("^((?!handicap).)*$");
for (auto command : m_engine.m_commands.filter(re)) {
QTextStream(stdout) << command << endl;
if (!sendGtpCommand(command))
{
QTextStream(stdout) << "GTP failed on: " << command << endl;
exit(EXIT_FAILURE);
}
}
QTextStream(stdout) << "Starting GTP commands sent." << endl;
return true;
}
void Game::move() {
m_moveNum++;
QString moveCmd;
if (m_blackToMove) {
moveCmd = "genmove b\n";
} else {
moveCmd = "genmove w\n";
}
write(qPrintable(moveCmd));
waitForBytesWritten(-1);
}
void Game::setMovesCount(int moves) {
m_moveNum = moves;
//The game always starts at move 0 (GTP states that handicap stones are not part
//of the move history), so if there is no handicap then black moves on even
//numbered turns but if there is handicap then black moves on odd numbered turns.
m_blackToMove = (moves % 2) == (m_isHandicap ? 1 : 0);
}
bool Game::waitReady() {
while (!canReadLine() && state() == QProcess::Running) {
waitForReadyRead(-1);
}
// somebody crashed
if (state() != QProcess::Running) {
return false;
}
return true;
}
bool Game::readMove() {
char readBuffer[256];
int readCount = readLine(readBuffer, 256);
if (readCount <= 3 || readBuffer[0] != '=') {
error(Game::WRONG_GTP);
QTextStream(stdout) << "Error read " << readCount << " '";
QTextStream(stdout) << readBuffer << "'" << endl;
terminate();
return false;
}
// Skip "= "
m_moveDone = readBuffer;
m_moveDone.remove(0, 2);
m_moveDone = m_moveDone.simplified();
if (!eatNewLine()) {
error(Game::PROCESS_DIED);
return false;
}
if (readCount == 0) {
error(Game::WRONG_GTP);
}
QTextStream(stdout) << m_moveNum << " (";
QTextStream(stdout) << (m_blackToMove ? "B " : "W ") << m_moveDone << ") ";
QTextStream(stdout).flush();
if (m_moveDone.compare(QStringLiteral("pass"),
Qt::CaseInsensitive) == 0) {
m_passes++;
} else if (m_moveDone.compare(QStringLiteral("resign"),
Qt::CaseInsensitive) == 0) {
m_resignation = true;
m_blackResigned = m_blackToMove;
} else {
m_passes = 0;
}
return true;
}
bool Game::setMove(const QString& m) {
if (!sendGtpCommand(m)) {
return false;
}
m_moveNum++;
QStringList moves = m.split(" ");
if (moves.at(2)
.compare(QStringLiteral("pass"), Qt::CaseInsensitive) == 0) {
m_passes++;
} else if (moves.at(2)
.compare(QStringLiteral("resign"), Qt::CaseInsensitive) == 0) {
m_resignation = true;
m_blackResigned = (moves.at(1).compare(QStringLiteral("black"), Qt::CaseInsensitive) == 0);
} else {
m_passes = 0;
}
m_blackToMove = !m_blackToMove;
return true;
}
bool Game::nextMove() {
if (checkGameEnd()) {
return false;
}
m_blackToMove = !m_blackToMove;
return true;
}
bool Game::getScore() {
if (m_resignation) {
if (m_blackResigned) {
m_winner = QString(QStringLiteral("white"));
m_result = "W+Resign ";
QTextStream(stdout) << "Score: " << m_result << endl;
} else {
m_winner = QString(QStringLiteral("black"));
m_result = "B+Resign ";
QTextStream(stdout) << "Score: " << m_result << endl;
}
} else{
write("final_score\n");
waitForBytesWritten(-1);
if (!waitReady()) {
error(Game::PROCESS_DIED);
return false;
}
char readBuffer[256];
readLine(readBuffer, 256);
m_result = readBuffer;
m_result.remove(0, 2);
if (readBuffer[2] == 'W') {
m_winner = QString(QStringLiteral("white"));
} else if (readBuffer[2] == 'B') {
m_winner = QString(QStringLiteral("black"));
}
if (!eatNewLine()) {
error(Game::PROCESS_DIED);
return false;
}
QTextStream(stdout) << "Score: " << m_result;
}
if (m_winner.isNull()) {
QTextStream(stdout) << "No winner found" << endl;
return false;
}
QTextStream(stdout) << "Winner: " << m_winner << endl;
return true;
}
int Game::getWinner() {
if (m_winner.compare(QStringLiteral("white"), Qt::CaseInsensitive) == 0)
return Game::WHITE;
else
return Game::BLACK;
}
bool Game::writeSgf() {
return sendGtpCommand(qPrintable("printsgf " + m_fileName + ".sgf"));
}
bool Game::loadTraining(const QString &fileName) {
QTextStream(stdout) << "Loading " << fileName + ".train" << endl;
return sendGtpCommand(qPrintable("load_training " + fileName + ".train"));
}
bool Game::saveTraining() {
QTextStream(stdout) << "Saving " << m_fileName + ".train" << endl;
return sendGtpCommand(qPrintable("save_training " + m_fileName + ".train"));
}
bool Game::loadSgf(const QString &fileName) {
QTextStream(stdout) << "Loading " << fileName + ".sgf" << endl;
return sendGtpCommand(qPrintable("loadsgf " + fileName + ".sgf"));
}
bool Game::loadSgf(const QString &fileName, const int moves) {
QTextStream(stdout) << "Loading " << fileName + ".sgf with " << moves << " moves" << endl;
return sendGtpCommand(qPrintable("loadsgf " + fileName + ".sgf " + QString::number(moves + 1)));
}
void Game::fixSgfPlayer(QString& sgfData, const Engine& whiteEngine) {
QRegularExpression oldPlayer("PW\\[Human\\]");
QString playerName("PB[Leela Zero ");
QRegularExpression le("PB\\[Leela Zero \\S+ ");
QRegularExpressionMatch match = le.match(sgfData);
if (match.hasMatch()) {
playerName = match.captured(0);
}
playerName = "PW" + playerName.remove(0, 2);
playerName += whiteEngine.getNetworkFile().left(8);
playerName += "]";
sgfData.replace(oldPlayer, playerName);
}
void Game::fixSgfComment(QString& sgfData, const Engine& whiteEngine,
const bool isSelfPlay) {
QRegularExpression oldComment("(C\\[Leela Zero)( options:.*)\\]");
QString comment("\\1");
if (!isSelfPlay) {
comment += " Black";
}
comment += "\\2 Starting GTP commands:";
for (const auto command : m_engine.m_commands) {
comment += " " + command;
}
if (!isSelfPlay) {
comment += " White options:";
comment += whiteEngine.m_options + " " + whiteEngine.m_network;
comment += " Starting GTP commands:";
for (const auto command : whiteEngine.m_commands) {
comment += " " + command;
}
}
comment += "]";
comment.replace(QRegularExpression("\\s\\s+"), " ");
sgfData.replace(oldComment, comment);
}
void Game::fixSgfResult(QString& sgfData, const bool resignation) {
if (resignation) {
QRegularExpression oldResult("RE\\[B\\+.*\\]");
QString newResult("RE[B+Resign] ");
sgfData.replace(oldResult, newResult);
if (!sgfData.contains(newResult, Qt::CaseInsensitive)) {
QRegularExpression oldwResult("RE\\[W\\+.*\\]");
sgfData.replace(oldwResult, newResult);
}
QRegularExpression lastpass(";W\\[tt\\]\\)");
QString noPass(")");
sgfData.replace(lastpass, noPass);
}
}
bool Game::fixSgf(const Engine& whiteEngine, const bool resignation,
const bool isSelfPlay) {
QFile sgfFile(m_fileName + ".sgf");
if (!sgfFile.open(QIODevice::Text | QIODevice::ReadOnly)) {
return false;
}
QString sgfData = sgfFile.readAll();
fixSgfPlayer(sgfData, whiteEngine);
fixSgfComment(sgfData, whiteEngine, isSelfPlay);
fixSgfResult(sgfData, resignation);
sgfFile.close();
if (sgfFile.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&sgfFile);
out << sgfData;
}
sgfFile.close();
return true;
}
bool Game::dumpTraining() {
return sendGtpCommand(
qPrintable("dump_training " + m_winner + " " + m_fileName + ".txt"));
}
bool Game::dumpDebug() {
return sendGtpCommand(
qPrintable("dump_debug " + m_fileName + ".debug.txt"));
}
void Game::gameQuit() {
write(qPrintable("quit\n"));
waitForFinished(-1);
}
|