Metadata


Notes

Rock Paper Scissors Game in C++ - Detailed Notes

In this video, we are going to create a simple game of Rock, Paper, Scissors using C++. This game will allow a player to choose one of the three options (Rock, Paper, or Scissors), and the computer will randomly pick one as well. Then, the program will determine the winner based on the standard rules of the game:

  • Rock beats Scissors
  • Scissors beats Paper
  • Paper beats Rock

Function Declarations:

  1. char get_user_choice(): This function returns a character that represents the user’s choice (Rock, Paper, or Scissors).
  2. char get_computer_choice(): This function returns a character that represents the computer’s random choice (Rock, Paper, or Scissors).
  3. void show_choice(char choice): This function outputs the choice that was made, whether it’s Rock, Paper, or Scissors.
  4. void choose_winner(char player, char computer): This function compares the player’s choice with the computer’s choice and determines the winner.

Implementation Steps:

1. Setting Up the Main Function:

In the main() function, we declare two variables to store the choices of the player and the computer:

  • char player: The player’s choice.
  • char computer: The computer’s randomly selected choice.

We then call the get_user_choice() function to obtain the player’s input and assign it to the player variable. The computer’s choice is fetched by calling get_computer_choice().

Next, we display the player’s choice using the show_choice() function and pass in the player variable.

2. The get_user_choice() Function:

In this function, we handle the user input:

  • First, we display a simple game title: “Rock Paper Scissors Game” and list the valid options (R for Rock, P for Paper, and S for Scissors).
  • We use a do-while loop to repeatedly prompt the player until a valid input is entered. The loop ensures that the user can only pick one of the following valid choices: R, P, or S.
    • If the user enters any other character, the loop will keep asking them to choose from the available options.
  • Once a valid choice is made, we return the corresponding character (R, P, or S) to the main() function.

Example code for get_user_choice():

char get_user_choice() {
    char player;
    do {
        std::cout << "Rock Paper Scissors Game\\n";
        std::cout << "Choose one of the following:\\n";
        std::cout << "r for Rock, p for Paper, s for Scissors\\n";
        std::cin >> player;
    } while (player != 'r' && player != 'p' && player != 's');
 
    return player;
}
 

3. The show_choice() Function:

This function outputs the player’s or computer’s choice in a user-friendly manner. It takes one argument, choice, which is a character (‘r’, ‘p’, or ‘s’). Using a switch statement, we print the corresponding choice.

Example code for show_choice():

void show_choice(char choice) {
    switch(choice) {
        case 'r': std::cout << "Rock\\n"; break;
        case 'p': std::cout << "Paper\\n"; break;
        case 's': std::cout << "Scissors\\n"; break;
    }
}
 

4. The get_computer_choice() Function:

The computer’s choice is generated randomly. To accomplish this:

  • We use the rand() function from the C++ standard library, which generates a random number.
  • We call srand() with the current time (time(0)) to seed the random number generator so that it produces different sequences of random numbers each time the program runs.
  • The random number is then restricted to values between 1 and 3 using the modulus operator (% 3 + 1), which ensures that the result corresponds to the three valid options.
    • 1 corresponds to Rock (‘r’),
    • 2 corresponds to Paper (‘p’),
    • 3 corresponds to Scissors (‘s’).

Example code for get_computer_choice():

char get_computer_choice() {
    srand(time(0));  // Seed the random number generator
    int num = rand() % 3 + 1;  // Random number between 1 and 3
 
    switch(num) {
        case 1: return 'r';  // Rock
        case 2: return 'p';  // Paper
        case 3: return 's';  // Scissors
    }
}
 

5. The choose_winner() Function:

This function compares the player’s choice with the computer’s choice and determines the winner:

  • If statements are used to evaluate different scenarios.
  • Based on the choices:
    • If both the player and the computer choose the same option, it’s a tie.
    • If the player’s choice beats the computer’s choice, the player wins.
    • If the computer’s choice beats the player’s choice, the player loses.

Example code for choose_winner():

void choose_winner(char player, char computer) {
    if (player == computer) {
        std::cout << "It's a tie!\\n";
    }
    else if ((player == 'r' && computer == 's') ||
             (player == 'p' && computer == 'r') ||
             (player == 's' && computer == 'p')) {
        std::cout << "You win!\\n";
    }
    else {
        std::cout << "You lose!\\n";
    }
}
 

6. Putting It All Together:

In the main() function, after obtaining both the player’s and the computer’s choices, we:

  • Display both the player’s and the computer’s choices.
  • Call the choose_winner() function to determine and display the result.

Example of a full main() function:

int main() {
    char player, computer;
    player = get_user_choice();
    show_choice(player);
 
    computer = get_computer_choice();
    std::cout << "Computer's choice: ";
    show_choice(computer);
 
    choose_winner(player, computer);
 
    return 0;
}
 

Final Game Flow:

  1. The program greets the player and prompts for their choice (Rock, Paper, or Scissors).
  2. The computer randomly selects its choice.
  3. Both the player’s and the computer’s choices are displayed.
  4. The winner is determined based on the standard rules of Rock, Paper, Scissors and the result is displayed.

Test Run:

  • Example 1:
    • Player chooses Rock, Computer chooses Scissors → You win!
  • Example 2:
    • Player chooses Paper, Computer chooses Rock → You win!
  • Example 3:
    • Player chooses Scissors, Computer chooses Scissors → It’s a tie!
  • Example 4:
    • Player chooses Rock, Computer chooses Paper → You lose!

Conclusion:

This program demonstrates how to use functions, switch statements, random number generation, and conditional logic in C++ to build a simple but engaging game of Rock, Paper, Scissors.


References