Created
September 6, 2017 08:03
-
-
Save eisterman/7a36f3f5464926479766065ae962b69c to your computer and use it in GitHub Desktop.
Sasso Carta Forbice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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