From 1b9ff0b4134fd50d555acc665abb77ed497a2a26 Mon Sep 17 00:00:00 2001 From: Kieran Kihn <114803508+kierankihn@users.noreply.github.com> Date: Sun, 16 Nov 2025 19:52:09 +0800 Subject: [PATCH] feat(game): add `Player` --- CMakeLists.txt | 2 ++ src/game/Player.cpp | 54 +++++++++++++++++++++++++++++++ src/game/Player.h | 78 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/game/Player.cpp create mode 100644 src/game/Player.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 64eef2b..59f567d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ find_package(ftxui CONFIG REQUIRED) add_executable(uno-game src/main.cpp src/game/Card.cpp src/game/CardTile.cpp + src/game/Player.cpp src/common/utils.cpp ) target_link_libraries(uno-game @@ -18,6 +19,7 @@ target_link_libraries(uno-game add_library(uno-game-lib src/game/Card.cpp src/game/CardTile.cpp + src/game/Player.cpp src/common/utils.cpp ) diff --git a/src/game/Player.cpp b/src/game/Player.cpp new file mode 100644 index 0000000..6ceb98c --- /dev/null +++ b/src/game/Player.cpp @@ -0,0 +1,54 @@ +/** + * @file + * + * @author Yuzhe Guo + * @date 2025.11.15 + */ +#include "Player.h" + +#include + +namespace UNO::GAME { + HandCard::HandCard(const std::array &cards) + { + for (const auto &card : cards) { + cards_.insert(card); + } + } + + const std::multiset &HandCard::getCards() const + { + return cards_; + } + + void HandCard::draw(const Card &card) + { + cards_.insert(card); + } + + void HandCard::draw(const std::vector &cards) + { + for (const auto &card : cards) { + cards_.insert(card); + } + } + + Card HandCard::play(const std::multiset::iterator &it) + { + const Card card = *it; + cards_.erase(it); + return card; + } + + bool HandCard::isEmpty() const + { + return cards_.empty(); + } + + Player::Player(std::string &name) : name_(std::move(name)), handCard_(nullptr) {} + + const std::string &Player::getName() const + { + return this->name_; + } +} // namespace UNO::GAME diff --git a/src/game/Player.h b/src/game/Player.h new file mode 100644 index 0000000..df7f9e2 --- /dev/null +++ b/src/game/Player.h @@ -0,0 +1,78 @@ +/** + * @file Player.h + * + * 玩家 + * + * @author Yuzhe Guo + * @date 2025.11.15 + */ +#ifndef UNO_GAME_PLAYER_H +#define UNO_GAME_PLAYER_H +#include +#include +#include + +#include "Card.h" + +namespace UNO::GAME { + /** + * 玩家手牌 + */ + class HandCard { + private: + std::multiset cards_; + + public: + explicit HandCard(const std::array &cards); + + /** + * 获得当前手牌 + * @return 当前手牌的集合 + */ + [[nodiscard]] const std::multiset &getCards() const; + + /** + * 摸一张牌 + * @param card 摸的牌 + */ + void draw(const Card &card); + + /** + * 摸多张牌 + * @param cards 摸的牌 + */ + void draw(const std::vector &cards); + + /** + * 打出一张牌 + * @param it 要打出的手牌的迭代器 + * @return 打出的手牌 + */ + Card play(const std::multiset::iterator &it); + + /** + * @return 手牌是否为空 + */ + [[nodiscard]] bool isEmpty() const; + }; + + /** + * 玩家 + */ + class Player { + private: + std::string name_; + + public: + HandCard *handCard_; + + explicit Player(std::string &name); + + /** + * @return 返回玩家名字 + */ + [[nodiscard]] const std::string &getName() const; + }; +} // namespace UNO::GAME + +#endif // UNO_GAME_PLAYER_H