Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib> // rand, srand
- #include <string>
- using namespace std;
- // This function returns 0 if tie, 1 if player wins,
- // or 2 if computer wins.
- int determine_winner(char player, char computer)
- {
- if (computer == 'r')
- {
- if (player == 'r') return 0;
- else if (player == 'p') return 1;
- else return 2;
- }
- else if (computer == 'p')
- {
- if (player == 'r') return 2;
- else if (player == 'p') return 0;
- else return 1;
- }
- else if (computer == 's')
- {
- if (player == 'r') return 1;
- else if (player == 'p') return 2;
- else return 0;
- }
- }
- // This function returns the rule string, based on
- // what the two players' choices were.
- string get_rule(char player, char computer)
- {
- string rule; // Announce the applicable rule.
- if (computer == 'r')
- {
- if (player == 'r') rule = "It's a tie.\n";
- else if (player == 'p') rule = "Paper covers Rock.\n";
- else rule = "Rock crushes Scissors.\n";
- }
- else if (computer == 'p')
- {
- if (player == 'r') rule = "Paper covers Rock.\n";
- else if (player == 'p') rule = "It's a tie.\n";
- else rule = "Scissors cut Paper.\n";
- }
- else if (computer == 's')
- {
- if (player == 'r') rule = "Rock crushes Scissors.\n";
- else if (player == 'p') rule = "Scissors cut Paper.\n";
- else rule = "It's a tie.\n"; // result_statement
- }
- return rule;
- }
- // This function returns the result statement, based on
- // who won. If a tie winner is 0, player 1, computer 2.
- string get_result_statement(int winner)
- {
- if (winner == 0) return "No points awarded.\n";
- else if (winner == 1) return "One point for you.\n";
- else return "One point for the computer.\n";
- }
- // This function displays the scores.
- void display_scores(int player, int computer)
- {
- cout << "Score:\nYou: " << player << "\nThe Computer: "
- << computer << '\n';
- }
- int main()
- {
- char player_choice = 'x';
- char computer_choice = 'x';
- // This is the possible choices,
- // rock, paper or scissors.
- char choices[3] = {'r', 'p', 's'};
- int winner = 0;
- int player_score = 0;
- int computer_score = 0;
- string rule; // The applicable rule for the round.
- string result_statement; // Which player got points.
- srand(time(NULL)); // Seed the random number generator.
- cout << "Welcome to Rock, Paper Scissors!\n\n"
- "Rules:\n\n"
- "Rock crushes Scissors\n"
- "Scissors cut Paper\n"
- "Paper covers Rock\n\n"
- "You are playing the computer.\n"
- "You start first.\n\n"
- "To choose Rock, type r\n"
- "Paper, type p\n"
- "Scissors, type s\n\n"
- "Then the computer will make a\n"
- "random choice.\n\n"
- "Whoever wins the round(based on\n"
- "the rules above) gets one point.\n\n"
- "The first player to get\n"
- "4 points wins.\n\n";
- display_scores(player_score, computer_score);
- while (true)
- {
- cout << "\nWhat is your choice? ";
- cin >> player_choice; // Get player choice.
- // If the player does not choose either, r, p or s,
- while ((player_choice != 'r') && (player_choice != 'p')
- && (player_choice != 's'))
- {
- cin >> player_choice; // Get new input.
- }
- // Computer chooses a random number.
- computer_choice = choices[rand() % 3];
- cout << "The computer randomly chose: "
- << computer_choice << '\n';;
- winner = determine_winner(player_choice, computer_choice);
- // Get the applicable rule for the round.
- rule = get_rule(player_choice, computer_choice);
- cout << rule; // Display the applicable rule.
- // Get which player got the points.
- result_statement = get_result_statement(winner);
- cout << result_statement; // Display who got points.
- cout << '\n'; // Go to the next time.
- // If someone has won the game, leave the loop.
- if (winner == 1) player_score += 1;
- else if (winner == 2) computer_score += 1;
- if ((player_score == 4) || (computer_score == 4))
- {
- break; // Leave the loop.
- }
- display_scores(player_score, computer_score);
- }
- display_scores(player_score, computer_score);
- // Display winner message based on who won.
- if (player_score == 4) cout << "\nYou win!\n";
- else cout << "\nThe computer wins!\n";
- // End the program.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement