Add a quick function to set up a winning deck

This commit is contained in:
ItsDrike 2024-12-04 19:06:39 +01:00
parent aa611b81ed
commit 492f8cfec4
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
3 changed files with 44 additions and 0 deletions

View file

@ -13,6 +13,7 @@ GameState::GameState(QObject *parent)
void GameState::dealCards() void GameState::dealCards()
{ {
// BUG: This causes a memory leak when called again
qDebug() << "Dealing cards"; qDebug() << "Dealing cards";
QList<PlayingCard*> deck = PlayingCard::createDeck(); QList<PlayingCard*> deck = PlayingCard::createDeck();
@ -56,6 +57,47 @@ void GameState::dealCards()
emit foundationChanged(); 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<PlayingCard*> deck = PlayingCard::createDeck();
// Setup the foundation with all cards except one per suit
for (int suit = 0; suit < 4; ++suit) {
QList<PlayingCard*> 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<ColumnSlot*> 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() void GameState::drawNextCard()
{ {
qDebug() << "Drawing next card."; qDebug() << "Drawing next card.";

View file

@ -29,6 +29,7 @@ public:
// General functions // General functions
Q_INVOKABLE void dealCards(); Q_INVOKABLE void dealCards();
Q_INVOKABLE void setupWinningDeck();
Q_INVOKABLE void drawNextCard(); Q_INVOKABLE void drawNextCard();
// Manual moves (from X to Y) // Manual moves (from X to Y)

View file

@ -16,6 +16,7 @@ int main(int argc, char *argv[])
Qt::QueuedConnection); Qt::QueuedConnection);
auto gameState = engine.singletonInstance<GameState*>("Solitare", "GameState"); auto gameState = engine.singletonInstance<GameState*>("Solitare", "GameState");
gameState->setupWinningDeck();
engine.loadFromModule("Solitare", "Main"); engine.loadFromModule("Solitare", "Main");
return app.exec(); return app.exec();