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
|
#include "datamanager.h"
#include "macondobackend.h"
#include "quackleio/gcgio.h"
#include "game.h"
#include <QFile>
#include <QProcess>
#include <QTimer>
#include <QTextStream>
#include <random>
using std::string;
static int getPlyNumber(const Quackle::GamePosition &position) {
int playerIndex = 0, numPlayers = position.players().size();
for (const Quackle::Player &player: position.players()) {
if (player.id() == position.playerOnTurn().id()) {
break;
}
playerIndex++;
}
if (playerIndex >= numPlayers) {
throw "couldn't find player in player list";
}
return (position.turnNumber() - 1) * numPlayers
+ playerIndex;
}
MacondoBackend::MacondoBackend(Quackle::Game *game, const InitOptions &options) {
m_execPath = options.execPath;
m_game = game;
m_updateTimer = new QTimer(this);
m_updateTimer->setInterval(1000);
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(timer()));
m_updateTimer->start();
}
void MacondoBackend::simulate(const SimulateOptions &) {
if (m_process) return;
printf("running macondo %s\n", m_execPath.c_str());
m_process = new QProcess(this);
QStringList args;
m_process->start(m_execPath.c_str(), args);
connect(m_process, SIGNAL(started()), this, SLOT(processStarted()));
connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
m_command = Command::Simulate;
}
static string trimLeft(const string &s) {
int i;
for (i = 0; strchr(" \t\r\n", s[i]); i++);
return s.substr(i);
}
static Quackle::Move extractSimMove(const string &s) {
string play = trimLeft(s);
if (play.find("(exch ") == 0) {
// exchange
} else if (strchr("123456789ABCDEFGHIJKLMNO", play[0])) {
// normal play
size_t space = play.find(" ");
if (space == string::npos)
throw "no space after placement";
string placement = play.substr(0, space);
play = trimLeft(play.substr(space));
space = play.find(" ");
if (space == string::npos)
throw "no space after move description";
string description = play.substr(0, space);
Quackle::Move move = Quackle::Move::createPlaceMove(placement, "A");
move.setPrettyTiles(QUACKLE_ALPHABET_PARAMETERS->encode(description.c_str()));
return move;
}
throw "bad syntax";
}
static std::vector<Quackle::Move> extractSimMoves(QByteArray &processOutput) {
std::vector<Quackle::Move> moves;
QByteArray playsStartIdentifier("Play Leave Score Win% Equity");
QByteArray playsEndIdentifier("Iterations:");
int start = processOutput.indexOf(playsStartIdentifier) + playsStartIdentifier.length();
if (start < 0) return moves;
int end = processOutput.indexOf(playsEndIdentifier, start);
if (end < 0) return moves;
string plays(processOutput.constData() + start, end - start);
processOutput.remove(0, end);
plays = trimLeft(plays);
for (size_t i = 0, next; (next = plays.find("\n", i)) != string::npos; i = next + 1) {
string play = plays.substr(i, next - i);
try {
moves.push_back(extractSimMove(play));
} catch (const char *s) {
fprintf(stderr, "WARNING: unrecognized play: %s (%s)\n", play.c_str(), s);
}
}
return moves;
}
void MacondoBackend::timer() {
if (m_process) {
QByteArray data = m_process->readAllStandardError();
fprintf(stderr,"%s",data.constData());
data = m_process->readAllStandardOutput();
m_processOutput.append(data);
fflush(stdout);
}
switch (m_command) {
case Command::None:
break;
case Command::Simulate:
if (m_runningSimulation) {
m_process->write("sim show\n");
}
{
std::vector<Quackle::Move> moves = extractSimMoves(m_processOutput);
if (!moves.empty())
emit gotSimMoves(moves);
}
break;
case Command::Solve:
// TODO
break;
}
}
void MacondoBackend::processStarted() {
loadGCG();
switch (m_command) {
case Command::None:
throw "process started with no command";
case Command::Simulate:
m_process->write("gen\nsim\n");
m_runningSimulation = true;
break;
case Command::Solve:
// TODO
break;
}
}
void MacondoBackend::loadGCG() {
std::random_device randDev;
std::mt19937 rand(randDev());
std::uniform_int_distribution<int> distribution(0, 26);
if (!m_tempGCG.empty()) {
remove(m_tempGCG.c_str());
}
// save game file with random name
char filename[] = "tmpGameXXXXXXXXXXXX.gcg";
for (int i = 0; filename[i]; i++) {
if (filename[i] == 'X') {
filename[i] = distribution(rand) + 'A';
}
}
m_tempGCG = filename;
QuackleIO::GCGIO gcg;
{
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream fileStream(&file);
gcg.write(*m_game, fileStream);
}
std::stringstream commands;
commands << "load " << filename << "\n"
<< "turn " << getPlyNumber(m_game->currentPosition()) << "\n";
m_process->write(commands.str().c_str());
}
void MacondoBackend::killProcess() {
if (m_process) {
m_process->kill();
m_process->deleteLater();
m_process = nullptr;
}
}
void MacondoBackend::processFinished(int, QProcess::ExitStatus) {
}
MacondoBackend::~MacondoBackend() {
if (m_process) {
m_process->kill();
}
}
|