fix(game): remove CardColor::WILD and update wild card handling logic

This commit is contained in:
Kieran Kihn
2025-11-17 14:38:58 +08:00
parent c3a5a3ee5f
commit 562ddb25b9
3 changed files with 41 additions and 41 deletions

View File

@@ -10,37 +10,35 @@
#include <stdexcept> #include <stdexcept>
namespace UNO::GAME { namespace UNO::GAME {
Card::Card(const CardColor color, const CardType value) { Card::Card(const CardColor color, const CardType type) : color_(color), type_(type) {}
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;
}
CardColor Card::getColor() const { CardColor Card::getColor() const
{
return this->color_; return this->color_;
} }
CardType Card::getType() const { CardType Card::getType() const
{
return this->type_; 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_) { switch (this->color_) {
case CardColor::RED: return "Red"; case CardColor::RED: return "Red";
case CardColor::GREEN: return "Green"; case CardColor::GREEN: return "Green";
case CardColor::BLUE: return "Blue"; case CardColor::BLUE: return "Blue";
case CardColor::YELLOW: return "Yellow"; case CardColor::YELLOW: return "Yellow";
case CardColor::WILD: return "Wild";
default: throw std::invalid_argument("Invalid card color"); default: throw std::invalid_argument("Invalid card color");
} }
} }
std::string Card::typeToString() const { std::string Card::typeToString() const
{
switch (this->type_) { switch (this->type_) {
case CardType::NUM0: return "0"; case CardType::NUM0: return "0";
case CardType::NUM1: return "1"; case CardType::NUM1: return "1";
@@ -62,14 +60,16 @@ namespace UNO::GAME {
} }
std::string Card::toString() const { std::string Card::toString() const
if (this->color_ == CardColor::WILD) { {
if (this->type_ == CardType::WILD || this->type_ == CardType::WILDDRAWFOUR) {
return this->typeToString(); return this->typeToString();
} }
return std::format("{} {}", this->colorToString(), 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_) { if (this->color_ != other.color_) {
return this->color_ < other.color_; return this->color_ < other.color_;
} }
@@ -78,13 +78,13 @@ namespace UNO::GAME {
bool Card::canBePlayedOn(const Card &other) const 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; return false;
} }
if (other.getType() == CardType::WILDDRAWFOUR && this->type_ != CardType::WILDDRAWFOUR) { if (other.getType() == CardType::WILDDRAWFOUR && this->type_ != CardType::WILDDRAWFOUR) {
return false; return false;
} }
if (this->color_ == CardColor::WILD) { if (this->type_ == CardType::WILD || this->type_ == CardType::WILDDRAWFOUR) {
return true; return true;
} }
if (this->color_ == other.getColor() || this->type_ == other.getType()) { if (this->color_ == other.getColor() || this->type_ == other.getType()) {
@@ -93,4 +93,4 @@ namespace UNO::GAME {
return false; return false;
} }
} } // namespace UNO::GAME

View File

@@ -15,12 +15,12 @@ namespace UNO::GAME {
/** /**
* @brief 卡牌颜色 * @brief 卡牌颜色
*/ */
enum class CardColor : uint8_t { RED, YELLOW, BLUE, GREEN, WILD }; enum class CardColor : uint8_t { RED, YELLOW, BLUE, GREEN };
/** /**
* 所有颜色 * 所有颜色
*/ */
constexpr std::array<CardColor, 5> AllColors = {CardColor::RED, CardColor::YELLOW, CardColor::BLUE, CardColor::GREEN, CardColor::WILD}; constexpr std::array AllColors = {CardColor::RED, CardColor::YELLOW, CardColor::BLUE, CardColor::GREEN};
/** /**
* @brief 卡牌类型 * @brief 卡牌类型
@@ -30,7 +30,7 @@ namespace UNO::GAME {
/** /**
* 所有类型 * 所有类型
*/ */
constexpr std::array<CardType, 15> AllTypes = {CardType::NUM0, constexpr std::array AllTypes = {CardType::NUM0,
CardType::NUM1, CardType::NUM1,
CardType::NUM2, CardType::NUM2,
CardType::NUM3, CardType::NUM3,

View File

@@ -80,7 +80,7 @@ namespace UNO::GAME {
{ {
for (const auto color : AllColors) { for (const auto color : AllColors) {
for (const auto type : AllTypes) { 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++) { for (int i = 1 + (type == CardType::NUM0); i <= 2; i++) {
this->pushBack(color, type); this->pushBack(color, type);
} }
@@ -88,8 +88,8 @@ namespace UNO::GAME {
} }
} }
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
this->pushBack(CardColor::WILD, CardType::WILD); this->pushBack(CardColor::RED, CardType::WILD);
this->pushBack(CardColor::WILD, CardType::WILDDRAWFOUR); this->pushBack(CardColor::RED, CardType::WILDDRAWFOUR);
} }
this->shuffle(); this->shuffle();
} }