diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/hot_potato/player.cc | |
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/hot_potato/player.cc')
| -rw-r--r-- | graphs/cpp/hot_potato/player.cc | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/graphs/cpp/hot_potato/player.cc b/graphs/cpp/hot_potato/player.cc new file mode 100644 index 0000000..3ab0f28 --- /dev/null +++ b/graphs/cpp/hot_potato/player.cc @@ -0,0 +1,63 @@ +// +// Created by martial.simon on 2/25/25. +// + +#include "player.hh" + +#include <iostream> +Player::Player(const std::string& name, size_t nb_presses) + : name_{ name } + , bomb_{ nullptr } + , nb_presses_{ nb_presses } + , next_{ nullptr } +{} +Player* Player::get_next() const +{ + return next_.get(); +} +void Player::set_next(std::unique_ptr<Player> next) +{ + next_ = std::move(next); +} +void Player::pass_bomb(Player& receiver) +{ + if (!this->has_bomb() || receiver.has_bomb()) + throw std::runtime_error{ + "Passer doesn't have the bomb or receiver already has it." + }; + std::cout << this->get_name() << " passes the bomb to " + << receiver.get_name() << ".\n"; + receiver.set_bomb(std::move(this->bomb_)); + this->bomb_ = nullptr; +} +void Player::press_bomb() +{ + if (bomb_ == nullptr) + throw std::runtime_error{ + "Can't press bomb if player doesn't have it." + }; + if (bomb_->has_exploded()) + return; + for (size_t i = 0; i < nb_presses_ && !bomb_->has_exploded(); ++i) + { + bomb_->tick(); + } +} +void Player::set_bomb(std::unique_ptr<Bomb> bomb) +{ + this->bomb_ = std::move(bomb); +} +const std::string& Player::get_name() const +{ + return name_; +} +bool Player::has_bomb() const +{ + return bomb_ != nullptr; +} +bool Player::is_dead() const +{ + if (has_bomb()) + return bomb_->has_exploded(); + return false; +}
\ No newline at end of file |
