Add clang-format config & format files
This commit is contained in:
parent
ddb17f8bba
commit
36a010d5aa
65
.clang-format
Normal file
65
.clang-format
Normal 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
|
|
@ -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;
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
#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")
|
||||||
Q_PROPERTY(PlayingCard* card READ card CONSTANT)
|
Q_PROPERTY(PlayingCard* card READ card CONSTANT)
|
||||||
Q_PROPERTY(bool revealed READ isRevealed NOTIFY revealedChanged)
|
Q_PROPERTY(bool revealed READ isRevealed NOTIFY revealedChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ColumnSlot(PlayingCard* card, bool revealed, QObject* parent = nullptr);
|
explicit ColumnSlot(PlayingCard* card, bool revealed, QObject* parent = nullptr);
|
||||||
|
|
||||||
PlayingCard* card() const;
|
PlayingCard* card() const;
|
||||||
|
@ -21,10 +20,10 @@ public:
|
||||||
bool isRevealed() const;
|
bool isRevealed() const;
|
||||||
void reveal();
|
void reveal();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void revealedChanged();
|
void revealedChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PlayingCard* m_card;
|
PlayingCard* m_card;
|
||||||
bool m_revealed;
|
bool m_revealed;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
@ -33,7 +30,7 @@ void GameState::dealCards()
|
||||||
// Deal exactly i+1 cards to the i-th column
|
// Deal exactly i+1 cards to the i-th column
|
||||||
for (int j = 0; j <= i; j++) {
|
for (int j = 0; j <= i; j++) {
|
||||||
bool revealed = (j == i);
|
bool revealed = (j == i);
|
||||||
ColumnSlot *col = new ColumnSlot(deck[index], revealed);
|
ColumnSlot* col = new ColumnSlot(deck[index], revealed);
|
||||||
column.append(col);
|
column.append(col);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
@ -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,11 +336,10 @@ 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)) {
|
||||||
// The piles need to contain all 13 card values each, otherwise the game isn't won
|
// The piles need to contain all 13 card values each, otherwise the game isn't won
|
||||||
if (foundationPile.size() != 13) {
|
if (foundationPile.size() != 13) {
|
||||||
gameWon = false;
|
gameWon = false;
|
||||||
|
@ -371,22 +357,21 @@ 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();
|
||||||
}
|
}
|
||||||
m_drawPile.clear();
|
m_drawPile.clear();
|
||||||
|
|
||||||
// Clean up all PlayingCard objects in the throwaway pile
|
// Clean up all PlayingCard objects in the throwaway pile
|
||||||
for (auto &card : m_throwawayPile) {
|
for (auto& card : m_throwawayPile) {
|
||||||
card->deleteLater();
|
card->deleteLater();
|
||||||
}
|
}
|
||||||
m_throwawayPile.clear();
|
m_throwawayPile.clear();
|
||||||
|
|
||||||
// Clean up all PlayingCard objects in the foundation piles
|
// Clean up all PlayingCard objects in the foundation piles
|
||||||
for (auto &foundationPile : m_foundation) {
|
for (auto& foundationPile : m_foundation) {
|
||||||
for (auto card : foundationPile)
|
for (auto card : foundationPile)
|
||||||
card->deleteLater();
|
card->deleteLater();
|
||||||
foundationPile.clear();
|
foundationPile.clear();
|
||||||
|
@ -395,7 +380,7 @@ void GameState::cleanupBoard(bool emitChanges)
|
||||||
// Clean up all ColumnSlot objects in the columns
|
// Clean up all ColumnSlot objects in the columns
|
||||||
// alongside with the associated PlayingCard objects
|
// alongside with the associated PlayingCard objects
|
||||||
// that they hold
|
// that they hold
|
||||||
for (auto &column : m_columns) {
|
for (auto& column : m_columns) {
|
||||||
for (auto slot : column) {
|
for (auto slot : column) {
|
||||||
slot->card()->deleteLater();
|
slot->card()->deleteLater();
|
||||||
slot->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
|
// 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];
|
||||||
|
|
||||||
|
@ -534,7 +514,7 @@ void GameState::ensureColumnRevealed(int columnId)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Get the last column slot
|
// Get the last column slot
|
||||||
ColumnSlot *col = columnStack.last();
|
ColumnSlot* col = columnStack.last();
|
||||||
|
|
||||||
// If it's already revealed, there's nothing to do
|
// If it's already revealed, there's nothing to do
|
||||||
if (col->isRevealed())
|
if (col->isRevealed())
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
@ -17,8 +16,8 @@ class GameState : public QObject
|
||||||
Q_PROPERTY(QList<QList<PlayingCard*>> foundation READ foundation NOTIFY foundationChanged)
|
Q_PROPERTY(QList<QList<PlayingCard*>> foundation READ foundation NOTIFY foundationChanged)
|
||||||
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
|
Q_PROPERTY(bool gameWon READ gameWon NOTIFY gameWonChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit GameState(QObject *parent = nullptr);
|
explicit GameState(QObject* parent = nullptr);
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
QList<PlayingCard*> drawPile() const;
|
QList<PlayingCard*> drawPile() const;
|
||||||
|
@ -42,17 +41,17 @@ public:
|
||||||
Q_INVOKABLE bool autoMoveThrownCard();
|
Q_INVOKABLE bool autoMoveThrownCard();
|
||||||
Q_INVOKABLE bool autoMoveColumnCard(int columnId, int cardIndex);
|
Q_INVOKABLE bool autoMoveColumnCard(int columnId, int cardIndex);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void drawPileChanged();
|
void drawPileChanged();
|
||||||
void throwawayPileChanged();
|
void throwawayPileChanged();
|
||||||
void columnsChanged();
|
void columnsChanged();
|
||||||
void foundationChanged();
|
void foundationChanged();
|
||||||
void gameWonChanged();
|
void gameWonChanged();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onFoundationChanged();
|
void onFoundationChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<PlayingCard*> m_drawPile;
|
QList<PlayingCard*> m_drawPile;
|
||||||
QList<PlayingCard*> m_throwawayPile;
|
QList<PlayingCard*> m_throwawayPile;
|
||||||
QList<QList<ColumnSlot*>> m_columns;
|
QList<QList<ColumnSlot*>> m_columns;
|
||||||
|
|
12
src/main.cpp
12
src/main.cpp
|
@ -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();
|
||||||
|
|
|
@ -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,18 +55,16 @@ 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) {
|
||||||
for (int valueIndex = PlayingCard::Value::Ace; valueIndex <= PlayingCard::Value::King; ++valueIndex) {
|
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->setSuit(static_cast<PlayingCard::Suit>(suitIndex));
|
||||||
card->setValue(static_cast<PlayingCard::Value>(valueIndex));
|
card->setValue(static_cast<PlayingCard::Value>(valueIndex));
|
||||||
deck.append(card);
|
deck.append(card);
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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")
|
||||||
|
@ -19,7 +18,7 @@ class PlayingCard : public QObject
|
||||||
Q_PROPERTY(QString valueString READ valueString NOTIFY valueChanged)
|
Q_PROPERTY(QString valueString READ valueString NOTIFY valueChanged)
|
||||||
Q_PROPERTY(QString suitString READ suitString NOTIFY suitChanged)
|
Q_PROPERTY(QString suitString READ suitString NOTIFY suitChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum Value {
|
enum Value {
|
||||||
Ace = 1,
|
Ace = 1,
|
||||||
Two = 2,
|
Two = 2,
|
||||||
|
@ -43,26 +42,26 @@ public:
|
||||||
Spades,
|
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;
|
Suit suit() const;
|
||||||
QString suitString() const;
|
QString suitString() const;
|
||||||
void setSuit(const Suit &suit);
|
void setSuit(const Suit& suit);
|
||||||
|
|
||||||
Value value() const;
|
Value value() const;
|
||||||
QString valueString() const;
|
QString valueString() const;
|
||||||
void setValue(const Value &value);
|
void setValue(const Value& value);
|
||||||
|
|
||||||
QString toString() const;
|
QString toString() const;
|
||||||
|
|
||||||
static QList<PlayingCard*> createDeck();
|
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 suitChanged();
|
||||||
void valueChanged();
|
void valueChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Suit m_suit;
|
Suit m_suit;
|
||||||
Value m_value;
|
Value m_value;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue