solitare/playingcard.cpp
ItsDrike 868699979d
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
2024-12-01 21:17:45 +01:00

96 lines
2.4 KiB
C++

#include "playingcard.h"
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}
{ }
PlayingCard::Suit PlayingCard::suit() const
{
return m_suit;
}
QString PlayingCard::suitString() const
{
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_suit = suit;
emit onSuitChanged();
}
PlayingCard::Value PlayingCard::value() const
{
return m_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;
m_value = value;
emit onValueChanged();
}
bool PlayingCard::isFaceDown() const
{
return m_isFaceDown;
}
void PlayingCard::setIsFaceDown(bool faceDown)
{
if (m_isFaceDown == faceDown)
return;
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;
}