mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
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:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user