From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/hot_potato/game.cc | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 graphs/cpp/hot_potato/game.cc (limited to 'graphs/cpp/hot_potato/game.cc') diff --git a/graphs/cpp/hot_potato/game.cc b/graphs/cpp/hot_potato/game.cc new file mode 100644 index 0000000..3e0889a --- /dev/null +++ b/graphs/cpp/hot_potato/game.cc @@ -0,0 +1,48 @@ +// +// Created by martial.simon on 2/25/25. +// + +#include "game.hh" + +#include +#include +void Game::add_player(const std::string& name, size_t nb_presses) +{ + auto new_player = std::make_unique(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_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(); + } + } +} \ No newline at end of file -- cgit v1.2.3