From 492f8cfec4dbfb9dd9e2ba4f91be5b41e6d6c180 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Wed, 4 Dec 2024 19:06:39 +0100 Subject: [PATCH] Add a quick function to set up a winning deck --- gamestate.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ gamestate.h | 1 + main.cpp | 1 + 3 files changed, 44 insertions(+) diff --git a/gamestate.cpp b/gamestate.cpp index e0a9764..37ef41c 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -13,6 +13,7 @@ GameState::GameState(QObject *parent) void GameState::dealCards() { + // BUG: This causes a memory leak when called again qDebug() << "Dealing cards"; QList deck = PlayingCard::createDeck(); @@ -56,6 +57,47 @@ void GameState::dealCards() emit foundationChanged(); } +void GameState::setupWinningDeck() +{ + // BUG: This causes a memory leak when called again + qDebug() << "Setting up a winning deck"; + + // Create a sorted deck of cards (4 suits, ordered) + QList deck = PlayingCard::createDeck(); + + // Setup the foundation with all cards except one per suit + for (int suit = 0; suit < 4; ++suit) { + QList foundationPile; + for (int rank = 1; rank <= 12; ++rank) { // Leave the King (rank 13) out + foundationPile.prepend(deck[suit * 13 + rank - 1]); + } + m_foundation[suit] = foundationPile; + } + + // The remaining four Kings are placed in the columns + for (int i = 0; i < 4; ++i) { + QList column; + PlayingCard* kingCard = deck[i * 13 + 12]; // King of each suit + ColumnSlot* slot = new ColumnSlot(kingCard, true); // Revealed + column.append(slot); + m_columns[i] = column; + } + + // Ensure other columns are empty + for (int i = 4; i < 7; ++i) { + m_columns[i].clear(); + } + + // The draw pile and throwaway pile are empty + m_drawPile.clear(); + m_throwawayPile.clear(); + + emit drawPileChanged(); + emit throwawayPileChanged(); + emit columnsChanged(); + emit foundationChanged(); +} + void GameState::drawNextCard() { qDebug() << "Drawing next card."; diff --git a/gamestate.h b/gamestate.h index aa9aee7..c313290 100644 --- a/gamestate.h +++ b/gamestate.h @@ -29,6 +29,7 @@ public: // General functions Q_INVOKABLE void dealCards(); + Q_INVOKABLE void setupWinningDeck(); Q_INVOKABLE void drawNextCard(); // Manual moves (from X to Y) diff --git a/main.cpp b/main.cpp index e4da9f6..e892220 100644 --- a/main.cpp +++ b/main.cpp @@ -16,6 +16,7 @@ int main(int argc, char *argv[]) Qt::QueuedConnection); auto gameState = engine.singletonInstance("Solitare", "GameState"); + gameState->setupWinningDeck(); engine.loadFromModule("Solitare", "Main"); return app.exec();