From b0630c269029e670bd7baaa37d536b98138f4f6b Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Mon, 9 Dec 2024 19:34:27 +0100 Subject: [PATCH] Store isWinnable as QVariant QML doesn't natively support the complex type returned from isWinnable property (`std::pair, int>`), instead use QVariantMap to implement custom attributes and return as QVariant. --- src/gamestate.cpp | 14 ++++++++++++-- src/gamestate.h | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/gamestate.cpp b/src/gamestate.cpp index 088b295..789e99e 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -879,8 +879,18 @@ bool GameState::gameWon() const { return m_gameWon; } -std::pair, int> GameState::isWinnable() const { - return m_isWinnable; +QVariant GameState::isWinnable() const { + QVariantMap map; + if (m_isWinnable.first.has_value()) { + map["winnable"] = QVariant(m_isWinnable.first.value()); + } else { + // bit annoyingly, in QML, this will map to undefined, not null + // there doesn't seem to be an easy way to specify that this is null from C++ + map["winnable"] = QVariant(); + } + map["optionalBool"] = m_isWinnable.first.has_value() ? QVariant(m_isWinnable.first.value()) : QVariant::fromValue(QVariant()); + map["depth"] = m_isWinnable.second; + return map; } std::pair, int> GameState::checkWinnable() { diff --git a/src/gamestate.h b/src/gamestate.h index a3e2f1c..a27e5ad 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -10,6 +10,7 @@ #include #include #include +#include // Limits for checking winnability #define MAX_EVAL_TIME 100 // Evaluation time limit (ms) @@ -25,7 +26,7 @@ class GameState : public QObject { Q_PROPERTY(int moveAmount READ moveAmount NOTIFY moveAmountChanged) Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged) Q_PROPERTY(bool preliminaryWin READ preliminaryWin NOTIFY preliminaryWinChanged) - Q_PROPERTY(std::pair, int> isWinnable READ isWinnable NOTIFY isWinnableChanged) + Q_PROPERTY(QVariant isWinnable READ isWinnable NOTIFY isWinnableChanged) public: explicit GameState(QObject* parent = nullptr, bool preDealCards = true, bool enableWinnabilitySim = true); @@ -39,7 +40,7 @@ class GameState : public QObject { int moveAmount() const; bool preliminaryWin() const; bool gameWon() const; - std::pair, int> isWinnable() const; + QVariant isWinnable() const; // General functions Q_INVOKABLE void dealCards();