Moortiii

R/P/S Functional - Reddit

Nov 5th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5. using namespace std;
  6.  
  7. void playGame();
  8. int genRandNum();
  9. string userChooses();
  10. bool playAgain();
  11.  
  12. bool playAgain() {
  13.     string stayOrGo;
  14.     do {
  15.         cout << "\nType 'y' to play again, 'n' to quit.\n" << endl;
  16.         cin >> stayOrGo;
  17.         if (stayOrGo != "y" && stayOrGo != "n") {
  18.             cout << "\nThat is not correct input." << endl;
  19.         }
  20.     } while (stayOrGo != "y" && stayOrGo != "n");
  21.    
  22.     return stayOrGo == "y";
  23. }
  24.  
  25. int genRandNum() {
  26.     int randomNumber;
  27.     srand(time(0));
  28.     randomNumber = rand() % 3 + 1;
  29.     return randomNumber;
  30. }
  31.  
  32. string userChooses() {
  33.     string userChoice;
  34.     do {
  35.         cout << "\nDo you choose 'rock', 'paper' or 'scissors'?\n\n";
  36.         cin >> userChoice;
  37.         if (userChoice != "rock"
  38.             && userChoice != "paper"
  39.             && userChoice != "scissors") {
  40.             cout << "\nThat is not correct input." << endl;
  41.         }
  42.     } while (userChoice != "rock"
  43.         && userChoice != "paper"
  44.         && userChoice != "scissors");
  45.     cout << "\nYou have chosen: " << userChoice << endl;
  46.     return userChoice;
  47. }
  48.  
  49. string computerChooses(int randomNumber) {
  50.     string computerChoice;
  51.     if (randomNumber == 1) {
  52.         computerChoice = "rock";
  53.         cout << "\nThe computer has chosen: " << computerChoice << endl;
  54.     }
  55.     else if (randomNumber == 2) {
  56.         computerChoice = "paper";
  57.         cout << "\nThe computer has chosen: " << computerChoice << endl;
  58.     }
  59.     else {
  60.         computerChoice = "scissors";
  61.         cout << "\nThe computer has chosen: " << computerChoice << endl;
  62.     }
  63.     return computerChoice;
  64. }
  65.  
  66. void compareChoices(string userChoice, string computerChoice) {
  67.     // If userChoice is rock
  68.     if (userChoice == "rock") {
  69.         if (computerChoice == "paper") {
  70.             cout << "\nPaper beats rock, you lose!" << endl;
  71.         } else if(computerChoice == "scissors") {
  72.             cout << "\nRock beats scissors, you win!" << endl;
  73.         }
  74.         else {
  75.             cout << "\nIt's a tie!" << endl;
  76.         }
  77.     }
  78.     // If userChoice is paper
  79.     if (userChoice == "paper") {
  80.         if (computerChoice == "scissors") {
  81.             cout << "\nScissors beats paper, you lose!" << endl;
  82.         }
  83.         else if (computerChoice == "rock") {
  84.             cout << "\nPaper beats rock, you win!" << endl;
  85.         }
  86.         else {
  87.             cout << "\nIt's a tie!" << endl;
  88.         }
  89.     }
  90.     // If userChoice is scissors
  91.     if (userChoice == "scissors") {
  92.         if (computerChoice == "rock") {
  93.             cout << "\nRock beats scissors, you lose!" << endl;
  94.         }
  95.         else if (computerChoice == "paper") {
  96.             cout << "\nScissors beats paper, you win!" << endl;
  97.         }
  98.         else {
  99.             cout << "\nIt's a tie!" << endl;
  100.         }
  101.     }
  102. }
  103.  
  104. void playGame() {
  105.     string userChoice = userChooses();
  106.     string computerChoice = computerChooses(genRandNum());
  107.     compareChoices(userChoice, computerChoice);
  108. }
  109.  
  110. int main() {
  111.     do {
  112.         playGame();
  113.     } while (playAgain());
  114.     cout << "\nYou have chosen to exit the game.\n" << endl;
  115.     system("PAUSE");
  116.     return 0;
  117. }
Add Comment
Please, Sign In to add comment