summaryrefslogtreecommitdiff
path: root/graphs/cpp/hot_potato/game.cc
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/cpp/hot_potato/game.cc')
-rw-r--r--graphs/cpp/hot_potato/game.cc48
1 files changed, 48 insertions, 0 deletions
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 <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();
+ }
+ }
+} \ No newline at end of file