Moortiii

Rock vs Paper vs Scissors - Ver 3)

Sep 11th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 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. int playAgain();
  12. void playGame();
  13.  
  14. int playAgain() {
  15.     string playAgain;
  16.     cout << "Play again ? Type 'y' for yes and 'n' for no\n" << endl;
  17.     cin >> playAgain;
  18.     return playAgain == "y";
  19. }
  20.  
  21. void playGame() {
  22.     // Let the user choose either rock, paper or scissors
  23.     string userChoice;
  24.     cout << "\n\nPlease use only lower-case letters!" << endl;
  25.     cout << "\n\nDo you choose 'rock', 'paper' or 'scissors'?\n" << endl;
  26.     cin >> userChoice;
  27.     cout << "\nYou have chosen: " << userChoice << endl;
  28.  
  29.     // Generate a pseudo-random number between 1 and 3
  30.     int randomNumber;
  31.     srand(time(0));
  32.     randomNumber = rand() % 3 + 1;
  33.  
  34.     // 1 = Rock, 2 = Paper, 3 = Scissors, assign a choice to the computer.
  35.     string computerChoice;
  36.     if (randomNumber == 1) {
  37.         computerChoice = "rock";
  38.     }
  39.     else if (randomNumber == 2) {
  40.         computerChoice = "paper";
  41.     }
  42.     else {
  43.         computerChoice = "scissors";
  44.     }
  45.     cout << "\nThe computer chose: " << computerChoice << endl;
  46.  
  47.     // Decide who wins by comparing the userChoice and computerChoice
  48.     // Choose what happens if userChoice is rock
  49.     if (userChoice == "rock") {
  50.         if (computerChoice == "paper") {
  51.             cout << "\nPaper beats rock, you lose!\n\n";
  52.         }
  53.         else if (computerChoice == "scissors") {
  54.             cout << "\nRock beats scissors, you win!\n\n";
  55.         }
  56.         else {
  57.             cout << "\nIt's a tie!\n\n";
  58.         }
  59.     }
  60.  
  61.  
  62.     // Choose what happens if userChoice is paper
  63.     if (userChoice == "paper") {
  64.         if (computerChoice == "scissors") {
  65.             cout << "\nScissors beats paper, you lose!\n\n";
  66.         }
  67.         else if (computerChoice == "rock") {
  68.             cout << "\nPaper beats rock, you win!\n\n";
  69.         }
  70.         else {
  71.             cout << "\nIt's a tie!\n\n";
  72.         }
  73.     }
  74.  
  75.  
  76.     // Choose what happens if userChoice is scissors
  77.     if (userChoice == "scissors") {
  78.         if (computerChoice == "rock") {
  79.             cout << "\nRock beats scissors, you lose!\n\n";
  80.         }
  81.         else if (computerChoice == "paper") {
  82.             cout << "\nScissors beats paper, you win!\n\n";
  83.         }
  84.         else {
  85.             cout << "\nIt's a tie!\n\n";
  86.         }
  87.     }
  88. }
  89.  
  90. int main(int nNumberofArgs, char* pszArgs[]) {
  91.     do {
  92.         playGame();
  93.     } while (playAgain());
  94.     cout << "\nYou have chosen to exit the game.\n\n";
  95.     system("PAUSE");
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment