Skip to content

Instantly share code, notes, and snippets.

@eisterman
Created September 6, 2017 08:03
Show Gist options
  • Save eisterman/7a36f3f5464926479766065ae962b69c to your computer and use it in GitHub Desktop.
Save eisterman/7a36f3f5464926479766065ae962b69c to your computer and use it in GitHub Desktop.
Sasso Carta Forbice
#include <string>
#include <vector>
#include <algorithm>
const std::vector<std::string> states = {"scissors","paper","rock"};
inline auto pick_up(const std::string& name)
{
return distance(states.begin(), find(states.begin(), states.end(), name));
}
std::string rps(const std::string& p1, const std::string& p2)
{
auto first = pick_up(p1);
auto second = pick_up(p2);
auto delta = second - first;
// Win the first
if (delta == 1 || delta == -2) {
return "Player 1 won!";
} else if (delta == 0) {
return "Draw!";
} else {
return "Player 2 won!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment