summaryrefslogtreecommitdiff
path: root/graphs/cpp/exception/game.cc
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/cpp/exception/game.cc')
-rw-r--r--graphs/cpp/exception/game.cc23
1 files changed, 23 insertions, 0 deletions
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 <iostream>
+
+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;
+}