mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
feat(game): add new class: Card
This commit is contained in:
71
src/game/Card.cpp
Normal file
71
src/game/Card.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* @file Card.cpp
|
||||
*
|
||||
* @author Yuzhe Guo
|
||||
* @date 2025.11.15
|
||||
*/
|
||||
|
||||
#include "Card.h"
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
87
src/game/Card.h
Normal file
87
src/game/Card.h
Normal file
@@ -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 <array>
|
||||
#include <string>
|
||||
|
||||
namespace UNO::GAME {
|
||||
/**
|
||||
* @brief 卡牌颜色
|
||||
*/
|
||||
enum class CardColor : uint8_t { RED, YELLOW, BLUE, GREEN, WILD };
|
||||
|
||||
/**
|
||||
* 所有颜色
|
||||
*/
|
||||
constexpr std::array<CardColor, 5> 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<CardType, 15> 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
|
||||
Reference in New Issue
Block a user