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.
This commit is contained in:
Kieran Kihn
2025-12-02 11:08:42 +08:00
parent a650b61610
commit e5304b8c6c
3 changed files with 12 additions and 18 deletions

View File

@@ -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<Card> &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));

View File

@@ -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<PlayerStateTypeConcept PlayerStateType>
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<PlayerStateTypeConcept PlayerStateType>
GameState<PlayerStateType>::GameState(GameStatus gameStatus) :
gameStatus_(gameStatus), isReversed_(false), drawCount_(0), players_(), currentPlayer_(players_.begin())
GameState<PlayerStateType>::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);
/**
* 获得当前手牌

View File

@@ -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);