mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
fix(game): refactor ServerPlayerState and update ServerGameState::updateStateByCard logic
- Replaced `handCard_` with `handCard` in `ServerPlayerState`. - Updated `updateStateByCard` in `ServerGameState` to handle card validation and player actions. - Made `updateStateByCard` virtual in `GameState`.
This commit is contained in:
@@ -47,20 +47,31 @@ namespace UNO::GAME {
|
||||
}
|
||||
|
||||
ServerPlayerState::ServerPlayerState(std::string name, size_t remainingCardCount, bool isUno, HandCard *handCard) :
|
||||
PlayerState(std::move(name), remainingCardCount, isUno), handCard_(handCard)
|
||||
PlayerState(std::move(name), remainingCardCount, isUno), handCard(handCard)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename... T>
|
||||
void ServerPlayerState::draw(T... args)
|
||||
{
|
||||
this->handCard_->draw(args...);
|
||||
}
|
||||
|
||||
ClientGameState::ClientGameState(GameStatus gameStatus, Player player) : GameState(gameStatus), player(std::move(player)) {}
|
||||
|
||||
ServerGameState::ServerGameState() : GameState(GameStatus::WAITING_PLAYERS_TO_JOIN) {}
|
||||
|
||||
void ServerGameState::updateStateByCard(const Card &card)
|
||||
{
|
||||
if (this->discardPile_.isEmpty() == false && card.canBePlayedOn(this->discardPile_.getFront()) == false) {
|
||||
throw std::invalid_argument("Card cannot be played");
|
||||
}
|
||||
|
||||
const auto &handCardSet = this->getCurrentPlayer()->handCard->getCards();
|
||||
for (auto it = handCardSet.begin(); it != handCardSet.end(); it++) {
|
||||
if (card.getType() == it->getType() && (card.getType() == CardType::WILD || card.getType() == CardType::WILDDRAWFOUR || card.getColor() == it->getColor())) {
|
||||
this->getCurrentPlayer()->handCard->play(it);
|
||||
}
|
||||
}
|
||||
|
||||
GameState::updateStateByCard(card);
|
||||
}
|
||||
|
||||
|
||||
void ServerGameState::updateStateByDraw()
|
||||
{
|
||||
if (this->drawCount_ == 0) {
|
||||
|
||||
@@ -66,14 +66,10 @@ namespace UNO::GAME {
|
||||
* (供服务端使用)玩家状态
|
||||
*/
|
||||
class ServerPlayerState : public PlayerState {
|
||||
private:
|
||||
HandCard *handCard_;
|
||||
|
||||
public:
|
||||
explicit ServerPlayerState(std::string name, size_t remainingCardCount, bool isUno, HandCard *handCard);
|
||||
|
||||
template<typename... T>
|
||||
void draw(T... args);
|
||||
HandCard *handCard;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -156,7 +152,7 @@ namespace UNO::GAME {
|
||||
* 由于用户出牌而改变状态
|
||||
* @param card 用户出的牌
|
||||
*/
|
||||
void updateStateByCard(const Card &card);
|
||||
void virtual updateStateByCard(const Card &card);
|
||||
|
||||
/**
|
||||
* 由于用户摸牌而改变状态
|
||||
@@ -287,6 +283,12 @@ namespace UNO::GAME {
|
||||
public:
|
||||
ServerGameState();
|
||||
|
||||
/**
|
||||
* 由于用户出牌而改变状态
|
||||
* @param card 用户出的牌
|
||||
*/
|
||||
void updateStateByCard(const Card &card) override;
|
||||
|
||||
/**
|
||||
* 由于用户摸牌而改变状态
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user