Add spock and lizard, add header comment
This commit is contained in:
parent
430a8225c5
commit
010a9188a6
3 changed files with 92 additions and 6 deletions
42
hands.cpp
42
hands.cpp
|
@ -18,6 +18,12 @@ std::unique_ptr<Hand> Hand::fromChar(const char letter) {
|
|||
case 'S':
|
||||
case 's':
|
||||
return std::make_unique<Scissors>();
|
||||
case 'L':
|
||||
case 'l':
|
||||
return std::make_unique<Lizard>();
|
||||
case 'O':
|
||||
case 'o':
|
||||
return std::make_unique<Spock>();
|
||||
default:
|
||||
std::cout << "Invalid choice" << std::endl;
|
||||
std::exit(1);
|
||||
|
@ -27,7 +33,7 @@ std::unique_ptr<Hand> Hand::fromChar(const char letter) {
|
|||
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);
|
||||
std::uniform_int_distribution<int> intDist(0, 4);
|
||||
|
||||
switch (intDist(mt)) {
|
||||
case 0:
|
||||
|
@ -36,6 +42,10 @@ std::unique_ptr<Hand> Hand::generateRandom() {
|
|||
return std::make_unique<Paper>();
|
||||
case 2:
|
||||
return std::make_unique<Scissors>();
|
||||
case 3:
|
||||
return std::make_unique<Lizard>();
|
||||
case 4:
|
||||
return std::make_unique<Spock>();
|
||||
default:
|
||||
std::cerr << "An invalid random number was generated, this should "
|
||||
"be impossible"
|
||||
|
@ -52,7 +62,7 @@ GameResult Rock::compareAgainst(const Hand &other) {
|
|||
|
||||
if (otherName == "rock")
|
||||
return GameResult::Tie;
|
||||
if (otherName == "scissors")
|
||||
if (otherName == "scissors" || otherName == "lizard")
|
||||
return GameResult::Win;
|
||||
else
|
||||
return GameResult::Loss;
|
||||
|
@ -64,7 +74,7 @@ GameResult Paper::compareAgainst(const Hand &other) {
|
|||
|
||||
if (otherName == "paper")
|
||||
return GameResult::Tie;
|
||||
if (otherName == "rock")
|
||||
if (otherName == "rock" || otherName == "spock")
|
||||
return GameResult::Win;
|
||||
else
|
||||
return GameResult::Loss;
|
||||
|
@ -76,7 +86,31 @@ GameResult Scissors::compareAgainst(const Hand &other) {
|
|||
|
||||
if (otherName == "scissors")
|
||||
return GameResult::Tie;
|
||||
if (otherName == "paper")
|
||||
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;
|
||||
|
|
12
hands.h
12
hands.h
|
@ -36,4 +36,16 @@ class Scissors : public Hand {
|
|||
public:
|
||||
std::string getHandName() const override;
|
||||
GameResult compareAgainst(const Hand&) override;
|
||||
};
|
||||
|
||||
class Lizard : public Hand {
|
||||
public:
|
||||
std::string getHandName() const override;
|
||||
GameResult compareAgainst(const Hand&) override;
|
||||
};
|
||||
|
||||
class Spock : public Hand {
|
||||
public:
|
||||
std::string getHandName() const override;
|
||||
GameResult compareAgainst(const Hand&) override;
|
||||
};
|
44
main.cpp
44
main.cpp
|
@ -1,3 +1,42 @@
|
|||
/* CSCI 200: Assignment 1 (Rock Paper Scissors): Tyler Beckman
|
||||
*
|
||||
* Author: Tyler Beckman
|
||||
*
|
||||
* Description:
|
||||
* A C++ program to play rock paper scissors (lizard spock) against
|
||||
* randomly-generated computer hands. A hand can be entered to start, and is
|
||||
* then compared against a random one. The game can then be played again until
|
||||
* the user chooses to stop. After each round, statistics are gathered and
|
||||
* displayed after the user chooses to exit the game.
|
||||
*
|
||||
* Note on project structure:
|
||||
* Just for fun and to get ahead on learning cpp, I did create my RPS using
|
||||
* classes, inheritance, and polymorphism. I have experience with this in
|
||||
* multiple other languages, so I felt it would be fun to use this as an excuse
|
||||
* to learn how the concepts work in cpp. I apologize if this is more
|
||||
* complicated than we were supposed to make it, but it was fun and it should
|
||||
* still be a perfectly functional and point-scoring program :)
|
||||
*
|
||||
* Academic integrity:
|
||||
* 1. Because I did go overboard on making an object-oriented program when we
|
||||
* haven't even learned how classes yet, I did use a lot of online cpp syntax
|
||||
* reference and explainers to learn how classes and inheritance work in cpp.
|
||||
* This includes mainly cppreference.com, but also geeksforgeeks.org and
|
||||
* tutorialspoint to figure out the basics. Nowhere did I actually copy the
|
||||
* exact structures, I simply used it to figure out how to do what I wanted and
|
||||
* what I knew how to do in other languages such as Java and JavaScript (have a
|
||||
* "which one wins" function on each sibling class of "Hand"). I am not entirely
|
||||
* sure if this needs cited, but given we have learned none of this yet, I just
|
||||
* wanted to clarify that this is all my code - not copied from the internet,
|
||||
* ChatGPT, Copilot, or anything else other than my code.
|
||||
* 2. Also since I was making a program with structures that I hadn't quite
|
||||
* learned in this language yet, I did ask Max[TODO FILL IN NAME] (someone with
|
||||
* experience in cpp) for debugging assistance when concepts I was used to in
|
||||
* other languages didn't work well in cpp. For example, I wasn't used to class
|
||||
* definitions and function bodies being separate, so he helped me figure out
|
||||
* the correct structure there so I could properly reference sibling classes in
|
||||
* methods on the parent class.
|
||||
*/
|
||||
#include "hands.h"
|
||||
|
||||
#include <iostream>
|
||||
|
@ -13,8 +52,9 @@ int main(void) {
|
|||
// Take a letter input and convert it to the correct class type for
|
||||
// later use
|
||||
char handLetter;
|
||||
std::cout << "Welcome to a round of Rock Paper Scissors! Please enter "
|
||||
"a hand to play (R/P/S): ";
|
||||
std::cout << "Welcome to a round of [R]ock [P]aper [S]cissors [L]izard "
|
||||
"Sp[O]ck! Please enter "
|
||||
"a hand to play (R/P/S/L/O): ";
|
||||
std::cin >> handLetter;
|
||||
const std::unique_ptr<Hand> userHand = Hand::fromChar(handLetter);
|
||||
|
||||
|
|
Loading…
Reference in a new issue