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:
parent
e3242b71e9
commit
868699979d
|
@ -18,7 +18,7 @@ Item {
|
|||
anchors.fill: parent
|
||||
source: cardModel.card.isFaceDown
|
||||
? "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
|
||||
}
|
||||
}
|
||||
|
|
2
main.cpp
2
main.cpp
|
@ -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.");
|
||||
|
||||
PlayingCard myCard("clubs", "ace");
|
||||
PlayingCard myCard(PlayingCard::Suit::Hearts, PlayingCard::Value::Seven);
|
||||
engine.rootContext()->setContextProperty("myCard", &myCard);
|
||||
|
||||
engine.loadFromModule("Solitare", "Main");
|
||||
|
|
|
@ -1,29 +1,60 @@
|
|||
#include "playingcard.h"
|
||||
|
||||
PlayingCard::PlayingCard(const QString &color, const QString &value, bool isFaceDown, QObject *parent)
|
||||
: QObject{parent}, m_color{color}, m_value{value}, m_isFaceDown{isFaceDown}
|
||||
PlayingCard::PlayingCard(const PlayingCard::Suit &suit, const PlayingCard::Value &value, bool isFaceDown, QObject *parent)
|
||||
: 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;
|
||||
|
||||
m_color = color;
|
||||
emit onColorChanged();
|
||||
m_suit = suit;
|
||||
emit onSuitChanged();
|
||||
}
|
||||
|
||||
QString PlayingCard::value() const
|
||||
PlayingCard::Value PlayingCard::value() const
|
||||
{
|
||||
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)
|
||||
return;
|
||||
|
@ -45,3 +76,20 @@ void PlayingCard::setIsFaceDown(bool faceDown)
|
|||
m_isFaceDown = faceDown;
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -6,30 +6,64 @@
|
|||
class PlayingCard : public QObject
|
||||
{
|
||||
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(QString valueString READ valueString NOTIFY onValueChanged)
|
||||
Q_PROPERTY(QString suitString READ suitString NOTIFY onSuitChanged)
|
||||
|
||||
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;
|
||||
void setColor(const QString &color);
|
||||
enum Suit {
|
||||
Clubs,
|
||||
Diamonds,
|
||||
Hearts,
|
||||
Spades,
|
||||
};
|
||||
|
||||
QString value() const;
|
||||
void setValue(const QString &value);
|
||||
explicit PlayingCard(const Suit &suit = Suit::Clubs, const Value &value = Value::Ace, bool isFaceDown = false, QObject *parent = nullptr);
|
||||
|
||||
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;
|
||||
void setIsFaceDown(bool faceDown);
|
||||
|
||||
static QList<PlayingCard*> createDeck();
|
||||
|
||||
signals:
|
||||
void onColorChanged();
|
||||
void onSuitChanged();
|
||||
void onValueChanged();
|
||||
void onIsFaceDownChanged();
|
||||
|
||||
private:
|
||||
QString m_color;
|
||||
QString m_value;
|
||||
Suit m_suit;
|
||||
Value m_value;
|
||||
bool m_isFaceDown;
|
||||
QString m_imgUrl;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue