#include "hands.h" #include #include #include #include std::unique_ptr Hand::fromChar(const char letter) { switch (letter) { case 'R': case 'r': return std::make_unique(); case 'P': case 'p': return std::make_unique(); case 'S': case 's': return std::make_unique(); case 'L': case 'l': return std::make_unique(); case 'O': case 'o': return std::make_unique(); default: std::cout << "Invalid choice" << std::endl; std::exit(1); } } std::unique_ptr Hand::generateRandom(std::mt19937 &mt, std::uniform_int_distribution &intDist) { switch (intDist(mt)) { case 0: return std::make_unique(); case 1: return std::make_unique(); case 2: return std::make_unique(); case 3: return std::make_unique(); case 4: return std::make_unique(); default: std::cerr << "An invalid random number was generated, this should " "be impossible" << std::endl; std::exit(1); } } // List of function implementations for all of the different types of Hand std::string Rock::getHandName() const { return "rock"; } GameResult Rock::compareAgainst(const Hand &other) { const std::string otherName = other.getHandName(); if (otherName == "rock") { return GameResult::Tie; } if (otherName == "scissors" || otherName == "lizard") { return GameResult::Win; } else { return GameResult::Loss; } } std::string Paper::getHandName() const { return "paper"; } GameResult Paper::compareAgainst(const Hand &other) { const std::string otherName = other.getHandName(); if (otherName == "paper") { return GameResult::Tie; } if (otherName == "rock" || otherName == "spock") { return GameResult::Win; } else { return GameResult::Loss; } } std::string Scissors::getHandName() const { return "scissors"; } GameResult Scissors::compareAgainst(const Hand &other) { const std::string otherName = other.getHandName(); if (otherName == "scissors") { return GameResult::Tie; } if (otherName == "paper" || otherName == "lizard") { return GameResult::Win; } else { return GameResult::Loss; } } std::string Lizard::getHandName() const { return "lizard"; } GameResult Lizard::compareAgainst(const Hand &other) { const std::string otherName = other.getHandName(); if (otherName == "lizard") { return GameResult::Tie; } if (otherName == "spock" || otherName == "paper") { return GameResult::Win; } else { return GameResult::Loss; } } std::string Spock::getHandName() const { return "spock"; } GameResult Spock::compareAgainst(const Hand &other) { const std::string otherName = other.getHandName(); if (otherName == "spock") { return GameResult::Tie; } if (otherName == "scissors" || otherName == "rock") { return GameResult::Win; } else { return GameResult::Loss; } }