mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
fix(game): remove CardColor::WILD and update wild card handling logic
This commit is contained in:
@@ -10,37 +10,35 @@
|
||||
#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;
|
||||
}
|
||||
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
|
||||
|
||||
@@ -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<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 卡牌类型
|
||||
@@ -30,21 +30,21 @@ namespace UNO::GAME {
|
||||
/**
|
||||
* 所有类型
|
||||
*/
|
||||
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};
|
||||
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 卡牌类
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user