-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.h
133 lines (123 loc) · 3.85 KB
/
Game.h
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
/*
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/>.
*/
#ifndef GAME_H
#define GAME_H
#include <QFileInfo>
#include <QProcess>
#include <tuple>
using VersionTuple = std::tuple<int, int, int>;
class Engine {
public:
Engine(const QString& network,
const QString& options,
const QStringList& commands = QStringList("time_settings 0 1 0"),
const QString& binary = QString("./leelaz")) :
m_binary(binary), m_options(options),
m_network(network), m_commands(commands) {
#ifdef WIN32
m_binary.append(".exe");
#endif
if (!QFileInfo::exists(m_binary)) {
m_binary.remove(0, 2); // ./leelaz -> leelaz
}
}
Engine() = default;
QString getCmdLine(void) const {
return m_binary + " " + m_options + " " + m_network;
}
QString getNetworkFile(void) const {
return QFileInfo(m_network).baseName();
}
QString m_binary;
QString m_options;
QString m_network;
QStringList m_commands;
};
class Game : QProcess {
public:
#if defined(LEELA_GTP)
Game(const Engine& engine, const QString& trainpath = QString("./data/"));
#else
Game(const Engine& engine);
#endif
~Game() = default;
bool gameStart(const VersionTuple& min_version,
const QString &sgf = QString(),
const int moves = 0);
void move();
bool waitForMove() { return waitReady(); }
#if defined(LEELA_GTP)
int readMove();
#else
bool readMove();
#endif
bool nextMove();
bool getScore();
bool loadSgf(const QString &fileName);
bool loadSgf(const QString &fileName, const int moves);
bool writeSgf();
bool loadTraining(const QString &fileName);
bool saveTraining();
bool fixSgf(const Engine& whiteEngine, const bool resignation,
const bool isSelfPlay);
bool dumpTraining();
bool dumpDebug();
void gameQuit();
QString getMove() const { return m_moveDone; }
QString getFile() const { return m_fileName; }
bool setMove(const QString& m);
bool checkGameEnd();
int getWinner();
QString getWinnerName() const { return m_winner; }
int getMovesCount() const { return m_moveNum; }
void setMovesCount(int moves);
int getToMove() const { return m_blackToMove ? BLACK : WHITE; }
QString getResult() const { return m_result.trimmed(); }
enum {
BLACK = 0,
WHITE = 1,
};
private:
enum {
NO_LEELAZ = 1,
PROCESS_DIED,
WRONG_GTP,
LAUNCH_FAILURE
};
Engine m_engine;
QString m_winner;
QString m_fileName;
QString m_moveDone;
QString m_result;
bool m_isHandicap;
bool m_resignation;
bool m_blackToMove;
bool m_blackResigned;
int m_passes;
#if defined(LEELA_GTP)
QString m_traindatapath;
#endif
int m_moveNum;
bool sendGtpCommand(QString cmd);
void checkVersion(const VersionTuple &min_version);
bool waitReady();
bool eatNewLine();
void error(int errnum);
void fixSgfPlayer(QString& sgfData, const Engine& whiteEngine);
void fixSgfComment(QString& sgfData, const Engine& whiteEngine,
const bool isSelfPlay);
void fixSgfResult(QString& sgfData, const bool resignation);
};
#endif /* GAME_H */