2024-09-01 23:26:25 -06:00
|
|
|
#include "hands.h"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <random>
|
2024-09-01 23:31:56 -06:00
|
|
|
|
2024-09-01 23:26:25 -06:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
std::unique_ptr<Hand> Hand::fromChar(const char letter) {
|
|
|
|
switch (letter) {
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
|
|
|
return std::make_unique<Rock>();
|
|
|
|
case 'P':
|
|
|
|
case 'p':
|
|
|
|
return std::make_unique<Paper>();
|
|
|
|
case 'S':
|
|
|
|
case 's':
|
|
|
|
return std::make_unique<Scissors>();
|
|
|
|
default:
|
|
|
|
std::cout << "Invalid choice" << std::endl;
|
|
|
|
std::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Hand> Hand::generateRandom() {
|
|
|
|
std::mt19937 mt(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch().count());
|
|
|
|
std::uniform_int_distribution<int> intDist(0, 2);
|
|
|
|
|
|
|
|
switch (intDist(mt)) {
|
|
|
|
case 0:
|
|
|
|
return std::make_unique<Rock>();
|
|
|
|
case 1:
|
|
|
|
return std::make_unique<Paper>();
|
|
|
|
case 2:
|
|
|
|
return std::make_unique<Scissors>();
|
|
|
|
default:
|
2024-09-01 23:31:56 -06:00
|
|
|
std::cerr << "An invalid random number was generated, this should "
|
|
|
|
"be impossible"
|
|
|
|
<< std::endl;
|
2024-09-01 23:26:25 -06:00
|
|
|
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();
|
|
|
|
|
2024-09-01 23:31:56 -06:00
|
|
|
if (otherName == "rock")
|
|
|
|
return GameResult::Tie;
|
|
|
|
if (otherName == "scissors")
|
|
|
|
return GameResult::Win;
|
|
|
|
else
|
|
|
|
return GameResult::Loss;
|
2024-09-01 23:26:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Paper::getHandName() const { return "paper"; }
|
|
|
|
GameResult Paper::compareAgainst(const Hand &other) {
|
|
|
|
const std::string otherName = other.getHandName();
|
|
|
|
|
2024-09-01 23:31:56 -06:00
|
|
|
if (otherName == "paper")
|
|
|
|
return GameResult::Tie;
|
|
|
|
if (otherName == "rock")
|
|
|
|
return GameResult::Win;
|
|
|
|
else
|
|
|
|
return GameResult::Loss;
|
2024-09-01 23:26:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Scissors::getHandName() const { return "scissors"; }
|
|
|
|
GameResult Scissors::compareAgainst(const Hand &other) {
|
|
|
|
const std::string otherName = other.getHandName();
|
|
|
|
|
2024-09-01 23:31:56 -06:00
|
|
|
if (otherName == "scissors")
|
|
|
|
return GameResult::Tie;
|
|
|
|
if (otherName == "paper")
|
|
|
|
return GameResult::Win;
|
|
|
|
else
|
|
|
|
return GameResult::Loss;
|
2024-09-01 23:26:25 -06:00
|
|
|
}
|