refactor(game): standardize card color and type serialization format

This commit is contained in:
Kieran Kihn
2025-12-10 21:41:30 +08:00
parent 59f8ac8186
commit 8d9f76eab3
2 changed files with 19 additions and 19 deletions

View File

@@ -25,10 +25,10 @@ namespace UNO::GAME {
std::string Card::colorToString() const
{
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::RED: return "red";
case CardColor::GREEN: return "green";
case CardColor::BLUE: return "blue";
case CardColor::YELLOW: return "yellow";
default: throw std::invalid_argument("Invalid card color");
}
}
@@ -46,11 +46,11 @@ namespace UNO::GAME {
case CardType::NUM7: return "7";
case CardType::NUM8: return "8";
case CardType::NUM9: return "9";
case CardType::SKIP: return "Skip";
case CardType::REVERSE: return "Reverse";
case CardType::DRAW2: return "Draw 2";
case CardType::WILD: return "Wild";
case CardType::WILDDRAWFOUR: return "Wild Draw 4";
case CardType::SKIP: return "skip";
case CardType::REVERSE: return "reverse";
case CardType::DRAW2: return "draw_two";
case CardType::WILD: return "wild_wild";
case CardType::WILDDRAWFOUR: return "wild_draw_four";
default: throw std::invalid_argument("Invalid card type");
}
}
@@ -61,7 +61,7 @@ namespace UNO::GAME {
if (this->type_ == CardType::WILD || this->type_ == CardType::WILDDRAWFOUR) {
return this->typeToString();
}
return std::format("{} {}", this->colorToString(), this->typeToString());
return std::format("{}_{}", this->colorToString(), this->typeToString());
}
bool Card::operator<(const Card &other) const

View File

@@ -118,16 +118,16 @@ namespace UNO::NETWORK {
GAME::CardColor MessageSerializer::deserializeCardColor(const std::string &cardColor)
{
if (cardColor == "Red") {
if (cardColor == "red") {
return GAME::CardColor::RED;
}
if (cardColor == "Blue") {
if (cardColor == "blue") {
return GAME::CardColor::BLUE;
}
if (cardColor == "Green") {
if (cardColor == "green") {
return GAME::CardColor::GREEN;
}
if (cardColor == "Yellow") {
if (cardColor == "yellow") {
return GAME::CardColor::YELLOW;
}
throw std::invalid_argument("Invalid card color: '" + cardColor + "'. Expected: Red, Blue, Green, or Yellow");
@@ -165,19 +165,19 @@ namespace UNO::NETWORK {
if (cardType == "9") {
return GAME::CardType::NUM9;
}
if (cardType == "Skip") {
if (cardType == "skip") {
return GAME::CardType::SKIP;
}
if (cardType == "Reverse") {
if (cardType == "reverse") {
return GAME::CardType::REVERSE;
}
if (cardType == "Draw 2") {
if (cardType == "draw_two") {
return GAME::CardType::DRAW2;
}
if (cardType == "Wild") {
if (cardType == "wild_wild") {
return GAME::CardType::WILD;
}
if (cardType == "Wild Draw 4") {
if (cardType == "wild_draw_four") {
return GAME::CardType::WILDDRAWFOUR;
}
throw std::invalid_argument("Invalid card type: '" + cardType + "'. Expected: 0-9, Skip, Reverse, Draw 2, Wild, or Wild Draw 4");