Advertisement
janac

Rock, Paper, Scissors

Dec 8th, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib> // rand, srand
  3. #include <string>
  4. using namespace std;
  5.  
  6. // This function returns 0 if tie, 1 if player wins,
  7. // or 2 if computer wins.
  8. int determine_winner(char player, char computer)
  9. {
  10.     if (computer == 'r')
  11.     {
  12.         if (player == 'r') return 0;
  13.         else if (player == 'p') return 1;
  14.         else return 2;
  15.     }
  16.     else if (computer == 'p')
  17.     {
  18.         if (player == 'r') return 2;
  19.         else if (player == 'p') return 0;
  20.         else return 1;
  21.     }
  22.     else if (computer == 's')
  23.     {
  24.         if (player == 'r') return 1;
  25.         else if (player == 'p') return 2;
  26.         else return 0;
  27.     }
  28. }
  29.  
  30. // This function returns the rule string, based on
  31. // what the two players' choices were.
  32. string get_rule(char player, char computer)
  33. {
  34.     string rule; // Announce the applicable rule.
  35.  
  36.     if (computer == 'r')
  37.     {
  38.         if (player == 'r') rule = "It's a tie.\n";
  39.         else if (player == 'p') rule = "Paper covers Rock.\n";
  40.         else rule = "Rock crushes Scissors.\n";
  41.     }
  42.     else if (computer == 'p')
  43.     {
  44.         if (player == 'r') rule = "Paper covers Rock.\n";
  45.         else if (player == 'p') rule = "It's a tie.\n";
  46.         else rule = "Scissors cut Paper.\n";
  47.     }
  48.     else if (computer == 's')
  49.     {
  50.         if (player == 'r') rule = "Rock crushes Scissors.\n";
  51.         else if (player == 'p') rule = "Scissors cut Paper.\n";
  52.         else rule = "It's a tie.\n"; // result_statement
  53.     }
  54.  
  55.     return rule;
  56. }
  57.  
  58. // This function returns the result statement, based on
  59. // who won. If a tie winner is 0, player 1, computer 2.
  60. string get_result_statement(int winner)
  61. {
  62.     if (winner == 0) return "No points awarded.\n";
  63.     else if (winner == 1) return "One point for you.\n";
  64.     else return "One point for the computer.\n";
  65. }
  66.  
  67. // This function displays the scores.
  68. void display_scores(int player, int computer)
  69. {
  70.     cout << "Score:\nYou: " << player << "\nThe Computer: "
  71.         << computer << '\n';
  72. }
  73.  
  74. int main()
  75. {
  76.     char player_choice = 'x';
  77.     char computer_choice = 'x';
  78.     // This is the possible choices,
  79.     // rock, paper or scissors.
  80.     char choices[3] = {'r', 'p', 's'};
  81.     int winner = 0;
  82.     int player_score = 0;
  83.     int computer_score = 0;
  84.     string rule; // The applicable rule for the round.
  85.     string result_statement; // Which player got points.
  86.     srand(time(NULL)); // Seed the random number generator.
  87.  
  88.  
  89.     cout << "Welcome to Rock, Paper Scissors!\n\n"
  90.         "Rules:\n\n"
  91.         "Rock crushes Scissors\n"
  92.         "Scissors cut Paper\n"
  93.         "Paper covers Rock\n\n"
  94.         "You are playing the computer.\n"
  95.         "You start first.\n\n"
  96.         "To choose Rock, type r\n"
  97.         "Paper, type p\n"
  98.         "Scissors, type s\n\n"
  99.         "Then the computer will make a\n"
  100.         "random choice.\n\n"
  101.         "Whoever wins the round(based on\n"
  102.         "the rules above) gets one point.\n\n"
  103.         "The first player to get\n"
  104.         "4 points wins.\n\n";
  105.         display_scores(player_score, computer_score);
  106.  
  107.     while (true)
  108.     {
  109.         cout << "\nWhat is your choice? ";
  110.         cin >> player_choice; // Get player choice.
  111.         // If the player does not choose either, r, p or s,
  112.         while ((player_choice != 'r') && (player_choice != 'p')
  113.             && (player_choice != 's'))
  114.         {
  115.             cin >> player_choice; // Get new input.
  116.         }
  117.         // Computer chooses a random number.
  118.         computer_choice = choices[rand() % 3];
  119.         cout << "The computer randomly chose: "
  120.             << computer_choice << '\n';;
  121.         winner = determine_winner(player_choice, computer_choice);
  122.         // Get the applicable rule for the round.
  123.         rule = get_rule(player_choice, computer_choice);
  124.         cout << rule; // Display the applicable rule.
  125.         // Get which player got the points.
  126.         result_statement = get_result_statement(winner);
  127.         cout << result_statement; // Display who got points.
  128.         cout << '\n'; // Go to the next time.
  129.         // If someone has won the game, leave the loop.
  130.         if (winner == 1) player_score += 1;
  131.         else if (winner == 2) computer_score += 1;
  132.         if ((player_score == 4) || (computer_score == 4))
  133.         {
  134.             break; // Leave the loop.
  135.         }
  136.         display_scores(player_score, computer_score);
  137.     }
  138.  
  139.     display_scores(player_score, computer_score);
  140.  
  141.     // Display winner message based on who won.
  142.     if (player_score == 4) cout << "\nYou win!\n";
  143.     else cout << "\nThe computer wins!\n";
  144.  
  145.     // End the program.
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement