summaryrefslogtreecommitdiff
path: root/graphs/cpp/exception/game.cc
blob: 88d435ce1455efa77a46b1f43af0679cd6b21f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}