A1/main.cpp

113 lines
No EOL
4.3 KiB
C++

/* 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>
#include <memory>
#include <ostream>
int main(void) {
int wins = 0;
int losses = 0;
int ties = 0;
while (true) {
// Take a letter input and convert it to the correct class type for
// later use
char handLetter;
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);
// Generate a random "computer" hand and display the two choices
const std::unique_ptr<Hand> computerHand = Hand::generateRandom();
std::cout << std::endl
<< "Player chose " << userHand->getHandName() << std::endl
<< "Computer chose " << computerHand->getHandName()
<< std::endl
<< std::endl;
// Compare the two hands, add to statistics, and give the correct output
// and explanation
switch (userHand->compareAgainst(*computerHand)) {
case Win:
std::cout << "The player wins, as " << userHand->getHandName()
<< " beats " << computerHand->getHandName() << "!"
<< std::endl;
wins++;
break;
case Tie:
std::cout << "No one wins, as " << userHand->getHandName()
<< " is the same as " << computerHand->getHandName()
<< "." << std::endl;
ties++;
break;
case Loss:
std::cout << "The computer wins, as "
<< computerHand->getHandName() << " beats "
<< userHand->getHandName() << "." << std::endl;
losses++;
break;
}
// Ask if the user would like to play again or exit
char playAgain;
std::cout << "Do you want to play again (Y/N)? ";
std::cin >> playAgain;
switch (playAgain) {
case 'Y':
case 'y':
std::cout << std::endl;
continue;
case 'N':
case 'n':
default:
std::cout << std::endl
<< "Thanks for playing!" << std::endl
<< "You won " << wins << " game(s), lost " << losses
<< " game(s), and tied " << ties << " time(s)."
<< std::endl;
std::exit(0);
}
}
}