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
|
#include "macondo.h"
#include "quacker.h"
#include <QGridLayout>
#include <QPushButton>
#include <QProcess>
#include <QTimer>
#include <random>
Macondo::Macondo(TopLevel *topLevel) : View() {
m_topLevel = topLevel;
m_updateTimer = new QTimer(this);
QGridLayout *layout = new QGridLayout(this);
m_simulateButton = new QPushButton(tr("Simulate"));
layout->addWidget(m_simulateButton, 0, 0);
layout->setAlignment(Qt::AlignTop);
const char *home = getenv("HOME");
// TODO: configurable path
m_execPath = home ? home : "/";
m_execPath += "/apps/macondo/macondo";
connect(m_simulateButton, SIGNAL(clicked()), this, SLOT(simulate()));
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateResults()));
m_updateTimer->setInterval(1000);
}
void Macondo::simulate() {
if (m_process) return;
printf("running macondo %s\n", m_execPath.c_str());
m_process = new QProcess;
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;
}
void Macondo::processStarted() {
m_updateTimer->start();
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 Macondo::killProcess() {
if (m_process) {
m_process->kill();
}
}
void Macondo::processFinished(int, QProcess::ExitStatus) {
delete m_process;
m_process = nullptr;
}
Macondo::~Macondo() {
killProcess();
}
void Macondo::loadGCG() {
std::default_random_engine rand;
std::uniform_int_distribution<int> distribution(0, 26);
// 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_topLevel->writeFile(filename);
std::stringstream commands;
commands << "load " << filename << "\n"
<< "turn " << m_viewingPlyNumber << "\n";
m_process->write(commands.str().c_str());
}
void Macondo::updateResults() {
if (m_runningSimulation) {
m_process->write("sim show\n");
}
QByteArray data = m_process->readAllStandardError();
printf("%s",data.constData());
data = m_process->readAllStandardOutput();
printf("%s", data.constData());
fflush(stdout);
}
void Macondo::positionChanged(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";
}
m_viewingPlyNumber = (position->turnNumber() - 1) * numPlayers
+ playerIndex;
}
|