std::string rps(const std::string p1, const std::string p2) { enum shape { rock = 1 << 0, paper = 1 << 1, scissors = 1 << 2, lizard = 1 << 3, spock = 1 << 4, beaten_by_rock = shape::scissors | shape::lizard, beaten_by_paper = shape::spock | shape::rock, beaten_by_scissors = shape::paper | shape::lizard, beaten_by_lizard = shape::spock | shape::paper, beaten_by_spock = shape::scissors | shape::rock }; typedef std::pair<shape, shape> shape_info; static std::map<std::string, shape_info> shapes; if (shapes.empty()) { shapes.insert(std::make_pair("rock", std::make_pair(shape::rock, shape::beaten_by_rock))); shapes.insert(std::make_pair("paper", std::make_pair(shape::paper, shape::beaten_by_paper))); shapes.insert(std::make_pair("scissors", std::make_pair(shape::scissors, shape::beaten_by_scissors))); shapes.insert(std::make_pair("lizard", std::make_pair(shape::lizard, shape::beaten_by_lizard))); shapes.insert(std::make_pair("spock", std::make_pair(shape::spock, shape::beaten_by_spock))); } assert(shapes.find(p1) != shapes.end()); assert(shapes.find(p2) != shapes.end()); shape_info shape_info_p1 = shapes.find(p1)->second; shape_info shape_info_p2 = shapes.find(p2)->second; if (p1 == p2) { return "Draw!"; } else if (shape_info_p1.first & ~(shape_info_p2.second)) { return "Player 1 won!"; } else if (shape_info_p2.first & ~(shape_info_p1.second)) { return "Player 2 won!"; } else { assert(false); } }
string rps(const std::string p1, const std::string p2){std::map<std::string, int> dict;- std::string rps(const std::string p1, const std::string p2) {
- enum shape {
- rock = 1 << 0,
- paper = 1 << 1,
- scissors = 1 << 2,
- lizard = 1 << 3,
- spock = 1 << 4,
dict["rock"] = 0;dict["paper"] = 1;dict["scissors"] = 2;dict["lizard"] = 3;dict["spock"] = 4;- beaten_by_rock = shape::scissors | shape::lizard,
- beaten_by_paper = shape::spock | shape::rock,
- beaten_by_scissors = shape::paper | shape::lizard,
- beaten_by_lizard = shape::spock | shape::paper,
- beaten_by_spock = shape::scissors | shape::rock
- };
if(p1 == p2) return "Draw!";if(dict[p1] == 0){if(dict[p2] == 1) return "Player 2 won!";if(dict[p2] == 2) return "Player 1 won!";if(dict[p2] == 3) return "Player 1 won!";if(dict[p2] == 4) return "Player 2 won!";}if(dict[p1] == 1){if(dict[p2] == 0) return "Player 1 won!";if(dict[p2] == 2) return "Player 2 won!";if(dict[p2] == 3) return "Player 2 won!";if(dict[p2] == 4) return "Player 1 won!";}if(dict[p1] == 2){if(dict[p2] == 0) return "Player 2 won!";if(dict[p2] == 1) return "Player 1 won!";if(dict[p2] == 3) return "Player 1 won!";if(dict[p2] == 4) return "Player 2 won!";}if(dict[p1] == 3){if(dict[p2] == 0) return "Player 2 won!";if(dict[p2] == 1) return "Player 1 won!";if(dict[p2] == 2) return "Player 2 won!";if(dict[p2] == 4) return "Player 1 won!";}if(dict[p1] == 4){if(dict[p2] == 0) return "Player 1 won!";if(dict[p2] == 1) return "Player 2 won!";if(dict[p2] == 2) return "Player 1 won!";if(dict[p2] == 3) return "Player 2 won!";}- typedef std::pair<shape, shape> shape_info;
- static std::map<std::string, shape_info> shapes;
- if (shapes.empty()) {
- shapes.insert(std::make_pair("rock", std::make_pair(shape::rock, shape::beaten_by_rock)));
- shapes.insert(std::make_pair("paper", std::make_pair(shape::paper, shape::beaten_by_paper)));
- shapes.insert(std::make_pair("scissors", std::make_pair(shape::scissors, shape::beaten_by_scissors)));
- shapes.insert(std::make_pair("lizard", std::make_pair(shape::lizard, shape::beaten_by_lizard)));
- shapes.insert(std::make_pair("spock", std::make_pair(shape::spock, shape::beaten_by_spock)));
- }
- assert(shapes.find(p1) != shapes.end());
- assert(shapes.find(p2) != shapes.end());
- shape_info shape_info_p1 = shapes.find(p1)->second;
- shape_info shape_info_p2 = shapes.find(p2)->second;
- if (p1 == p2) {
- return "Draw!";
- } else if (shape_info_p1.first & ~(shape_info_p2.second)) {
- return "Player 1 won!";
- } else if (shape_info_p2.first & ~(shape_info_p1.second)) {
- return "Player 2 won!";
- } else {
- assert(false);
- }
- }
Describe(rock_paper_scissor_lizard_spock) { It(should_pass_some_example_tests) { Assert::That(rps("rock", "lizard"), Equals("Player 1 won!")); Assert::That(rps("paper", "rock"), Equals("Player 1 won!")); Assert::That(rps("rock", "paper"), Equals("Player 2 won!")); Assert::That(rps("lizard", "scissors"), Equals("Player 2 won!")); Assert::That(rps("spock", "spock"), Equals("Draw!")); } };
- Describe(rock_paper_scissor_lizard_spock)
- {
- It(should_pass_some_example_tests)
- {
- Assert::That(rps("rock", "lizard"), Equals("Player 1 won!"));
- Assert::That(rps("paper", "rock"), Equals("Player 1 won!"));
- Assert::That(rps("rock", "paper"), Equals("Player 2 won!"));
- Assert::That(rps("lizard", "scissors"), Equals("Player 2 won!"));
- Assert::That(rps("spock", "spock"), Equals("Draw!"));
- }
}- };