In addition to Edward's answer.
Use enum class
instead of enum
##
enum class Outcome { user, bot, draw };
enum class Choice { rock, paper, scissors };
This does two things:
- Inject the names into the
enum class
, rather than into the surrounding namespace. (You then have to writeOutcome::user
instead ofuser
, until C++20'susing enum
is available) - Remove any implicit conversion to/from the underlying type.
A better random generation alternative
You want equiprobable numbers between 0 and 2 inclusive, to be able to get one of Choice
s values. Improving on Edward's answer, this would be:
Choice getBotChoice() {
constexpr static std::array<Choice,3> choices{ rock, paper, scissors };
static auto rnd{std::mt19937{std::random_device{}()}};
std::uniform_int_distribution<int> distribution(0,choices.size() - 1);
return choices[distribution(rnd)];
}