summaryrefslogtreecommitdiff
path: root/graphs/cpp/hot_potato
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/cpp/hot_potato')
-rw-r--r--graphs/cpp/hot_potato/bomb.cc28
-rw-r--r--graphs/cpp/hot_potato/bomb.hh11
-rw-r--r--graphs/cpp/hot_potato/game.cc48
-rw-r--r--graphs/cpp/hot_potato/game.hh17
-rw-r--r--graphs/cpp/hot_potato/main.cc20
-rw-r--r--graphs/cpp/hot_potato/player.cc63
-rw-r--r--graphs/cpp/hot_potato/player.hh25
7 files changed, 212 insertions, 0 deletions
diff --git a/graphs/cpp/hot_potato/bomb.cc b/graphs/cpp/hot_potato/bomb.cc
new file mode 100644
index 0000000..256e3d7
--- /dev/null
+++ b/graphs/cpp/hot_potato/bomb.cc
@@ -0,0 +1,28 @@
+#include "bomb.hh"
+
+#include <iostream>
+#include <stdexcept>
+
+Bomb::Bomb(int ticks)
+{
+ if (ticks <= 0)
+ throw std::runtime_error{
+ "Number of ticks should be strictly positive"
+ };
+ max_ticks_ = ticks;
+ count_ = 0;
+}
+void Bomb::tick()
+{
+ if (count_ >= max_ticks_)
+ throw std::runtime_error{ "Bomb should have already exploded." };
+ if (count_ % 2)
+ std::cout << "Tac!\n";
+ else
+ std::cout << "Tic!\n";
+ count_++;
+}
+bool Bomb::has_exploded() const
+{
+ return count_ >= max_ticks_;
+} \ No newline at end of file
diff --git a/graphs/cpp/hot_potato/bomb.hh b/graphs/cpp/hot_potato/bomb.hh
new file mode 100644
index 0000000..41e0bd3
--- /dev/null
+++ b/graphs/cpp/hot_potato/bomb.hh
@@ -0,0 +1,11 @@
+#pragma once
+
+class Bomb {
+public:
+ Bomb(int ticks);
+ void tick();
+ bool has_exploded() const;
+private:
+ int max_ticks_;
+ int count_;
+}; \ No newline at end of file
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
diff --git a/graphs/cpp/hot_potato/game.hh b/graphs/cpp/hot_potato/game.hh
new file mode 100644
index 0000000..fd65b28
--- /dev/null
+++ b/graphs/cpp/hot_potato/game.hh
@@ -0,0 +1,17 @@
+//
+// Created by martial.simon on 2/25/25.
+//
+
+#pragma once
+#include <memory>
+
+#include "player.hh"
+
+class Game {
+public:
+ void add_player(const std::string& name, size_t nb_presses);
+ void play(int bomb_ticks);
+private:
+ std::unique_ptr<Player> head_;
+ Player* tail_ = nullptr;
+};
diff --git a/graphs/cpp/hot_potato/main.cc b/graphs/cpp/hot_potato/main.cc
new file mode 100644
index 0000000..ce491d0
--- /dev/null
+++ b/graphs/cpp/hot_potato/main.cc
@@ -0,0 +1,20 @@
+//
+// Created by martial.simon on 2/25/25.
+//
+#include "game.hh"
+int main()
+{
+ auto game = Game();
+ game.add_player("Benjamin", 2);
+ game.add_player("Andy", 1);
+ game.add_player("Geoffrey", 3);
+ game.add_player("Hugo", 1);
+ game.add_player("Maayane", 2);
+ game.add_player("Quentin", 1);
+ game.add_player("Lucas", 5); // Likes risks
+ game.add_player("Tom", 1);
+ game.add_player("Alexis", 0); // Cheater
+ game.add_player("FX", 3);
+ game.add_player("Lois", 2);
+ game.play(12);
+} \ No newline at end of file
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
diff --git a/graphs/cpp/hot_potato/player.hh b/graphs/cpp/hot_potato/player.hh
new file mode 100644
index 0000000..0439305
--- /dev/null
+++ b/graphs/cpp/hot_potato/player.hh
@@ -0,0 +1,25 @@
+#pragma once
+#include <memory>
+#include <string>
+
+#include "bomb.hh"
+
+class Player
+{
+public:
+ Player(const std::string& name, size_t nb_presses);
+ Player* get_next() const;
+ void set_next(std::unique_ptr<Player> next);
+ void pass_bomb(Player& receiver);
+ void press_bomb();
+ void set_bomb(std::unique_ptr<Bomb> bomb);
+ const std::string& get_name() const;
+ bool has_bomb() const;
+ bool is_dead() const;
+
+private:
+ std::string name_;
+ std::unique_ptr<Bomb> bomb_;
+ size_t nb_presses_;
+ std::unique_ptr<Player> next_;
+}; \ No newline at end of file