blob: 3ab0f28c8ebc77beb168224462f934256dd737f2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
}
|