74 lines
No EOL
2 KiB
C++
74 lines
No EOL
2 KiB
C++
#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 Rock Paper Scissors! Please enter "
|
|
"a hand to play (R/P/S): ";
|
|
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);
|
|
}
|
|
}
|
|
} |