mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
refactor(game): simplify card play logic
- Replaced iterator-based `play` methods with a single card-based method. - Streamlined logic in `ServerPlayerState`, `ClientGameState`, and `HandCard`.
This commit is contained in:
@@ -56,11 +56,6 @@ namespace UNO::GAME {
|
|||||||
PlayerState::draw(n, cards);
|
PlayerState::draw(n, cards);
|
||||||
}
|
}
|
||||||
|
|
||||||
Card ClientPlayerState::play(const Card &card)
|
|
||||||
{
|
|
||||||
return PlayerState::play(card);
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerPlayerState::ServerPlayerState(std::string name, size_t remainingCardCount, bool isUno) :
|
ServerPlayerState::ServerPlayerState(std::string name, size_t remainingCardCount, bool isUno) :
|
||||||
PlayerState(std::move(name), remainingCardCount, isUno)
|
PlayerState(std::move(name), remainingCardCount, isUno)
|
||||||
{
|
{
|
||||||
@@ -80,16 +75,7 @@ namespace UNO::GAME {
|
|||||||
|
|
||||||
Card ServerPlayerState::play(const Card &card)
|
Card ServerPlayerState::play(const Card &card)
|
||||||
{
|
{
|
||||||
for (auto it = this->handCard_.getCards().begin();; it++) {
|
this->handCard_.play(card);
|
||||||
if (it == this->handCard_.getCards().end()) {
|
|
||||||
throw std::invalid_argument("Card not found in hand");
|
|
||||||
}
|
|
||||||
if (card.getType() == it->getType()
|
|
||||||
&& (card.getType() == CardType::WILD || card.getType() == CardType::WILDDRAWFOUR || card.getColor() == it->getColor())) {
|
|
||||||
this->handCard_.play(it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return PlayerState::play(card);
|
return PlayerState::play(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,9 +107,9 @@ namespace UNO::GAME {
|
|||||||
this->player_.draw(cards);
|
this->player_.draw(cards);
|
||||||
}
|
}
|
||||||
|
|
||||||
Card ClientGameState::play(const std::multiset<Card>::iterator &it)
|
void ClientGameState::play(const Card &card)
|
||||||
{
|
{
|
||||||
return this->player_.play(it);
|
this->player_.play(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClientGameState::isEmpty() const
|
bool ClientGameState::isEmpty() const
|
||||||
|
|||||||
@@ -75,11 +75,6 @@ namespace UNO::GAME {
|
|||||||
* @param cards 摸的牌
|
* @param cards 摸的牌
|
||||||
*/
|
*/
|
||||||
void draw(size_t n, const std::vector<Card> &cards) override;
|
void draw(size_t n, const std::vector<Card> &cards) override;
|
||||||
|
|
||||||
/**
|
|
||||||
* 出一张牌
|
|
||||||
*/
|
|
||||||
Card play(const Card &card) override;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -337,10 +332,9 @@ namespace UNO::GAME {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出一张牌
|
* 打出一张牌
|
||||||
* @param it 要打出的手牌的迭代器
|
* @param card 要打出的手牌
|
||||||
* @return 打出的手牌
|
|
||||||
*/
|
*/
|
||||||
Card play(const std::multiset<Card>::iterator &it);
|
void play(const Card &card);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 手牌是否为空
|
* @return 手牌是否为空
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace UNO::GAME {
|
namespace UNO::GAME {
|
||||||
@@ -28,11 +29,24 @@ namespace UNO::GAME {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Card HandCard::play(const std::multiset<Card>::iterator &it)
|
void HandCard::play(const std::multiset<Card>::iterator &it)
|
||||||
{
|
{
|
||||||
const Card card = *it;
|
this->cards_.erase(it);
|
||||||
cards_.erase(it);
|
}
|
||||||
return card;
|
|
||||||
|
|
||||||
|
void HandCard::play(const Card &card)
|
||||||
|
{
|
||||||
|
for (auto it = cards_.begin();; it++) {
|
||||||
|
if (it == cards_.end()) {
|
||||||
|
throw std::invalid_argument("Card not found in hand");
|
||||||
|
}
|
||||||
|
if (card.getType() == it->getType()
|
||||||
|
&& (card.getType() == CardType::WILD || card.getType() == CardType::WILDDRAWFOUR || card.getColor() == it->getColor())) {
|
||||||
|
this->play(it);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HandCard::isEmpty() const
|
bool HandCard::isEmpty() const
|
||||||
@@ -67,8 +81,10 @@ namespace UNO::GAME {
|
|||||||
return this->handCard_.isEmpty();
|
return this->handCard_.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
Card Player::play(const std::multiset<Card>::iterator &it)
|
void Player::play(const Card &card)
|
||||||
{
|
{
|
||||||
return this->handCard_.play(it);
|
return this->handCard_.play(card);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
} // namespace UNO::GAME
|
} // namespace UNO::GAME
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
*/
|
*/
|
||||||
#ifndef UNO_GAME_PLAYER_H
|
#ifndef UNO_GAME_PLAYER_H
|
||||||
#define UNO_GAME_PLAYER_H
|
#define UNO_GAME_PLAYER_H
|
||||||
#include <array>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -22,6 +21,13 @@ namespace UNO::GAME {
|
|||||||
private:
|
private:
|
||||||
std::multiset<Card> cards_;
|
std::multiset<Card> cards_;
|
||||||
|
|
||||||
|
private:
|
||||||
|
/**
|
||||||
|
* 打出一张牌
|
||||||
|
* @param it 要打出的手牌的迭代器
|
||||||
|
*/
|
||||||
|
void play(const std::multiset<Card>::iterator &it);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit HandCard();
|
explicit HandCard();
|
||||||
|
|
||||||
@@ -45,10 +51,9 @@ namespace UNO::GAME {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出一张牌
|
* 打出一张牌
|
||||||
* @param it 要打出的手牌的迭代器
|
* @param card 要打出的手牌
|
||||||
* @return 打出的手牌
|
|
||||||
*/
|
*/
|
||||||
Card play(const std::multiset<Card>::iterator &it);
|
void play(const Card &card);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 手牌是否为空
|
* @return 手牌是否为空
|
||||||
@@ -92,10 +97,9 @@ namespace UNO::GAME {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 打出一张牌
|
* 打出一张牌
|
||||||
* @param it 要打出的手牌的迭代器
|
* @param card 要打出的手牌
|
||||||
* @return 打出的手牌
|
|
||||||
*/
|
*/
|
||||||
Card play(const std::multiset<Card>::iterator &it);
|
void play(const Card &card);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 手牌是否为空
|
* @return 手牌是否为空
|
||||||
|
|||||||
Reference in New Issue
Block a user