From 562ddb25b926d7f3ef0999f91d3a33e9e13ad7db Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Mon, 17 Nov 2025 14:38:58 +0800 Subject: [PATCH] fix(game): remove `CardColor::WILD` and update wild card handling logic --- src/game/Card.cpp | 42 +++++++++++++++++++++--------------------- src/game/Card.h | 34 +++++++++++++++++----------------- src/game/CardTile.cpp | 6 +++--- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/game/Card.cpp b/src/game/Card.cpp index c6b6d46..b50b786 100644 --- a/src/game/Card.cpp +++ b/src/game/Card.cpp @@ -10,37 +10,35 @@ #include namespace UNO::GAME { - Card::Card(const CardColor color, const CardType value) { - if (color == CardColor::WILD && value != CardType::WILD && value != CardType::WILDDRAWFOUR) { - throw std::invalid_argument("WILD card must be WILD or WILDDRAWFOUR"); - } - if ((value == CardType::WILD || value == CardType::WILDDRAWFOUR) && color != CardColor::WILD) { - throw std::invalid_argument("WILD or WILDDRAWFOUR muse be WILD"); - } - this->color_ = color; - this->type_ = value; - } + Card::Card(const CardColor color, const CardType type) : color_(color), type_(type) {} - CardColor Card::getColor() const { + CardColor Card::getColor() const + { return this->color_; } - CardType Card::getType() const { + CardType Card::getType() const + { return this->type_; } - std::string Card::colorToString() const { + std::string Card::colorToString() const + { + switch (this->type_) { + case CardType::WILD: + case CardType::WILDDRAWFOUR: return "Wild"; + } switch (this->color_) { case CardColor::RED: return "Red"; case CardColor::GREEN: return "Green"; case CardColor::BLUE: return "Blue"; case CardColor::YELLOW: return "Yellow"; - case CardColor::WILD: return "Wild"; default: throw std::invalid_argument("Invalid card color"); } } - std::string Card::typeToString() const { + std::string Card::typeToString() const + { switch (this->type_) { case CardType::NUM0: return "0"; case CardType::NUM1: return "1"; @@ -62,14 +60,16 @@ namespace UNO::GAME { } - std::string Card::toString() const { - if (this->color_ == CardColor::WILD) { + std::string Card::toString() const + { + if (this->type_ == CardType::WILD || this->type_ == CardType::WILDDRAWFOUR) { return this->typeToString(); } return std::format("{} {}", this->colorToString(), this->typeToString()); } - bool Card::operator<(const Card &other) const { + bool Card::operator<(const Card &other) const + { if (this->color_ != other.color_) { return this->color_ < other.color_; } @@ -78,13 +78,13 @@ namespace UNO::GAME { bool Card::canBePlayedOn(const Card &other) const { - if (other.getType()== CardType::DRAW2 && this->type_ != CardType::DRAW2 && this->type_ != CardType::WILDDRAWFOUR) { + if (other.getType() == CardType::DRAW2 && this->type_ != CardType::DRAW2 && this->type_ != CardType::WILDDRAWFOUR) { return false; } if (other.getType() == CardType::WILDDRAWFOUR && this->type_ != CardType::WILDDRAWFOUR) { return false; } - if (this->color_ == CardColor::WILD) { + if (this->type_ == CardType::WILD || this->type_ == CardType::WILDDRAWFOUR) { return true; } if (this->color_ == other.getColor() || this->type_ == other.getType()) { @@ -93,4 +93,4 @@ namespace UNO::GAME { return false; } -} +} // namespace UNO::GAME diff --git a/src/game/Card.h b/src/game/Card.h index a30012d..e85c457 100644 --- a/src/game/Card.h +++ b/src/game/Card.h @@ -15,12 +15,12 @@ namespace UNO::GAME { /** * @brief 卡牌颜色 */ - enum class CardColor : uint8_t { RED, YELLOW, BLUE, GREEN, WILD }; + enum class CardColor : uint8_t { RED, YELLOW, BLUE, GREEN }; /** * 所有颜色 */ - constexpr std::array AllColors = {CardColor::RED, CardColor::YELLOW, CardColor::BLUE, CardColor::GREEN, CardColor::WILD}; + constexpr std::array AllColors = {CardColor::RED, CardColor::YELLOW, CardColor::BLUE, CardColor::GREEN}; /** * @brief 卡牌类型 @@ -30,21 +30,21 @@ namespace UNO::GAME { /** * 所有类型 */ - constexpr std::array AllTypes = {CardType::NUM0, - CardType::NUM1, - CardType::NUM2, - CardType::NUM3, - CardType::NUM4, - CardType::NUM5, - CardType::NUM6, - CardType::NUM7, - CardType::NUM8, - CardType::NUM9, - CardType::SKIP, - CardType::REVERSE, - CardType::DRAW2, - CardType::WILD, - CardType::WILDDRAWFOUR}; + constexpr std::array AllTypes = {CardType::NUM0, + CardType::NUM1, + CardType::NUM2, + CardType::NUM3, + CardType::NUM4, + CardType::NUM5, + CardType::NUM6, + CardType::NUM7, + CardType::NUM8, + CardType::NUM9, + CardType::SKIP, + CardType::REVERSE, + CardType::DRAW2, + CardType::WILD, + CardType::WILDDRAWFOUR}; /** * @brief 卡牌类 diff --git a/src/game/CardTile.cpp b/src/game/CardTile.cpp index a407698..ca62164 100644 --- a/src/game/CardTile.cpp +++ b/src/game/CardTile.cpp @@ -80,7 +80,7 @@ namespace UNO::GAME { { for (const auto color : AllColors) { for (const auto type : AllTypes) { - if (color != CardColor::WILD && type != CardType::WILD && type != CardType::WILDDRAWFOUR) { + if (type != CardType::WILD && type != CardType::WILDDRAWFOUR) { for (int i = 1 + (type == CardType::NUM0); i <= 2; i++) { this->pushBack(color, type); } @@ -88,8 +88,8 @@ namespace UNO::GAME { } } for (int i = 0; i < 4; i++) { - this->pushBack(CardColor::WILD, CardType::WILD); - this->pushBack(CardColor::WILD, CardType::WILDDRAWFOUR); + this->pushBack(CardColor::RED, CardType::WILD); + this->pushBack(CardColor::RED, CardType::WILDDRAWFOUR); } this->shuffle(); }