Keep track of gameWon state

This commit is contained in:
ItsDrike 2024-12-03 01:33:12 +01:00
parent b558a3396e
commit a1d676779d
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
2 changed files with 37 additions and 0 deletions

View file

@ -48,6 +48,9 @@ void GameState::dealCards()
m_foundation.clear(); m_foundation.clear();
m_throwawayPile.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 drawPileChanged();
emit throwawayPileChanged(); emit throwawayPileChanged();
emit columnsChanged(); 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<PlayingCard*> &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) bool GameState::tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove)
{ {
assert(foundationId >= PlayingCard::Suit::Clubs && foundationId < PlayingCard::Suit::Spades); assert(foundationId >= PlayingCard::Suit::Clubs && foundationId < PlayingCard::Suit::Spades);
@ -234,3 +259,8 @@ QList<QList<PlayingCard *> > GameState::foundation() const
{ {
return m_foundation; return m_foundation;
} }
bool GameState::gameWon() const
{
return m_gameWon;
}

View file

@ -12,6 +12,7 @@ class GameState : public QObject
Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged) Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged)
Q_PROPERTY(QList<QList<PlayingCard*>> foundation READ foundation NOTIFY foundationChanged) Q_PROPERTY(QList<QList<PlayingCard*>> foundation READ foundation NOTIFY foundationChanged)
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
public: public:
explicit GameState(QObject *parent = nullptr); explicit GameState(QObject *parent = nullptr);
@ -20,6 +21,7 @@ public:
QList<PlayingCard*> throwawayPile() const; QList<PlayingCard*> throwawayPile() const;
QList<QList<ColumnSlot*>> columns() const; QList<QList<ColumnSlot*>> columns() const;
QList<QList<PlayingCard*>> foundation() const; QList<QList<PlayingCard*>> foundation() const;
bool gameWon() const;
void dealCards(); void dealCards();
void drawNextCard(); void drawNextCard();
@ -32,12 +34,17 @@ signals:
void throwawayPileChanged(); void throwawayPileChanged();
void columnsChanged(); void columnsChanged();
void foundationChanged(); void foundationChanged();
void gameWonChanged();
private slots:
void onFoundationChanged();
private: private:
QList<PlayingCard*> m_drawPile; QList<PlayingCard*> m_drawPile;
QList<PlayingCard*> m_throwawayPile; QList<PlayingCard*> m_throwawayPile;
QList<QList<ColumnSlot*>> m_columns; QList<QList<ColumnSlot*>> m_columns;
QList<QList<PlayingCard*>> m_foundation; QList<QList<PlayingCard*>> m_foundation;
bool m_gameWon;
bool tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove); bool tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingCard* cardToMove);
void ensureColumnRevealed(int columnId); void ensureColumnRevealed(int columnId);