fix(game): update Card::canBePlayedOn to include draw count validation

- Modified `canBePlayedOn` method to consider `drawCount` for additional play restrictions.
- Updated `GameState::updateStateByCard` to pass `drawCount` when validating playable cards.
This commit is contained in:
Kieran Kihn
2025-11-17 21:33:36 +08:00
parent b7dcaabc28
commit b027e5c4db
3 changed files with 6 additions and 5 deletions

View File

@@ -76,12 +76,13 @@ namespace UNO::GAME {
return this->type_ < other.type_;
}
bool Card::canBePlayedOn(const Card &other) const
bool Card::canBePlayedOn(const Card &other, size_t drawCount) const
{
if (other.getType() == CardType::DRAW2 && this->type_ != CardType::DRAW2 && this->type_ != CardType::WILDDRAWFOUR) {
if (drawCount != 0 && other.getType() == CardType::DRAW2 && this->type_ != CardType::DRAW2
&& this->type_ != CardType::WILDDRAWFOUR) {
return false;
}
if (other.getType() == CardType::WILDDRAWFOUR && this->type_ != CardType::WILDDRAWFOUR) {
if (drawCount != 0 && other.getType() == CardType::WILDDRAWFOUR && this->type_ != CardType::WILDDRAWFOUR) {
return false;
}
if (this->type_ == CardType::WILD || this->type_ == CardType::WILDDRAWFOUR) {

View File

@@ -91,7 +91,7 @@ namespace UNO::GAME {
* @param other 另一张牌
* @return 是否能在打出另一张牌后打出
*/
[[nodiscard]] bool canBePlayedOn(const Card &other) const;
[[nodiscard]] bool canBePlayedOn(const Card &other, size_t drawCount) const;
};
} // namespace UNO::GAME

View File

@@ -285,7 +285,7 @@ namespace UNO::GAME {
template<PlayerStateTypeConcept PlayerStateType>
void GameState<PlayerStateType>::updateStateByCard(const Card &card)
{
if (this->discardPile_.isEmpty() == false && card.canBePlayedOn(this->discardPile_.getFront()) == false) {
if (this->discardPile_.isEmpty() == false && card.canBePlayedOn(this->discardPile_.getFront(), this->drawCount_) == false) {
throw std::invalid_argument("Card cannot be played");
}
this->currentPlayer_->play(card);