From e5304b8c6ca4a6a326514e9f2b17ccbec6589a4d Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Tue, 2 Dec 2025 11:08:42 +0800 Subject: [PATCH] refactor(game): simplify `GameState` construction and initialization - Removed `GameStatus` dependency from `GameState` constructors. - Adjusted `ClientGameState` and `ServerGameState` constructors accordingly. - Simplified `ServerGameState::init()` to handle card initialization and player actions. - Updated tests to reflect these changes. --- src/game/GameState.cpp | 10 ++++++++-- src/game/GameState.h | 18 +++--------------- test/unit/game/GameStateTest.cpp | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/game/GameState.cpp b/src/game/GameState.cpp index 8829746..3603578 100644 --- a/src/game/GameState.cpp +++ b/src/game/GameState.cpp @@ -97,7 +97,7 @@ namespace UNO::GAME { return this->handCard_.isEmpty(); } - ClientGameState::ClientGameState(GameStatus gameStatus, std::string name) : GameState(gameStatus), player_(std::move(name)) {} + ClientGameState::ClientGameState(std::string name) : player_(std::move(name)) {} const std::multiset &ClientGameState::getCards() const { @@ -124,7 +124,7 @@ namespace UNO::GAME { return this->player_.isEmpty(); } - ServerGameState::ServerGameState() : GameState(GameStatus::WAITING_PLAYERS_TO_JOIN) {} + ServerGameState::ServerGameState() = default; void ServerGameState::init() { @@ -133,6 +133,12 @@ namespace UNO::GAME { discardPile_.add(deck_.draw()); } + for (auto &player : this->players_) { + while (player.isEmpty() == false) { + player.play(*player.getCards().begin()); + } + } + for (size_t i = 0; i < 7; i++) { for (auto &player : this->players_) { player.draw(1, this->deck_.draw(1)); diff --git a/src/game/GameState.h b/src/game/GameState.h index 3002233..01ad70b 100644 --- a/src/game/GameState.h +++ b/src/game/GameState.h @@ -119,16 +119,6 @@ namespace UNO::GAME { [[nodiscard]] bool isEmpty() const; }; - /** - * 游戏状态 - */ - enum class GameStatus : uint8_t { - WAITING_PLAYERS_TO_JOIN, - WAITING_PLAYERS_TO_START, - WAITING_PLAYERS_TO_NEXT_TURN, - WAITING_PLAYERS_TO_NEXT_ROUND - }; - /** * 保证模板使用的是 PlayerState 的派生类 */ @@ -142,8 +132,7 @@ namespace UNO::GAME { template class GameState { protected: - explicit GameState(GameStatus gameStatus); - GameStatus gameStatus_; + explicit GameState(); DiscardPile discardPile_; bool isReversed_; size_t drawCount_; @@ -208,8 +197,7 @@ namespace UNO::GAME { }; template - GameState::GameState(GameStatus gameStatus) : - gameStatus_(gameStatus), isReversed_(false), drawCount_(0), players_(), currentPlayer_(players_.begin()) + GameState::GameState() : isReversed_(false), drawCount_(0), players_(), currentPlayer_(players_.begin()) { } @@ -321,7 +309,7 @@ namespace UNO::GAME { Player player_; public: - ClientGameState(GameStatus gameStatus, std::string name); + ClientGameState(std::string name); /** * 获得当前手牌 diff --git a/test/unit/game/GameStateTest.cpp b/test/unit/game/GameStateTest.cpp index 8fb02d0..8d42bbd 100644 --- a/test/unit/game/GameStateTest.cpp +++ b/test/unit/game/GameStateTest.cpp @@ -13,7 +13,7 @@ TEST(game_state_test, game_state_test_1) { - UNO::GAME::ClientGameState clientGameState(UNO::GAME::GameStatus::WAITING_PLAYERS_TO_NEXT_TURN, std::string("lzh")); + UNO::GAME::ClientGameState clientGameState(std::string("lzh")); UNO::GAME::ClientPlayerState playerState1("pkq", 100, false); UNO::GAME::ClientPlayerState playerState2("kpq", 100, false);