Add clang-format config & format files

This commit is contained in:
ItsDrike 2024-12-05 03:06:34 +01:00
parent ddb17f8bba
commit 36a010d5aa
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
8 changed files with 130 additions and 112 deletions

65
.clang-format Normal file
View file

@ -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

View file

@ -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;

View file

@ -1,19 +1,18 @@
#ifndef COLUMNSLOT_H
#define COLUMNSLOT_H
#include "playingcard.h"
#include <QObject>
#include <qqmlintegration.h>
#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;
};

View file

@ -1,10 +1,8 @@
#include "gamestate.h"
#include <random>
#include <QDebug>
#include <random>
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<PlayingCard*> &foundationPile : std::as_const(m_foundation)) {
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;
@ -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<int>(cardToMove.suit());
if (isFoundationMoveValid(cardToMove, foundationId)) {
@ -442,8 +426,7 @@ bool GameState::tryAutoMoveSingleCard(PlayingCard &cardToMove, int skipColumnId)
return false;
}
bool GameState::tryAutoMoveMultipleCards(const QList<PlayingCard*>& cards, int skipColumnId)
{
bool GameState::tryAutoMoveMultipleCards(const QList<PlayingCard*>& 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<PlayingCard*>& 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<PlayingCard::Suit>(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<PlayingCard *> GameState::drawPile() const
{
QList<PlayingCard*> GameState::drawPile() const {
return m_drawPile;
}
QList<PlayingCard *> GameState::throwawayPile() const
{
QList<PlayingCard*> GameState::throwawayPile() const {
return m_throwawayPile;
}
QList<QList<ColumnSlot *> > GameState::columns() const
{
QList<QList<ColumnSlot*> > GameState::columns() const {
return m_columns;
}
QList<QList<PlayingCard *> > GameState::foundation() const
{
QList<QList<PlayingCard*> > GameState::foundation() const {
return m_foundation;
}
bool GameState::gameWon() const
{
bool GameState::gameWon() const {
return m_gameWon;
}

View file

@ -1,13 +1,12 @@
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include "columnslot.h"
#include "playingcard.h"
#include <QObject>
#include <qqmlintegration.h>
#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<QList<PlayingCard*>> 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<PlayingCard*> 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<PlayingCard*> m_drawPile;
QList<PlayingCard*> m_throwawayPile;
QList<QList<ColumnSlot*>> m_columns;

View file

@ -1,19 +1,13 @@
#include "gamestate.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#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<GameState*>("Solitare", "GameState");
gameState->setupWinningDeck();

View file

@ -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 *> PlayingCard::createDeck()
{
QList<PlayingCard*> PlayingCard::createDeck() {
QList<PlayingCard*> 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<PlayingCard::Suit>(suitIndex));
card->setValue(static_cast<PlayingCard::Value>(valueIndex));
deck.append(card);
@ -84,8 +74,7 @@ QList<PlayingCard *> 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);

View file

@ -4,8 +4,7 @@
#include <QObject>
#include <qqmlintegration.h>
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<PlayingCard*> 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;
};