Moortiii

Rock vs Paper vs Scissors

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