From a025461be8c465ce13a8a25d52a6615f1eed34f6 Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Mon, 17 Nov 2025 12:32:39 +0800 Subject: [PATCH] fix(game): move `GameState` implementation from `.cpp` to `.h` to resolve linkage issues --- src/game/GameState.cpp | 101 ----------------------------------------- src/game/GameState.h | 100 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 101 deletions(-) diff --git a/src/game/GameState.cpp b/src/game/GameState.cpp index e619018..b07240c 100644 --- a/src/game/GameState.cpp +++ b/src/game/GameState.cpp @@ -57,105 +57,6 @@ namespace UNO::GAME { this->handCard_->draw(args...); } - template - GameState::GameState(GameStatus gameStatus) : - gameStatus_(gameStatus), isReversed_(false), drawCount_(0), players_(), currentPlayer_(players_.begin()) - { - } - - template - const std::vector &GameState::getPlayers() const - { - return this->players_; - } - - template - std::vector::iterator GameState::getCurrentPlayer() const - { - return this->currentPlayer_; - } - - template - const DiscardPile &GameState::getDiscardPile() const - { - return this->discardPile_; - } - - template - bool GameState::getIsReversed() const - { - return this->isReversed_; - } - - template - void GameState::addPlayer(PlayerStateType playerState) - { - this->currentPlayer_ = this->players_.push_back(std::move(playerState), this->currentPlayer_); - } - - template - void GameState::nextPlayer() - { - if (this->isReversed_ == false) { - if (this->currentPlayer_ == this->players_.end()) { - this->currentPlayer_ = std::next(this->currentPlayer_); - this->currentPlayer_ = this->players_.begin(); - } - } - else { - if (this->currentPlayer_ == this->players_.begin()) { - this->currentPlayer_ = std::prev(this->players_.end()); - } - else { - this->currentPlayer_ = std::prev(this->currentPlayer_); - } - } - } - - template - void GameState::clearPlayers() - { - this->players_.clear(); - this->currentPlayer_ = this->players_.begin(); - } - - template - void GameState::reverse() - { - this->isReversed_ ^= 1; - } - - template - void GameState::updateStateByCard(const Card &card) - { - if (this->discardPile_.isEmpty() == false && card.canBePlayedOn(this->discardPile_.getFront()) == false) { - throw std::invalid_argument("Card cannot be played"); - } - this->currentPlayer_->setRemainingCardCount(this->currentPlayer_->getRemainingCardCount() - 1); - if (card.getType() == CardType::DRAW2) { - this->drawCount_ += 2; - } - if (card.getType() == CardType::WILDDRAWFOUR) { - this->drawCount_ += 4; - } - if (card.getType() == CardType::REVERSE) { - this->reverse(); - } - if (card.getType() == CardType::SKIP) { - this->nextPlayer(); - } - this->nextPlayer(); - this->discardPile_.add(card); - } - - template - void GameState::updateStateByDraw() - { - if (this->drawCount_ != 0) { - this->drawCount_ = 0; - } - } - ClientGameState::ClientGameState(GameStatus gameStatus, Player player) : GameState(gameStatus), player(std::move(player)) {} ServerGameState::ServerGameState() : GameState(GameStatus::WAITING_PLAYERS_TO_JOIN) {} @@ -168,6 +69,4 @@ namespace UNO::GAME { this->currentPlayer_->draw(this->deck_.draw(this->drawCount_)); this->drawCount_ = 0; } - - } // namespace UNO::GAME \ No newline at end of file diff --git a/src/game/GameState.h b/src/game/GameState.h index 5a56a2b..0e967dc 100644 --- a/src/game/GameState.h +++ b/src/game/GameState.h @@ -9,6 +9,7 @@ #include "CardTile.h" #include "Player.h" +#include #include namespace UNO::GAME { @@ -161,6 +162,105 @@ namespace UNO::GAME { void virtual updateStateByDraw(); }; + template + GameState::GameState(GameStatus gameStatus) : + gameStatus_(gameStatus), isReversed_(false), drawCount_(0), players_(), currentPlayer_(players_.begin()) + { + } + + template + const std::vector &GameState::getPlayers() const + { + return this->players_; + } + + template + std::vector::iterator GameState::getCurrentPlayer() const + { + return this->currentPlayer_; + } + + template + const DiscardPile &GameState::getDiscardPile() const + { + return this->discardPile_; + } + + template + bool GameState::getIsReversed() const + { + return this->isReversed_; + } + + template + void GameState::addPlayer(PlayerStateType playerState) + { + this->currentPlayer_ = this->players_.push_back(std::move(playerState), this->currentPlayer_); + } + + template + void GameState::nextPlayer() + { + if (this->isReversed_ == false) { + if (this->currentPlayer_ == this->players_.end()) { + this->currentPlayer_ = std::next(this->currentPlayer_); + this->currentPlayer_ = this->players_.begin(); + } + } + else { + if (this->currentPlayer_ == this->players_.begin()) { + this->currentPlayer_ = std::prev(this->players_.end()); + } + else { + this->currentPlayer_ = std::prev(this->currentPlayer_); + } + } + } + + template + void GameState::clearPlayers() + { + this->players_.clear(); + this->currentPlayer_ = this->players_.begin(); + } + + template + void GameState::reverse() + { + this->isReversed_ ^= 1; + } + + template + void GameState::updateStateByCard(const Card &card) + { + if (this->discardPile_.isEmpty() == false && card.canBePlayedOn(this->discardPile_.getFront()) == false) { + throw std::invalid_argument("Card cannot be played"); + } + this->currentPlayer_->setRemainingCardCount(this->currentPlayer_->getRemainingCardCount() - 1); + if (card.getType() == CardType::DRAW2) { + this->drawCount_ += 2; + } + if (card.getType() == CardType::WILDDRAWFOUR) { + this->drawCount_ += 4; + } + if (card.getType() == CardType::REVERSE) { + this->reverse(); + } + if (card.getType() == CardType::SKIP) { + this->nextPlayer(); + } + this->nextPlayer(); + this->discardPile_.add(card); + } + + template + void GameState::updateStateByDraw() + { + if (this->drawCount_ != 0) { + this->drawCount_ = 0; + } + } + class ClientGameState final : public GameState { public: Player player;