From da0f2160ce236d3899f5397f9462fc8300be6e54 Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Sun, 16 Nov 2025 19:52:47 +0800 Subject: [PATCH] feat(game): add `canBePlayedOn` for `Card` --- src/game/Card.cpp | 18 ++++++++++++++++++ src/game/Card.h | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/src/game/Card.cpp b/src/game/Card.cpp index 9abf7a7..c6b6d46 100644 --- a/src/game/Card.cpp +++ b/src/game/Card.cpp @@ -75,4 +75,22 @@ namespace UNO::GAME { } return this->type_ < other.type_; } + + bool Card::canBePlayedOn(const Card &other) const + { + 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) { + return true; + } + if (this->color_ == other.getColor() || this->type_ == other.getType()) { + return true; + } + return false; + } + } diff --git a/src/game/Card.h b/src/game/Card.h index 3082265..a30012d 100644 --- a/src/game/Card.h +++ b/src/game/Card.h @@ -86,6 +86,12 @@ namespace UNO::GAME { * 比较运算符,用于排序 */ bool operator<(const Card &other) const; + + /** + * @param other 另一张牌 + * @return 是否能在打出另一张牌后打出 + */ + [[nodiscard]] bool canBePlayedOn(const Card &other) const; }; } // namespace UNO::GAME