Moortiii

Rock, paper, scissors functional

Sep 11th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. // Add necessary includes
  2.  
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <string>
  7. using namespace std;
  8.  
  9. // This is a simple game of "Rock, Paper, Scissors"
  10. // It is also my first project ever in C++.
  11.  
  12. // Declare functions
  13. void playGame();
  14. bool playAgain();
  15. string letUserChoose(string userChoice);
  16. int genRandNum(int randomNumber);
  17. string letComputerChoose(int randomNumber, string computerChoice);
  18. void compareChoices(string userChoice, string computerChoice);
  19.  
  20. bool playAgain() {
  21.     string stayOrGo;
  22.     cout << "Type 'y' to play again, 'n' to quit.\n" << endl;
  23.     cin >> stayOrGo;
  24.     return stayOrGo == "y";
  25. }
  26.  
  27. int genRandNum(int randomNumber) {
  28.     // Generate a pseudo-random number between 1 and 3
  29.     srand(time(0));
  30.     randomNumber = rand() % 3 + 1;
  31.     return randomNumber;
  32. }
  33.  
  34. string letUserChoose(string userChoice) {
  35.     // Let the user choose either rock, paper or scissors
  36.     cout << "\n\nPlease use only lower-case letters!" << endl;
  37.     cout << "\n\nDo you choose 'rock', 'paper' or 'scissors'?\n" << endl;
  38.     cin >> userChoice;
  39.     cout << "\nYou have chosen: " << userChoice << endl;
  40.     return userChoice;
  41. }
  42.  
  43. string letComputerChoose(int randomNumber, string computerChoice) {
  44.     // 1 = Rock, 2 = Paper, 3 = Scissors, assign a choice to the computer.
  45.     if (randomNumber == 1) {
  46.         computerChoice = "rock";
  47.     }
  48.     else if (randomNumber == 2) {
  49.         computerChoice = "paper";
  50.     }
  51.     else {
  52.         computerChoice = "scissors";
  53.     }
  54.     cout << "\nThe computer chose: " << computerChoice << endl;
  55.     return computerChoice;
  56. }
  57.  
  58. void compareChoices(string userChoice, string computerChoice) {
  59.     // Decide who wins by comparing the userChoice and computerChoice
  60.     // Choose what happens if userChoice is rock
  61.     if (userChoice == "rock") {
  62.         if (computerChoice == "paper") {
  63.             cout << "\nPaper beats rock, you lose!\n\n";
  64.         }
  65.         else if (computerChoice == "scissors") {
  66.             cout << "\nRock beats scissors, you win!\n\n";
  67.         }
  68.         else {
  69.             cout << "\nIt's a tie!\n\n";
  70.         }
  71.     }
  72.  
  73.     // Choose what happens if userChoice is paper
  74.     if (userChoice == "paper") {
  75.         if (computerChoice == "scissors") {
  76.             cout << "\nScissors beats paper, you lose!\n\n";
  77.         }
  78.         else if (computerChoice == "rock") {
  79.             cout << "\nPaper beats rock, you win!\n\n";
  80.         }
  81.         else {
  82.             cout << "\nIt's a tie!\n\n";
  83.         }
  84.     }
  85.  
  86.     // Choose what happens if userChoice is scissors
  87.     if (userChoice == "scissors") {
  88.         if (computerChoice == "rock") {
  89.             cout << "\nRock beats scissors, you lose!\n\n";
  90.         }
  91.         else if (computerChoice == "paper") {
  92.             cout << "\nScissors beats paper, you win!\n\n";
  93.         }
  94.         else {
  95.             cout << "\nIt's a tie!\n\n";
  96.         }
  97.     }
  98. }
  99.  
  100. void playGame(string userChoice, int randomNumber, string computerChoice) {
  101.     letUserChoose(userChoice);
  102.     genRandNum(randomNumber);
  103.     letComputerChoose(randomNumber, computerChoice);
  104.     compareChoices(userChoice, computerChoice);
  105. }
  106.  
  107. int main(int nNumberofArgs, char* pszArgs[]) {
  108.     // Initialize variables
  109.     string userChoice = letUserChoose(userChoice);
  110.     int randomNumber = genRandNum(randomNumber);
  111.     string computerChoice = letComputerChoose(randomNumber, computerChoice);
  112.  
  113.     // Main loop of the game
  114.     do {
  115.         playGame();
  116.     } while (playAgain());
  117.     // Let the player know that he or she has chosen to exit the game
  118.     cout << "\nYou have chosen to exit the game.\n\n";
  119.     system("PAUSE");
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment