Improve PlayingCard class

- Move to using enums to represent the suit & value
- This changes the badly named color to more appropriate suit.
- Add createDeck static method
This commit is contained in:
ItsDrike 2024-12-01 21:17:09 +01:00
parent e3242b71e9
commit 868699979d
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
4 changed files with 104 additions and 22 deletions

View file

@ -18,7 +18,7 @@ Item {
anchors.fill: parent anchors.fill: parent
source: cardModel.card.isFaceDown source: cardModel.card.isFaceDown
? "qrc:/img/playing_cards/backs/" + cardModel.backStyle + ".svg" ? "qrc:/img/playing_cards/backs/" + cardModel.backStyle + ".svg"
: "qrc:/img/playing_cards/fronts/" + cardModel.card.color + "_" + cardModel.card.value + ".svg" : "qrc:/img/playing_cards/fronts/" + cardModel.card.suitString + "_" + cardModel.card.valueString + ".svg"
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
} }
} }

View file

@ -17,7 +17,7 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<PlayingCard>("Solitare", 1, 0, "PlayingCard", "PlayingCard cannot be directly created in QML. Use C++ logic to instantiate."); qmlRegisterUncreatableType<PlayingCard>("Solitare", 1, 0, "PlayingCard", "PlayingCard cannot be directly created in QML. Use C++ logic to instantiate.");
PlayingCard myCard("clubs", "ace"); PlayingCard myCard(PlayingCard::Suit::Hearts, PlayingCard::Value::Seven);
engine.rootContext()->setContextProperty("myCard", &myCard); engine.rootContext()->setContextProperty("myCard", &myCard);
engine.loadFromModule("Solitare", "Main"); engine.loadFromModule("Solitare", "Main");

View file

@ -1,29 +1,60 @@
#include "playingcard.h" #include "playingcard.h"
PlayingCard::PlayingCard(const QString &color, const QString &value, bool isFaceDown, QObject *parent) PlayingCard::PlayingCard(const PlayingCard::Suit &suit, const PlayingCard::Value &value, bool isFaceDown, QObject *parent)
: QObject{parent}, m_color{color}, m_value{value}, m_isFaceDown{isFaceDown} : QObject{parent}, m_suit{suit}, m_value{value}, m_isFaceDown{isFaceDown}
{ } { }
QString PlayingCard::color() const PlayingCard::Suit PlayingCard::suit() const
{ {
return m_color; return m_suit;
} }
void PlayingCard::setColor(const QString &color) QString PlayingCard::suitString() const
{ {
if (m_color == color) switch (m_suit) {
case Clubs: return "clubs";
case Diamonds: return "diamonds";
case Hearts: return "hearts";
case Spades: return "spades";
default: throw std::invalid_argument("Invalid Suit enum value");
}
}
void PlayingCard::setSuit(const PlayingCard::Suit &suit)
{
if (m_suit == suit)
return; return;
m_color = color; m_suit = suit;
emit onColorChanged(); emit onSuitChanged();
} }
QString PlayingCard::value() const PlayingCard::Value PlayingCard::value() const
{ {
return m_value; return m_value;
} }
void PlayingCard::setValue(const QString &value) QString PlayingCard::valueString() const
{
switch (m_value) {
case Ace: return "ace";
case Two: return "2";
case Three: return "3";
case Four: return "4";
case Five: return "5";
case Six: return "6";
case Seven: return "7";
case Eight: return "8";
case Nine: return "9";
case Ten: return "10";
case Jack: return "jack";
case Queen: return "queen";
case King: return "king";
default: throw std::invalid_argument("Invalid Value enum value");
}
}
void PlayingCard::setValue(const PlayingCard::Value &value)
{ {
if (m_value == value) if (m_value == value)
return; return;
@ -45,3 +76,20 @@ void PlayingCard::setIsFaceDown(bool faceDown)
m_isFaceDown = faceDown; m_isFaceDown = faceDown;
emit onIsFaceDownChanged(); emit onIsFaceDownChanged();
} }
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();
card->setSuit(static_cast<PlayingCard::Suit>(suitIndex));
card->setValue(static_cast<PlayingCard::Value>(valueIndex));
card->setIsFaceDown(true); // All cards are face down initially
deck.append(card);
}
}
return deck;
}

View file

@ -6,30 +6,64 @@
class PlayingCard : public QObject class PlayingCard : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY onColorChanged REQUIRED)
Q_PROPERTY(QString value READ value WRITE setValue NOTIFY onValueChanged REQUIRED) Q_ENUMS(Value)
Q_ENUMS(Suit)
Q_PROPERTY(Suit suit READ suit WRITE setSuit NOTIFY onSuitChanged REQUIRED)
Q_PROPERTY(Value value READ value WRITE setValue NOTIFY onValueChanged REQUIRED)
Q_PROPERTY(bool isFaceDown READ isFaceDown WRITE setIsFaceDown NOTIFY onIsFaceDownChanged) Q_PROPERTY(bool isFaceDown READ isFaceDown WRITE setIsFaceDown NOTIFY onIsFaceDownChanged)
Q_PROPERTY(QString valueString READ valueString NOTIFY onValueChanged)
Q_PROPERTY(QString suitString READ suitString NOTIFY onSuitChanged)
public: public:
explicit PlayingCard(const QString &color = "", const QString &value = "", bool isFaceDown = false, QObject *parent = nullptr); enum Value {
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
};
QString color() const; enum Suit {
void setColor(const QString &color); Clubs,
Diamonds,
Hearts,
Spades,
};
QString value() const; explicit PlayingCard(const Suit &suit = Suit::Clubs, const Value &value = Value::Ace, bool isFaceDown = false, QObject *parent = nullptr);
void setValue(const QString &value);
Suit suit() const;
QString suitString() const;
void setSuit(const Suit &suit);
Value value() const;
QString valueString() const;
void setValue(const Value &value);
bool isFaceDown() const; bool isFaceDown() const;
void setIsFaceDown(bool faceDown); void setIsFaceDown(bool faceDown);
static QList<PlayingCard*> createDeck();
signals: signals:
void onColorChanged(); void onSuitChanged();
void onValueChanged(); void onValueChanged();
void onIsFaceDownChanged(); void onIsFaceDownChanged();
private: private:
QString m_color; Suit m_suit;
QString m_value; Value m_value;
bool m_isFaceDown; bool m_isFaceDown;
QString m_imgUrl; QString m_imgUrl;
}; };