diff --git a/gamestate.cpp b/gamestate.cpp index 169c9b7..0446936 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -48,6 +48,9 @@ void GameState::dealCards() m_foundation.clear(); m_throwawayPile.clear(); + // Note that we don't need to reset gameWon from here, as it's + // auto-checked from onFoundationChanged, which the emits trigger + emit drawPileChanged(); emit throwawayPileChanged(); emit columnsChanged(); @@ -165,6 +168,28 @@ bool GameState::moveColumnCardToFoundation(int columnId, PlayingCard::Suit found } +void GameState::onFoundationChanged() +{ + // Check if the game is won (can only happen on a foundation pile change) + bool gameWon = true; + for (const QList &foundationPile : std::as_const(m_foundation)) { + // The piles need to contain all 13 card values each, otherwise the game isn't won + if (foundationPile.size() != 13) { + gameWon = false; + break; + } + } + + if (gameWon == m_gameWon) + return; + + if (gameWon) + qDebug() << "The game was won!"; + + m_gameWon = gameWon; + emit gameWonChanged(); +} + bool GameState::tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove) { assert(foundationId >= PlayingCard::Suit::Clubs && foundationId < PlayingCard::Suit::Spades); @@ -234,3 +259,8 @@ QList > GameState::foundation() const { return m_foundation; } + +bool GameState::gameWon() const +{ + return m_gameWon; +} diff --git a/gamestate.h b/gamestate.h index 944814c..8ed45c8 100644 --- a/gamestate.h +++ b/gamestate.h @@ -12,6 +12,7 @@ class GameState : public QObject Q_PROPERTY(QList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) Q_PROPERTY(QList> columns READ columns NOTIFY columnsChanged) Q_PROPERTY(QList> foundation READ foundation NOTIFY foundationChanged) + Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged) public: explicit GameState(QObject *parent = nullptr); @@ -20,6 +21,7 @@ public: QList throwawayPile() const; QList> columns() const; QList> foundation() const; + bool gameWon() const; void dealCards(); void drawNextCard(); @@ -32,12 +34,17 @@ signals: void throwawayPileChanged(); void columnsChanged(); void foundationChanged(); + void gameWonChanged(); + +private slots: + void onFoundationChanged(); private: QList m_drawPile; QList m_throwawayPile; QList> m_columns; QList> m_foundation; + bool m_gameWon; bool tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove); void ensureColumnRevealed(int columnId);