From 543137ed06745a40f9cc4f0e1cef5167cdf0afbb Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Sat, 15 Nov 2025 22:22:02 +0800 Subject: [PATCH] feat(game): add new class: Card --- src/game/Card.cpp | 71 ++++++++++++++++++++++++++++++++++++++ src/game/Card.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/game/Card.cpp create mode 100644 src/game/Card.h diff --git a/src/game/Card.cpp b/src/game/Card.cpp new file mode 100644 index 0000000..3fc2864 --- /dev/null +++ b/src/game/Card.cpp @@ -0,0 +1,71 @@ +/** + * @file Card.cpp + * + * @author Yuzhe Guo + * @date 2025.11.15 + */ + +#include "Card.h" +#include +#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; + } + + CardColor Card::getColor() const { + return this->color_; + } + + CardType Card::getType() const { + return this->type_; + } + + 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::WILD: return "Wild"; + default: throw std::invalid_argument("Invalid card color"); + } + } + + std::string Card::typeToString() const { + switch (this->type_) { + case CardType::NUM0: return "0"; + case CardType::NUM1: return "1"; + case CardType::NUM2: return "2"; + case CardType::NUM3: return "3"; + case CardType::NUM4: return "4"; + case CardType::NUM5: return "5"; + case CardType::NUM6: return "6"; + 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"; + default: throw std::invalid_argument("Invalid card type"); + } + } + + + std::string Card::toString() const { + if (this->color_ == CardColor::WILD) { + return this->typeToString(); + } + return std::format("{} {}", this->colorToString(), this->typeToString()); + } +} diff --git a/src/game/Card.h b/src/game/Card.h new file mode 100644 index 0000000..9a3f28a --- /dev/null +++ b/src/game/Card.h @@ -0,0 +1,87 @@ +/** + * @file Card.h + * @brief 卡牌类型 + * + * @author Yuzhe Guo + * @date 2025.11.15 + */ + +#ifndef UNO_GAME_CARD_H +#define UNO_GAME_CARD_H +#include +#include + +namespace UNO::GAME { + /** + * @brief 卡牌颜色 + */ + enum class CardColor : uint8_t { RED, YELLOW, BLUE, GREEN, WILD }; + + /** + * 所有颜色 + */ + constexpr std::array AllColors = {CardColor::RED, CardColor::YELLOW, CardColor::BLUE, CardColor::GREEN, CardColor::WILD}; + + /** + * @brief 卡牌类型 + */ + enum class CardType : uint8_t { NUM0, NUM1, NUM2, NUM3, NUM4, NUM5, NUM6, NUM7, NUM8, NUM9, SKIP, REVERSE, DRAW2, WILD, 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 卡牌类 + */ + class Card { + private: + CardColor color_; + CardType type_; + + public: + Card(CardColor color, CardType value); + + /** + * @return 卡牌的颜色 + */ + [[nodiscard]] CardColor getColor() const; + + /** + * @return 卡牌的类型 + */ + [[nodiscard]] CardType getType() const; + + /** + * @return 卡牌的颜色对应的字符串 + */ + [[nodiscard]] std::string colorToString() const; + + /** + * @return 卡牌的类型对应的字符串 + */ + [[nodiscard]] std::string typeToString() const; + + /** + * @return 卡牌对应的显示字符串 + */ + [[nodiscard]] std::string toString() const; + }; +} // namespace UNO::GAME + +#endif // UNO_GAME_CARD_H