Skip to main content
Commonmark migration
Source Link

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 write Outcome::user instead of user, until C++20's using 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 Choices 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)];
}

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 write Outcome::user instead of user, until C++20's using 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 Choices 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)];
}

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 write Outcome::user instead of user, until C++20's using 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 Choices 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)];
}
Source Link

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 write Outcome::user instead of user, until C++20's using 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 Choices 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)];
}