diff options
author | pommicket <pommicket@gmail.com> | 2025-08-08 18:20:43 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2025-08-08 18:20:43 -0400 |
commit | 4c9d6ed3024d7869c427a7f6bd2f4f61feba027d (patch) | |
tree | 825cb60768ad68870140eb7981d9080b174e5981 | |
parent | 551c51c02649aeb38d72002d6bb70b58dbee1eb2 (diff) |
start extracting plays
-rw-r--r-- | quacker/macondo.cpp | 18 | ||||
-rw-r--r-- | quacker/macondo.h | 7 | ||||
-rw-r--r-- | quacker/macondobackend.cpp | 73 | ||||
-rw-r--r-- | quacker/macondobackend.h | 22 |
4 files changed, 70 insertions, 50 deletions
diff --git a/quacker/macondo.cpp b/quacker/macondo.cpp index 83e4553..cb65110 100644 --- a/quacker/macondo.cpp +++ b/quacker/macondo.cpp @@ -4,10 +4,8 @@ #include <QGridLayout> #include <QPushButton> -#include <QTimer> Macondo::Macondo(Quackle::Game *game) : QWidget() { - m_updateTimer = new QTimer(this); QGridLayout *layout = new QGridLayout(this); m_simulateButton = new QPushButton(tr("Simulate")); layout->addWidget(m_simulateButton, 0, 0); @@ -19,25 +17,9 @@ Macondo::Macondo(Quackle::Game *game) : QWidget() { MacondoBackend::InitOptions initOptions(execPath); m_backend = new MacondoBackend(game, initOptions); connect(m_simulateButton, SIGNAL(clicked()), this, SLOT(simulate())); - connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(updateResults())); - m_updateTimer->setInterval(1000); } void Macondo::simulate() { MacondoBackend::SimulateOptions options; m_backend->simulate(options); - m_updateTimer->start(); -} - -void Macondo::updateResults() { - switch (m_backend->command()) { - case MacondoCommand::None: - break; - case MacondoCommand::Simulate: - m_backend->getSimResults(); - break; - case MacondoCommand::Solve: - // TODO - break; - } } diff --git a/quacker/macondo.h b/quacker/macondo.h index 77af35f..d8b2eb1 100644 --- a/quacker/macondo.h +++ b/quacker/macondo.h @@ -1,3 +1,6 @@ +#ifndef MACONDO_H +#define MACONDO_H + #include <QWidget> class QPushButton; @@ -13,7 +16,6 @@ public: Macondo(Quackle::Game *); public slots: void simulate(); - void updateResults(); private: enum class Command { None, @@ -21,8 +23,9 @@ private: Solve, }; QPushButton *m_simulateButton; - QTimer *m_updateTimer; MacondoBackend *m_backend; int m_viewingPlyNumber = 0; Command m_command = Command::None; }; + +#endif diff --git a/quacker/macondobackend.cpp b/quacker/macondobackend.cpp index e172b9d..83fddad 100644 --- a/quacker/macondobackend.cpp +++ b/quacker/macondobackend.cpp @@ -4,6 +4,7 @@ #include <QFile> #include <QProcess> +#include <QTimer> #include <QTextStream> #include <random> @@ -26,29 +27,71 @@ static int getPlyNumber(const Quackle::GamePosition &position) { 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; + 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 = MacondoCommand::Simulate; + m_command = Command::Simulate; +} + +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"); + } + { + QByteArray playsStartIdentifier("Play Leave Score Win% Equity"); + QByteArray playsEndIdentifier("Iterations:"); + int start = m_processOutput.indexOf(playsStartIdentifier) + playsStartIdentifier.length(); + if (start < 0) return; + // trim whitespace before plays + while (start < m_processOutput.length() + && strchr(" \r\n", m_processOutput[start])) { + start++; + } + int end = m_processOutput.indexOf(playsEndIdentifier, start); + if (end < 0) return; + std::string plays(m_processOutput.constData() + start, end - start); + m_processOutput.remove(0, end); + printf("%s\n",plays.c_str()); + } + break; + case Command::Solve: + // TODO + break; + } } void MacondoBackend::processStarted() { loadGCG(); switch (m_command) { - case MacondoCommand::None: + case Command::None: throw "process started with no command"; - case MacondoCommand::Simulate: + case Command::Simulate: m_process->write("gen\nsim\n"); m_runningSimulation = true; break; - case MacondoCommand::Solve: + case Command::Solve: // TODO break; } @@ -85,26 +128,16 @@ void MacondoBackend::loadGCG() { void MacondoBackend::killProcess() { if (m_process) { m_process->kill(); + m_process->deleteLater(); + m_process = nullptr; } } void MacondoBackend::processFinished(int, QProcess::ExitStatus) { - delete m_process; - m_process = nullptr; -} - -std::string MacondoBackend::getSimResults() { - if (m_runningSimulation) { - m_process->write("sim show\n"); - } - QByteArray data = m_process->readAllStandardError(); - fprintf(stderr,"%s",data.constData()); - data = m_process->readAllStandardOutput(); - printf("%s", data.constData()); - fflush(stdout); - return ""; } MacondoBackend::~MacondoBackend() { - killProcess(); + if (m_process) { + m_process->kill(); + } } diff --git a/quacker/macondobackend.h b/quacker/macondobackend.h index dfda0c5..c8e07fa 100644 --- a/quacker/macondobackend.h +++ b/quacker/macondobackend.h @@ -1,18 +1,13 @@ -#ifndef MACONDO_BACKEND_H_ -#define MACONDO_BACKEND_H_ +#ifndef MACONDO_BACKEND_H +#define MACONDO_BACKEND_H -#include <QObject> #include <QProcess> namespace Quackle { class Game; } -enum class MacondoCommand { - None, - Simulate, - Solve, -}; +class QTimer; class MacondoBackend: public QObject { Q_OBJECT @@ -30,19 +25,26 @@ public: void simulate(const SimulateOptions &); ~MacondoBackend(); std::string getSimResults(); - inline MacondoCommand command() const { return m_command; } protected slots: void processStarted(); void processFinished(int, QProcess::ExitStatus); + void timer(); private: + enum class Command { + None, + Simulate, + Solve, + }; void loadGCG(); void killProcess(); std::string m_execPath; std::string m_tempGCG; QProcess *m_process = nullptr; + QTimer *m_updateTimer = nullptr; bool m_runningSimulation = false; Quackle::Game *m_game; - MacondoCommand m_command = MacondoCommand::None; + QByteArray m_processOutput; + Command m_command = Command::None; }; #endif |