Moortiii

Rock vs Paper vs Scissors - WORKING

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