This commit is contained in:
Tyler Beckman 2024-09-01 23:31:56 -06:00
parent e07bcb6dcf
commit 430a8225c5
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 23 additions and 12 deletions

View file

@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <random> #include <random>
#include <cstdlib> #include <cstdlib>
std::unique_ptr<Hand> Hand::fromChar(const char letter) { std::unique_ptr<Hand> Hand::fromChar(const char letter) {
@ -36,7 +37,9 @@ std::unique_ptr<Hand> Hand::generateRandom() {
case 2: case 2:
return std::make_unique<Scissors>(); return std::make_unique<Scissors>();
default: default:
std::cerr << "An invalid random number was generated, this should be impossible" << std::endl; std::cerr << "An invalid random number was generated, this should "
"be impossible"
<< std::endl;
std::exit(1); std::exit(1);
} }
} }
@ -47,25 +50,34 @@ std::string Rock::getHandName() const { return "rock"; }
GameResult Rock::compareAgainst(const Hand &other) { GameResult Rock::compareAgainst(const Hand &other) {
const std::string otherName = other.getHandName(); const std::string otherName = other.getHandName();
if (otherName == "rock") return GameResult::Tie; if (otherName == "rock")
if (otherName == "scissors") return GameResult::Win; return GameResult::Tie;
else return GameResult::Loss; if (otherName == "scissors")
return GameResult::Win;
else
return GameResult::Loss;
} }
std::string Paper::getHandName() const { return "paper"; } std::string Paper::getHandName() const { return "paper"; }
GameResult Paper::compareAgainst(const Hand &other) { GameResult Paper::compareAgainst(const Hand &other) {
const std::string otherName = other.getHandName(); const std::string otherName = other.getHandName();
if (otherName == "paper") return GameResult::Tie; if (otherName == "paper")
if (otherName == "rock") return GameResult::Win; return GameResult::Tie;
else return GameResult::Loss; if (otherName == "rock")
return GameResult::Win;
else
return GameResult::Loss;
} }
std::string Scissors::getHandName() const { return "scissors"; } std::string Scissors::getHandName() const { return "scissors"; }
GameResult Scissors::compareAgainst(const Hand &other) { GameResult Scissors::compareAgainst(const Hand &other) {
const std::string otherName = other.getHandName(); const std::string otherName = other.getHandName();
if (otherName == "scissors") return GameResult::Tie; if (otherName == "scissors")
if (otherName == "paper") return GameResult::Win; return GameResult::Tie;
else return GameResult::Loss; if (otherName == "paper")
return GameResult::Win;
else
return GameResult::Loss;
} }

View file

@ -11,8 +11,7 @@ int main(void) {
while (true) { while (true) {
// Take a letter input and convert it to the correct class type for // Take a letter input and convert it to the correct class type for
// later // later use
// use
char handLetter; char handLetter;
std::cout << "Welcome to a round of Rock Paper Scissors! Please enter " std::cout << "Welcome to a round of Rock Paper Scissors! Please enter "
"a hand to play (R/P/S): "; "a hand to play (R/P/S): ";