Moortiii

Make it functional!

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