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/exception/game.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 graphs/cpp/exception/game.cc (limited to 'graphs/cpp/exception/game.cc') diff --git a/graphs/cpp/exception/game.cc b/graphs/cpp/exception/game.cc new file mode 100644 index 0000000..88d435c --- /dev/null +++ b/graphs/cpp/exception/game.cc @@ -0,0 +1,23 @@ +#include "game.hh" + +#include + +Game::Game(int secret) + : secret_(secret) +{} + +void Game::play(const Player& p1, const Player& p2) const +{ + if (p1 == p2) + throw InvalidArgumentException{ "Stop playing by yourself!" }; + + auto d1 = std::abs(p1.get_guess() - secret_); + auto d2 = std::abs(p2.get_guess() - secret_); + + if (d1 > d2) + std::cout << p1 << " wins!" << std::endl; + else if (d1 < d2) + std::cout << p2 << " wins!" << std::endl; + else + std::cout << "Draw!" << std::endl; +} -- cgit v1.2.3