Advertisement
Sinux1

T6E7.cpp (rock paper scissors)

Mar 29th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib> //srand, rand
  3. #include <ctime>
  4.  
  5. using namespace std;
  6. void show_winner(string p1, string p2)
  7. {
  8.     if (p1 == "Paper" && p2 == "Rock"
  9.         || p1 == "Rock" && p2 == "Scissors"
  10.         || p1 == "Scissors" && p2 == "Paper")
  11.     {
  12.         cout << " :-) Human wins!" << endl;
  13.     }
  14.     else if (p1 == p2)
  15.     {
  16.         cout << " :-| This is a tie" << endl;
  17.     }
  18.     else
  19.     {
  20.         cout << " :-( Computer Wins!" << endl;
  21.     }
  22. }
  23.  
  24. string get_computer_choice()
  25. {
  26.     int selection = rand() % 3;
  27.  
  28.     if (selection == 0)
  29.     {
  30.         return "paper";
  31.     }
  32.     else if (selection == 1)
  33.     {
  34.         return "rock";
  35.     }
  36.     else
  37.     {
  38.         return "scissors";
  39.     }
  40. }
  41.  
  42. string get_user_choice()
  43. {
  44.     string input;
  45.     cout << "Enter paper, rock, or scissors" << endl;
  46.     cin >> input;
  47.     return input;
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53.  
  54.     srand(time(NULL));
  55.  
  56.     cout << "---Paper --- Rock --- Scissors---" << endl;
  57.  
  58.     string p1 = get_user_choice();
  59.     string p2 = get_computer_choice();
  60.     show_winner(p1,p2);
  61.  
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement