diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..07c9806 --- /dev/null +++ b/.clang-format @@ -0,0 +1,65 @@ +--- +Language: Cpp +BasedOnStyle: LLVM + +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignEscapedNewlines: Right +AlignOperands: DontAlign +AlignTrailingComments: false +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +BreakBeforeBraces: Attach +BreakBeforeTernaryOperators: false +BreakConstructorInitializers: AfterColon +ColumnLimit: 180 +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +IncludeBlocks: Preserve +IndentCaseLabels: true +IndentWidth: 4 +PointerAlignment: Left +ReflowComments: false +SortIncludes: CaseSensitive +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 4 +UseTab: Never + +AllowShortEnumsOnASingleLine: false + +BraceWrapping: + AfterEnum: false + +AlignConsecutiveDeclarations: None + +NamespaceIndentation: All diff --git a/src/columnslot.cpp b/src/columnslot.cpp index 43d47e2..11ce734 100644 --- a/src/columnslot.cpp +++ b/src/columnslot.cpp @@ -1,8 +1,6 @@ #include "columnslot.h" -ColumnSlot::ColumnSlot(PlayingCard* card, bool revealed, QObject* parent) - : QObject(parent), m_card(card), m_revealed(revealed) -{} +ColumnSlot::ColumnSlot(PlayingCard* card, bool revealed, QObject* parent) : QObject(parent), m_card(card), m_revealed(revealed) {} PlayingCard* ColumnSlot::card() const { return m_card; diff --git a/src/columnslot.h b/src/columnslot.h index 2079e8f..cf98779 100644 --- a/src/columnslot.h +++ b/src/columnslot.h @@ -1,19 +1,18 @@ #ifndef COLUMNSLOT_H #define COLUMNSLOT_H +#include "playingcard.h" #include #include -#include "playingcard.h" -class ColumnSlot : public QObject -{ +class ColumnSlot : public QObject { Q_OBJECT QML_ELEMENT QML_UNCREATABLE("Use C++ logic to instantiate") Q_PROPERTY(PlayingCard* card READ card CONSTANT) Q_PROPERTY(bool revealed READ isRevealed NOTIFY revealedChanged) -public: + public: explicit ColumnSlot(PlayingCard* card, bool revealed, QObject* parent = nullptr); PlayingCard* card() const; @@ -21,10 +20,10 @@ public: bool isRevealed() const; void reveal(); -signals: + signals: void revealedChanged(); -private: + private: PlayingCard* m_card; bool m_revealed; }; diff --git a/src/gamestate.cpp b/src/gamestate.cpp index a64d39c..58be4c5 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp @@ -1,10 +1,8 @@ #include "gamestate.h" -#include #include +#include -GameState::GameState(QObject *parent) - : QObject{parent} -{ +GameState::GameState(QObject* parent) : QObject{parent} { assert(connect(this, SIGNAL(foundationChanged()), this, SLOT(onFoundationChanged()))); m_foundation.resize(4); @@ -13,8 +11,7 @@ GameState::GameState(QObject *parent) dealCards(); } -void GameState::dealCards() -{ +void GameState::dealCards() { qDebug() << "Dealing cards"; cleanupBoard(false); @@ -33,7 +30,7 @@ void GameState::dealCards() // Deal exactly i+1 cards to the i-th column for (int j = 0; j <= i; j++) { bool revealed = (j == i); - ColumnSlot *col = new ColumnSlot(deck[index], revealed); + ColumnSlot* col = new ColumnSlot(deck[index], revealed); column.append(col); index++; } @@ -51,8 +48,7 @@ void GameState::dealCards() emit foundationChanged(); } -void GameState::setupWinningDeck() -{ +void GameState::setupWinningDeck() { qDebug() << "Setting up a winning deck"; cleanupBoard(false); @@ -92,8 +88,7 @@ void GameState::setupWinningDeck() emit foundationChanged(); } -void GameState::drawNextCard() -{ +void GameState::drawNextCard() { qDebug() << "Drawing next card."; // If drawPile is empty, flip the throwawayPile to drawPile @@ -116,8 +111,7 @@ void GameState::drawNextCard() emit throwawayPileChanged(); } -bool GameState::moveThrownCardToColumn(int columnId) -{ +bool GameState::moveThrownCardToColumn(int columnId) { assert(columnId >= 0 && columnId < 7); auto& columnStack = m_columns[columnId]; @@ -146,8 +140,7 @@ bool GameState::moveThrownCardToColumn(int columnId) return true; } -bool GameState::moveThrownCardToFoundation(int foundationId) -{ +bool GameState::moveThrownCardToFoundation(int foundationId) { assert(foundationId >= 0 && foundationId < 4); auto& foundationStack = m_foundation[foundationId]; @@ -176,8 +169,7 @@ bool GameState::moveThrownCardToFoundation(int foundationId) return true; } -bool GameState::moveColumnCardToColumn(int fromColumnId, int toColumnId, int fromCardIndex) -{ +bool GameState::moveColumnCardToColumn(int fromColumnId, int toColumnId, int fromCardIndex) { assert(fromColumnId >= 0 && fromColumnId < 7); assert(toColumnId >= 0 && toColumnId < 7); auto fromColumnStack = m_columns[fromColumnId]; @@ -213,8 +205,7 @@ bool GameState::moveColumnCardToColumn(int fromColumnId, int toColumnId, int fro return true; } -bool GameState::moveColumnCardToFoundation(int columnId, int foundationId) -{ +bool GameState::moveColumnCardToFoundation(int columnId, int foundationId) { assert(columnId >= 0 && columnId < 7); assert(foundationId >= 0 && foundationId < 4); auto& columnStack = m_columns[columnId]; @@ -246,11 +237,9 @@ bool GameState::moveColumnCardToFoundation(int columnId, int foundationId) emit columnsChanged(); // CRASH (not if I remove the delete col line though) emit foundationChanged(); return true; - } -bool GameState::autoMoveThrownCard() -{ +bool GameState::autoMoveThrownCard() { if (m_throwawayPile.isEmpty()) { qWarning() << "Attempted to move thrown card to foundation with empty throwaway pile"; return false; @@ -272,7 +261,6 @@ bool GameState::autoMoveThrownCard() emit throwawayPileChanged(); - // We don't know which pile the card was moved to, to be safe, emit // a change from both // NOTE: consider returning what changed from tryAutoMoveSingleCard @@ -282,8 +270,7 @@ bool GameState::autoMoveThrownCard() return true; } -bool GameState::autoMoveColumnCard(int columnId, int cardIndex) -{ +bool GameState::autoMoveColumnCard(int columnId, int cardIndex) { assert(columnId >= 0 && columnId < 7); auto& columnStack = m_columns[columnId]; @@ -349,11 +336,10 @@ bool GameState::autoMoveColumnCard(int columnId, int cardIndex) return true; } -void GameState::onFoundationChanged() -{ +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)) { + 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; @@ -371,22 +357,21 @@ void GameState::onFoundationChanged() emit gameWonChanged(); } -void GameState::cleanupBoard(bool emitChanges) -{ +void GameState::cleanupBoard(bool emitChanges) { // Clean up all PlayingCard objects in the draw pile - for (auto &card : m_drawPile) { + for (auto& card : m_drawPile) { card->deleteLater(); } m_drawPile.clear(); // Clean up all PlayingCard objects in the throwaway pile - for (auto &card : m_throwawayPile) { + for (auto& card : m_throwawayPile) { card->deleteLater(); } m_throwawayPile.clear(); // Clean up all PlayingCard objects in the foundation piles - for (auto &foundationPile : m_foundation) { + for (auto& foundationPile : m_foundation) { for (auto card : foundationPile) card->deleteLater(); foundationPile.clear(); @@ -395,7 +380,7 @@ void GameState::cleanupBoard(bool emitChanges) // Clean up all ColumnSlot objects in the columns // alongside with the associated PlayingCard objects // that they hold - for (auto &column : m_columns) { + for (auto& column : m_columns) { for (auto slot : column) { slot->card()->deleteLater(); slot->deleteLater(); @@ -414,8 +399,7 @@ void GameState::cleanupBoard(bool emitChanges) } } -bool GameState::tryAutoMoveSingleCard(PlayingCard &cardToMove, int skipColumnId) -{ +bool GameState::tryAutoMoveSingleCard(PlayingCard& cardToMove, int skipColumnId) { // 1. Try moving the card to the foundation const int foundationId = static_cast(cardToMove.suit()); if (isFoundationMoveValid(cardToMove, foundationId)) { @@ -442,8 +426,7 @@ bool GameState::tryAutoMoveSingleCard(PlayingCard &cardToMove, int skipColumnId) return false; } -bool GameState::tryAutoMoveMultipleCards(const QList& cards, int skipColumnId) -{ +bool GameState::tryAutoMoveMultipleCards(const QList& cards, int skipColumnId) { assert(cards.size() > 1); // If we can move the first (selected) card to another column, @@ -471,8 +454,7 @@ bool GameState::tryAutoMoveMultipleCards(const QList& cards, int s return false; } -bool GameState::isFoundationMoveValid(const PlayingCard& cardToMove, int foundationId) -{ +bool GameState::isFoundationMoveValid(const PlayingCard& cardToMove, int foundationId) { assert(foundationId >= 0 && foundationId < 4); const auto foundationSuit = static_cast(foundationId); const auto& foundationStack = m_foundation[foundationId]; @@ -523,9 +505,7 @@ bool GameState::isColumnMoveValid(const PlayingCard& cardToMove, int columnId) { return PlayingCard::areOppositeColors(cardToMove, columnCard); } - -void GameState::ensureColumnRevealed(int columnId) -{ +void GameState::ensureColumnRevealed(int columnId) { assert(columnId >= 0 && columnId < 7); auto& columnStack = m_columns[columnId]; @@ -534,7 +514,7 @@ void GameState::ensureColumnRevealed(int columnId) return; // Get the last column slot - ColumnSlot *col = columnStack.last(); + ColumnSlot* col = columnStack.last(); // If it's already revealed, there's nothing to do if (col->isRevealed()) @@ -545,27 +525,22 @@ void GameState::ensureColumnRevealed(int columnId) qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId; } -QList GameState::drawPile() const -{ +QList GameState::drawPile() const { return m_drawPile; } -QList GameState::throwawayPile() const -{ +QList GameState::throwawayPile() const { return m_throwawayPile; } -QList > GameState::columns() const -{ +QList > GameState::columns() const { return m_columns; } -QList > GameState::foundation() const -{ +QList > GameState::foundation() const { return m_foundation; } -bool GameState::gameWon() const -{ +bool GameState::gameWon() const { return m_gameWon; } diff --git a/src/gamestate.h b/src/gamestate.h index 1060050..8bd3bcd 100644 --- a/src/gamestate.h +++ b/src/gamestate.h @@ -1,13 +1,12 @@ #ifndef GAMESTATE_H #define GAMESTATE_H +#include "columnslot.h" +#include "playingcard.h" #include #include -#include "playingcard.h" -#include "columnslot.h" -class GameState : public QObject -{ +class GameState : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON @@ -17,8 +16,8 @@ class GameState : public QObject Q_PROPERTY(QList> foundation READ foundation NOTIFY foundationChanged) Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged) -public: - explicit GameState(QObject *parent = nullptr); + public: + explicit GameState(QObject* parent = nullptr); // Getters QList drawPile() const; @@ -42,17 +41,17 @@ public: Q_INVOKABLE bool autoMoveThrownCard(); Q_INVOKABLE bool autoMoveColumnCard(int columnId, int cardIndex); -signals: + signals: void drawPileChanged(); void throwawayPileChanged(); void columnsChanged(); void foundationChanged(); void gameWonChanged(); -private slots: + private slots: void onFoundationChanged(); -private: + private: QList m_drawPile; QList m_throwawayPile; QList> m_columns; diff --git a/src/main.cpp b/src/main.cpp index 82c586c..74fe474 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,13 @@ +#include "gamestate.h" #include #include #include -#include "gamestate.h" -int main(int argc, char *argv[]) -{ +int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - QObject::connect( - &engine, - &QQmlApplicationEngine::objectCreationFailed, - &app, - []() { QCoreApplication::exit(-1); }, - Qt::QueuedConnection); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); auto gameState = engine.singletonInstance("Solitare", "GameState"); gameState->setupWinningDeck(); diff --git a/src/playingcard.cpp b/src/playingcard.cpp index 848b379..d47a594 100644 --- a/src/playingcard.cpp +++ b/src/playingcard.cpp @@ -1,16 +1,12 @@ #include "playingcard.h" -PlayingCard::PlayingCard(const PlayingCard::Suit &suit, const PlayingCard::Value &value, QObject *parent) - : QObject{parent}, m_suit{suit}, m_value{value} -{ } +PlayingCard::PlayingCard(const PlayingCard::Suit& suit, const PlayingCard::Value& value, QObject* parent) : QObject{parent}, m_suit{suit}, m_value{value} {} -PlayingCard::Suit PlayingCard::suit() const -{ +PlayingCard::Suit PlayingCard::suit() const { return m_suit; } -QString PlayingCard::suitString() const -{ +QString PlayingCard::suitString() const { switch (m_suit) { case Clubs: return "clubs"; case Diamonds: return "diamonds"; @@ -20,8 +16,7 @@ QString PlayingCard::suitString() const } } -void PlayingCard::setSuit(const PlayingCard::Suit &suit) -{ +void PlayingCard::setSuit(const PlayingCard::Suit& suit) { if (m_suit == suit) return; @@ -29,13 +24,11 @@ void PlayingCard::setSuit(const PlayingCard::Suit &suit) emit suitChanged(); } -PlayingCard::Value PlayingCard::value() const -{ +PlayingCard::Value PlayingCard::value() const { return m_value; } -QString PlayingCard::valueString() const -{ +QString PlayingCard::valueString() const { switch (m_value) { case Ace: return "ace"; case Two: return "2"; @@ -54,8 +47,7 @@ QString PlayingCard::valueString() const } } -void PlayingCard::setValue(const PlayingCard::Value &value) -{ +void PlayingCard::setValue(const PlayingCard::Value& value) { if (m_value == value) return; @@ -63,18 +55,16 @@ void PlayingCard::setValue(const PlayingCard::Value &value) emit valueChanged(); } -QString PlayingCard::toString() const -{ +QString PlayingCard::toString() const { return valueString() + " of " + suitString(); } -QList PlayingCard::createDeck() -{ +QList PlayingCard::createDeck() { QList deck; for (int suitIndex = PlayingCard::Suit::Clubs; suitIndex <= PlayingCard::Suit::Spades; ++suitIndex) { for (int valueIndex = PlayingCard::Value::Ace; valueIndex <= PlayingCard::Value::King; ++valueIndex) { - PlayingCard *card = new PlayingCard(); + PlayingCard* card = new PlayingCard(); card->setSuit(static_cast(suitIndex)); card->setValue(static_cast(valueIndex)); deck.append(card); @@ -84,8 +74,7 @@ QList PlayingCard::createDeck() return deck; } -bool PlayingCard::areOppositeColors(const PlayingCard &card1, const PlayingCard &card2) -{ +bool PlayingCard::areOppositeColors(const PlayingCard& card1, const PlayingCard& card2) { bool card1IsRed = (card1.suit() == Suit::Hearts || card1.suit() == Suit::Diamonds); bool card2IsRed = (card2.suit() == Suit::Hearts || card2.suit() == Suit::Diamonds); diff --git a/src/playingcard.h b/src/playingcard.h index a034a62..8085fad 100644 --- a/src/playingcard.h +++ b/src/playingcard.h @@ -4,8 +4,7 @@ #include #include -class PlayingCard : public QObject -{ +class PlayingCard : public QObject { Q_OBJECT QML_ELEMENT QML_UNCREATABLE("Use C++ logic to instantiate") @@ -19,7 +18,7 @@ class PlayingCard : public QObject Q_PROPERTY(QString valueString READ valueString NOTIFY valueChanged) Q_PROPERTY(QString suitString READ suitString NOTIFY suitChanged) -public: + public: enum Value { Ace = 1, Two = 2, @@ -43,26 +42,26 @@ public: Spades, }; - explicit PlayingCard(const Suit &suit = Suit::Clubs, const Value &value = Value::Ace, QObject *parent = nullptr); + explicit PlayingCard(const Suit& suit = Suit::Clubs, const Value& value = Value::Ace, QObject* parent = nullptr); Suit suit() const; QString suitString() const; - void setSuit(const Suit &suit); + void setSuit(const Suit& suit); Value value() const; QString valueString() const; - void setValue(const Value &value); + void setValue(const Value& value); QString toString() const; static QList createDeck(); - static bool areOppositeColors(const PlayingCard &card1, const PlayingCard &card2); + static bool areOppositeColors(const PlayingCard& card1, const PlayingCard& card2); -signals: + signals: void suitChanged(); void valueChanged(); -private: + private: Suit m_suit; Value m_value; };