summaryrefslogtreecommitdiff
path: root/graphs/cpp/hot_potato/game.cc
blob: 3e0889a9fffee72e557acdff49d4cd35254c3fdc (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
//
// Created by martial.simon on 2/25/25.
//

#include "game.hh"

#include <iostream>
#include <stdexcept>
void Game::add_player(const std::string& name, size_t nb_presses)
{
    auto new_player = std::make_unique<Player>(name, nb_presses);
    if (head_ == nullptr)
    {
        head_ = std::move(new_player);
        tail_ = head_.get();
    }
    else
    {
        tail_->set_next(std::move(new_player));
        tail_ = tail_->get_next();
    }
}
void Game::play(int bomb_ticks)
{
    if (!head_ || !head_->get_next())
        throw std::runtime_error{ "Not enough players." };
    head_->set_bomb(std::make_unique<Bomb>(bomb_ticks));
    Player* p = head_.get();
    while (true)
    {
        p->press_bomb();
        if (p->is_dead())
        {
            std::cout << p->get_name() << " has exploded.\n";
            return;
        }
        if (p == tail_)
        {
            p->pass_bomb(*head_);
            p = head_.get();
        }
        else
        {
            p->pass_bomb(*p->get_next());
            p = p->get_next();
        }
    }
}