diff --git a/gamestate.cpp b/gamestate.cpp index 3a19370..fa40b5a 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -18,6 +18,8 @@ GameState::GameState(QObject *parent) void GameState::dealCards() { + qDebug() << "Dealing cards"; + QList deck = PlayingCard::createDeck(); // Randomly shuffle the deck @@ -60,15 +62,23 @@ void GameState::dealCards() void GameState::drawNextCard() { + qDebug() << "Drawing next card."; + // If drawPile is empty, flip the throwawayPile to drawPile if (m_drawPile.isEmpty()) { + if (m_throwawayPile.isEmpty()) { + qWarning() << "Drawing a card failed, no more cards to draw from"; + return; + } m_drawPile = m_throwawayPile; m_throwawayPile.clear(); std::reverse(m_drawPile.begin(), m_drawPile.end()); + qDebug() << "> Draw pile empty, flipping throwaway pile"; } // Draw the top card from drawPile, dropping it into throwawayPile m_throwawayPile.append(m_drawPile.takeFirst()); + qDebug() << "> Drawn card: " << m_throwawayPile.last()->toString(); emit drawPileChanged(); emit throwawayPileChanged(); @@ -85,14 +95,18 @@ bool GameState::moveCardToColumn(int columnId) // We'll be moving the last card in the throwaway pile (maybe) PlayingCard *cardToMove = m_throwawayPile.last(); + qDebug() << "Attempting to move thrown card " << cardToMove->toString() << " to column " << columnId; - if (!isMoveToColumnLegal(cardToMove, columnId)) + if (!isMoveToColumnLegal(cardToMove, columnId)) { + qDebug() << "> Moving aborted, illegal move"; return false; + } ColumnSlot *col = new ColumnSlot(cardToMove, true); m_columns[columnId].append(col); m_throwawayPile.removeLast(); ensureColumnRevealed(columnId); + qDebug() << "> Moving complete"; emit throwawayPileChanged(); emit columnsChanged(); @@ -108,14 +122,18 @@ bool GameState::moveThrownCardToFoundation(PlayingCard::Suit foundationId) // We'll be moving the last card in the foundation pile (maybe) PlayingCard *cardToMove = m_throwawayPile.last(); + qDebug() << "Attempting to move thrown card " << cardToMove->toString() << " to foundation " << foundationId; // Try moving the card into the foundation - if (!tryMoveCardToFoundation(foundationId, cardToMove)) + if (!tryMoveCardToFoundation(foundationId, cardToMove)) { + qDebug() << "> Moving aborted, illegal move"; return false; + } // We succeeded, the card is now in the appropriate foundation pile, // let's remove the card from the throwaway pile. m_throwawayPile.removeLast(); + qDebug() << "> Moving complete"; emit throwawayPileChanged(); return true; @@ -132,15 +150,19 @@ bool GameState::moveColumnCardToFoundation(int columnId, PlayingCard::Suit found // We'll be moving the last card in the column (maybe) PlayingCard *cardToMove = m_columns[columnId].last()->card(); + qDebug() << "Attempting to move card " << cardToMove->toString() << " from column " << columnId << " to foundation " << foundationId; // Try moving the card into the foundation - if (!tryMoveCardToFoundation(foundationId, cardToMove)) + if (!tryMoveCardToFoundation(foundationId, cardToMove)) { + qDebug() << "> Moving aborted, illegal move"; return false; + } // We succeeded, the card is now in the appropriate foundation pile, // let's remove the column slot. m_columns[columnId].removeLast(); ensureColumnRevealed(columnId); + qDebug() << "> Moving complete"; emit columnsChanged(); return true; @@ -160,14 +182,18 @@ bool GameState::autoMoveThrownCard() // We'll be moving the last card in the foundation pile (maybe) PlayingCard *cardToMove = m_throwawayPile.last(); + qDebug() << "Attempting auto-move of thrown card " << cardToMove->toString(); // Try moving the card into the foundation - if (!tryAutoMoveCard(cardToMove)) + if (!tryAutoMoveCard(cardToMove)) { + qDebug() << "> Moving failed, no available move found"; return false; + } // We succeeded, the card is now in the appropriate foundation pile, // let's remove the card from the throwaway pile. m_throwawayPile.removeLast(); + qDebug() << "> Moving complete"; emit throwawayPileChanged(); return true; @@ -188,14 +214,18 @@ bool GameState::autoMoveColumnCard(int columnId) // We'll be moving the last card in the column (maybe) PlayingCard *cardToMove = m_columns[columnId].last()->card(); + qDebug() << "Attempting auto-move of column card " << cardToMove->toString(); - if (!tryAutoMoveCard(cardToMove)) + if (!tryAutoMoveCard(cardToMove)) { + qDebug() << "> Moving failed, no available move found"; return false; + } // We succeeded, the card is now in the appropriate foundation pile, // let's remove the column slot. m_columns[columnId].removeLast(); ensureColumnRevealed(columnId); + qDebug() << "> Moving complete"; emit columnsChanged(); return true; @@ -227,8 +257,12 @@ bool GameState::tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingC { assert(foundationId >= PlayingCard::Suit::Clubs && foundationId <= PlayingCard::Suit::Spades); - if (cardToMove->suit() != foundationId) + qDebug() << "* Trying to move card " << cardToMove->toString() << " to foundation " << foundationId; + + if (cardToMove->suit() != foundationId) { + qDebug() << "* Move attempt failed (wrong suit)"; return false; + } PlayingCard::Value requiredValue; if (m_foundation[foundationId].isEmpty()) { @@ -237,18 +271,23 @@ bool GameState::tryMoveCardToFoundation(PlayingCard::Suit foundationId, PlayingC } else { // Otherwise it's the next card by value, unless we're already at king PlayingCard::Value curValue = m_foundation[foundationId].first()->value(); - if (curValue == PlayingCard::Value::King) + if (curValue == PlayingCard::Value::King) { + qDebug() << "* Move attempt failed (expected King)"; return false; + } // Clever trick to get the next value. Note that this relies on the enum having // the variants defined in correct order. requiredValue = static_cast(static_cast(curValue) + 1); } - if (cardToMove->value() != requiredValue) + if (cardToMove->value() != requiredValue) { + qDebug() << "* Move attempt failed (expected value: " << requiredValue << ")"; return false; + } m_foundation[foundationId].push_front(cardToMove); + qDebug() << "* Moved card " << cardToMove->toString() << " to foundation " << foundationId; emit foundationChanged(); return true; @@ -258,13 +297,16 @@ bool GameState::tryAutoMoveCard(PlayingCard *cardToMove) { // 1. Try moving the card to the foundation for (PlayingCard::Suit suit : {PlayingCard::Suit::Clubs, PlayingCard::Suit::Diamonds, PlayingCard::Suit::Hearts, PlayingCard::Suit::Spades}) - if (cardToMove->suit() == suit && tryMoveCardToFoundation(suit, cardToMove)) + if (cardToMove->suit() == suit && tryMoveCardToFoundation(suit, cardToMove)) { + qDebug() << "* Auto-moved card " << cardToMove->toString() << " to foundation " << suit; return true; + } // 2. Try moving the card to another column for (int columnId = 0; columnId < m_columns.size(); ++columnId) if (isMoveToColumnLegal(cardToMove, columnId)) { moveCardToColumn(columnId); + qDebug() << "* Auto-moved card " << cardToMove->toString() << " to column " << columnId; return true; } diff --git a/playingcard.cpp b/playingcard.cpp index f1b9d34..848b379 100644 --- a/playingcard.cpp +++ b/playingcard.cpp @@ -63,6 +63,11 @@ void PlayingCard::setValue(const PlayingCard::Value &value) emit valueChanged(); } +QString PlayingCard::toString() const +{ + return valueString() + " of " + suitString(); +} + QList PlayingCard::createDeck() { QList deck; diff --git a/playingcard.h b/playingcard.h index fc69643..a034a62 100644 --- a/playingcard.h +++ b/playingcard.h @@ -53,6 +53,8 @@ public: QString valueString() const; void setValue(const Value &value); + QString toString() const; + static QList createDeck(); static bool areOppositeColors(const PlayingCard &card1, const PlayingCard &card2);