1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
enum Choice {PAPER, SCISSOR, ROCK, LIZARD, SPOCK, QUIT};
enum Result {PLAYER_WON, COMPUTER_WON, DRAW};
struct Rule
{
Choice player;
Choice comp;
Result result;
};
const std::vector<Rule> Rules =
{
{ PAPER, SCISSOR, COMPUTER_WON},
{ PAPER, ROCK, PLAYER_WON},
{ PAPER, PAPER, DRAW},
{ PAPER, SPOCK, PLAYER_WON},
{ PAPER, LIZARD, COMPUTER_WON},
{ ROCK, PAPER, COMPUTER_WON},
{ ROCK, SCISSOR, PLAYER_WON},
{ ROCK, ROCK, DRAW},
{ ROCK, SPOCK, COMPUTER_WON},
{ ROCK, LIZARD, PLAYER_WON},
{ SCISSOR, ROCK, COMPUTER_WON},
{ SCISSOR, PAPER, PLAYER_WON},
{ SCISSOR, SCISSOR, DRAW},
{ SCISSOR, LIZARD, PLAYER_WON},
{ SCISSOR, SPOCK, PLAYER_WON},
{ LIZARD, ROCK, COMPUTER_WON},
{ LIZARD, PAPER, PLAYER_WON},
{ LIZARD, SCISSOR, PLAYER_WON},
{ LIZARD, SPOCK, PLAYER_WON},
{ LIZARD, SPOCK, DRAW}, { LIZARD, LIZARD, DRAW},
{ SPOCK, LIZARD, COMPUTER_WON},
{ SPOCK, ROCK, PLAYER_WON},
{ SPOCK, PAPER, COMPUTER_WON},
{ SPOCK, SPOCK, DRAW},
{ SPOCK, SCISSOR, PLAYER_WON},
};
// Finding the winner:
Result getResult(Choice player, Choice comp)
{
auto result = std::find_if(Rules.begin(), Rules.end(), [player, comp](const Rule& rule)
{
return rule.comp == comp && rule.player == player;
});
if (result == Rules.end())
throw std::runtime_error("No result found");
return (*result).result;
}
|