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" #include "columnslot.h"
ColumnSlot::ColumnSlot(PlayingCard* card, bool revealed, QObject* parent) ColumnSlot::ColumnSlot(PlayingCard* card, bool revealed, QObject* parent) : QObject(parent), m_card(card), m_revealed(revealed) {}
: QObject(parent), m_card(card), m_revealed(revealed)
{}
PlayingCard* ColumnSlot::card() const { PlayingCard* ColumnSlot::card() const {
return m_card; return m_card;

View file

@ -1,12 +1,11 @@
#ifndef COLUMNSLOT_H #ifndef COLUMNSLOT_H
#define COLUMNSLOT_H #define COLUMNSLOT_H
#include "playingcard.h"
#include <QObject> #include <QObject>
#include <qqmlintegration.h> #include <qqmlintegration.h>
#include "playingcard.h"
class ColumnSlot : public QObject class ColumnSlot : public QObject {
{
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
QML_UNCREATABLE("Use C++ logic to instantiate") QML_UNCREATABLE("Use C++ logic to instantiate")

View file

@ -1,10 +1,8 @@
#include "gamestate.h" #include "gamestate.h"
#include <random>
#include <QDebug> #include <QDebug>
#include <random>
GameState::GameState(QObject *parent) GameState::GameState(QObject* parent) : QObject{parent} {
: QObject{parent}
{
assert(connect(this, SIGNAL(foundationChanged()), this, SLOT(onFoundationChanged()))); assert(connect(this, SIGNAL(foundationChanged()), this, SLOT(onFoundationChanged())));
m_foundation.resize(4); m_foundation.resize(4);
@ -13,8 +11,7 @@ GameState::GameState(QObject *parent)
dealCards(); dealCards();
} }
void GameState::dealCards() void GameState::dealCards() {
{
qDebug() << "Dealing cards"; qDebug() << "Dealing cards";
cleanupBoard(false); cleanupBoard(false);
@ -51,8 +48,7 @@ void GameState::dealCards()
emit foundationChanged(); emit foundationChanged();
} }
void GameState::setupWinningDeck() void GameState::setupWinningDeck() {
{
qDebug() << "Setting up a winning deck"; qDebug() << "Setting up a winning deck";
cleanupBoard(false); cleanupBoard(false);
@ -92,8 +88,7 @@ void GameState::setupWinningDeck()
emit foundationChanged(); emit foundationChanged();
} }
void GameState::drawNextCard() void GameState::drawNextCard() {
{
qDebug() << "Drawing next card."; qDebug() << "Drawing next card.";
// If drawPile is empty, flip the throwawayPile to drawPile // If drawPile is empty, flip the throwawayPile to drawPile
@ -116,8 +111,7 @@ void GameState::drawNextCard()
emit throwawayPileChanged(); emit throwawayPileChanged();
} }
bool GameState::moveThrownCardToColumn(int columnId) bool GameState::moveThrownCardToColumn(int columnId) {
{
assert(columnId >= 0 && columnId < 7); assert(columnId >= 0 && columnId < 7);
auto& columnStack = m_columns[columnId]; auto& columnStack = m_columns[columnId];
@ -146,8 +140,7 @@ bool GameState::moveThrownCardToColumn(int columnId)
return true; return true;
} }
bool GameState::moveThrownCardToFoundation(int foundationId) bool GameState::moveThrownCardToFoundation(int foundationId) {
{
assert(foundationId >= 0 && foundationId < 4); assert(foundationId >= 0 && foundationId < 4);
auto& foundationStack = m_foundation[foundationId]; auto& foundationStack = m_foundation[foundationId];
@ -176,8 +169,7 @@ bool GameState::moveThrownCardToFoundation(int foundationId)
return true; 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(fromColumnId >= 0 && fromColumnId < 7);
assert(toColumnId >= 0 && toColumnId < 7); assert(toColumnId >= 0 && toColumnId < 7);
auto fromColumnStack = m_columns[fromColumnId]; auto fromColumnStack = m_columns[fromColumnId];
@ -213,8 +205,7 @@ bool GameState::moveColumnCardToColumn(int fromColumnId, int toColumnId, int fro
return true; return true;
} }
bool GameState::moveColumnCardToFoundation(int columnId, int foundationId) bool GameState::moveColumnCardToFoundation(int columnId, int foundationId) {
{
assert(columnId >= 0 && columnId < 7); assert(columnId >= 0 && columnId < 7);
assert(foundationId >= 0 && foundationId < 4); assert(foundationId >= 0 && foundationId < 4);
auto& columnStack = m_columns[columnId]; 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 columnsChanged(); // CRASH (not if I remove the delete col line though)
emit foundationChanged(); emit foundationChanged();
return true; return true;
} }
bool GameState::autoMoveThrownCard() bool GameState::autoMoveThrownCard() {
{
if (m_throwawayPile.isEmpty()) { if (m_throwawayPile.isEmpty()) {
qWarning() << "Attempted to move thrown card to foundation with empty throwaway pile"; qWarning() << "Attempted to move thrown card to foundation with empty throwaway pile";
return false; return false;
@ -272,7 +261,6 @@ bool GameState::autoMoveThrownCard()
emit throwawayPileChanged(); emit throwawayPileChanged();
// We don't know which pile the card was moved to, to be safe, emit // We don't know which pile the card was moved to, to be safe, emit
// a change from both // a change from both
// NOTE: consider returning what changed from tryAutoMoveSingleCard // NOTE: consider returning what changed from tryAutoMoveSingleCard
@ -282,8 +270,7 @@ bool GameState::autoMoveThrownCard()
return true; return true;
} }
bool GameState::autoMoveColumnCard(int columnId, int cardIndex) bool GameState::autoMoveColumnCard(int columnId, int cardIndex) {
{
assert(columnId >= 0 && columnId < 7); assert(columnId >= 0 && columnId < 7);
auto& columnStack = m_columns[columnId]; auto& columnStack = m_columns[columnId];
@ -349,8 +336,7 @@ bool GameState::autoMoveColumnCard(int columnId, int cardIndex)
return true; return true;
} }
void GameState::onFoundationChanged() void GameState::onFoundationChanged() {
{
// Check if the game is won (can only happen on a foundation pile change) // Check if the game is won (can only happen on a foundation pile change)
bool gameWon = true; bool gameWon = true;
for (const QList<PlayingCard*>& foundationPile : std::as_const(m_foundation)) { for (const QList<PlayingCard*>& foundationPile : std::as_const(m_foundation)) {
@ -371,8 +357,7 @@ void GameState::onFoundationChanged()
emit gameWonChanged(); emit gameWonChanged();
} }
void GameState::cleanupBoard(bool emitChanges) void GameState::cleanupBoard(bool emitChanges) {
{
// Clean up all PlayingCard objects in the draw pile // Clean up all PlayingCard objects in the draw pile
for (auto& card : m_drawPile) { for (auto& card : m_drawPile) {
card->deleteLater(); card->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 // 1. Try moving the card to the foundation
const int foundationId = static_cast<int>(cardToMove.suit()); const int foundationId = static_cast<int>(cardToMove.suit());
if (isFoundationMoveValid(cardToMove, foundationId)) { if (isFoundationMoveValid(cardToMove, foundationId)) {
@ -442,8 +426,7 @@ bool GameState::tryAutoMoveSingleCard(PlayingCard &cardToMove, int skipColumnId)
return false; return false;
} }
bool GameState::tryAutoMoveMultipleCards(const QList<PlayingCard*>& cards, int skipColumnId) bool GameState::tryAutoMoveMultipleCards(const QList<PlayingCard*>& cards, int skipColumnId) {
{
assert(cards.size() > 1); assert(cards.size() > 1);
// If we can move the first (selected) card to another column, // 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; return false;
} }
bool GameState::isFoundationMoveValid(const PlayingCard& cardToMove, int foundationId) bool GameState::isFoundationMoveValid(const PlayingCard& cardToMove, int foundationId) {
{
assert(foundationId >= 0 && foundationId < 4); assert(foundationId >= 0 && foundationId < 4);
const auto foundationSuit = static_cast<PlayingCard::Suit>(foundationId); const auto foundationSuit = static_cast<PlayingCard::Suit>(foundationId);
const auto& foundationStack = m_foundation[foundationId]; const auto& foundationStack = m_foundation[foundationId];
@ -523,9 +505,7 @@ bool GameState::isColumnMoveValid(const PlayingCard& cardToMove, int columnId) {
return PlayingCard::areOppositeColors(cardToMove, columnCard); return PlayingCard::areOppositeColors(cardToMove, columnCard);
} }
void GameState::ensureColumnRevealed(int columnId) {
void GameState::ensureColumnRevealed(int columnId)
{
assert(columnId >= 0 && columnId < 7); assert(columnId >= 0 && columnId < 7);
auto& columnStack = m_columns[columnId]; auto& columnStack = m_columns[columnId];
@ -545,27 +525,22 @@ void GameState::ensureColumnRevealed(int columnId)
qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId; qDebug() << "Revealed card " << col->card()->toString() << " in column " << columnId;
} }
QList<PlayingCard *> GameState::drawPile() const QList<PlayingCard*> GameState::drawPile() const {
{
return m_drawPile; return m_drawPile;
} }
QList<PlayingCard *> GameState::throwawayPile() const QList<PlayingCard*> GameState::throwawayPile() const {
{
return m_throwawayPile; return m_throwawayPile;
} }
QList<QList<ColumnSlot *> > GameState::columns() const QList<QList<ColumnSlot*> > GameState::columns() const {
{
return m_columns; return m_columns;
} }
QList<QList<PlayingCard *> > GameState::foundation() const QList<QList<PlayingCard*> > GameState::foundation() const {
{
return m_foundation; return m_foundation;
} }
bool GameState::gameWon() const bool GameState::gameWon() const {
{
return m_gameWon; return m_gameWon;
} }

View file

@ -1,13 +1,12 @@
#ifndef GAMESTATE_H #ifndef GAMESTATE_H
#define GAMESTATE_H #define GAMESTATE_H
#include "columnslot.h"
#include "playingcard.h"
#include <QObject> #include <QObject>
#include <qqmlintegration.h> #include <qqmlintegration.h>
#include "playingcard.h"
#include "columnslot.h"
class GameState : public QObject class GameState : public QObject {
{
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
QML_SINGLETON QML_SINGLETON

View file

@ -1,19 +1,13 @@
#include "gamestate.h"
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext> #include <QQmlContext>
#include "gamestate.h"
int main(int argc, char *argv[]) int main(int argc, char* argv[]) {
{
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
QObject::connect( QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection);
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
auto gameState = engine.singletonInstance<GameState*>("Solitare", "GameState"); auto gameState = engine.singletonInstance<GameState*>("Solitare", "GameState");
gameState->setupWinningDeck(); gameState->setupWinningDeck();

View file

@ -1,16 +1,12 @@
#include "playingcard.h" #include "playingcard.h"
PlayingCard::PlayingCard(const PlayingCard::Suit &suit, const PlayingCard::Value &value, QObject *parent) PlayingCard::PlayingCard(const PlayingCard::Suit& suit, const PlayingCard::Value& value, QObject* parent) : QObject{parent}, m_suit{suit}, m_value{value} {}
: QObject{parent}, m_suit{suit}, m_value{value}
{ }
PlayingCard::Suit PlayingCard::suit() const PlayingCard::Suit PlayingCard::suit() const {
{
return m_suit; return m_suit;
} }
QString PlayingCard::suitString() const QString PlayingCard::suitString() const {
{
switch (m_suit) { switch (m_suit) {
case Clubs: return "clubs"; case Clubs: return "clubs";
case Diamonds: return "diamonds"; 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) if (m_suit == suit)
return; return;
@ -29,13 +24,11 @@ void PlayingCard::setSuit(const PlayingCard::Suit &suit)
emit suitChanged(); emit suitChanged();
} }
PlayingCard::Value PlayingCard::value() const PlayingCard::Value PlayingCard::value() const {
{
return m_value; return m_value;
} }
QString PlayingCard::valueString() const QString PlayingCard::valueString() const {
{
switch (m_value) { switch (m_value) {
case Ace: return "ace"; case Ace: return "ace";
case Two: return "2"; 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) if (m_value == value)
return; return;
@ -63,13 +55,11 @@ void PlayingCard::setValue(const PlayingCard::Value &value)
emit valueChanged(); emit valueChanged();
} }
QString PlayingCard::toString() const QString PlayingCard::toString() const {
{
return valueString() + " of " + suitString(); return valueString() + " of " + suitString();
} }
QList<PlayingCard *> PlayingCard::createDeck() QList<PlayingCard*> PlayingCard::createDeck() {
{
QList<PlayingCard*> deck; QList<PlayingCard*> deck;
for (int suitIndex = PlayingCard::Suit::Clubs; suitIndex <= PlayingCard::Suit::Spades; ++suitIndex) { for (int suitIndex = PlayingCard::Suit::Clubs; suitIndex <= PlayingCard::Suit::Spades; ++suitIndex) {
@ -84,8 +74,7 @@ QList<PlayingCard *> PlayingCard::createDeck()
return deck; 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 card1IsRed = (card1.suit() == Suit::Hearts || card1.suit() == Suit::Diamonds);
bool card2IsRed = (card2.suit() == Suit::Hearts || card2.suit() == Suit::Diamonds); bool card2IsRed = (card2.suit() == Suit::Hearts || card2.suit() == Suit::Diamonds);

View file

@ -4,8 +4,7 @@
#include <QObject> #include <QObject>
#include <qqmlintegration.h> #include <qqmlintegration.h>
class PlayingCard : public QObject class PlayingCard : public QObject {
{
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
QML_UNCREATABLE("Use C++ logic to instantiate") QML_UNCREATABLE("Use C++ logic to instantiate")