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