Using a C++ Switch Statement for Rock Paper Scissors - Comprehensive GuideStep

Rock, paper, scissors is a game that can be incredibly useful in various programming tasks. This guide will walk you through the basics of coming up with a game of rock, paper scissors using C++ and a switch statement. We’ll explore a few variations of the game and eventually arrive at the most versatile version.

Getting Started

First, we must cover the basics. After the game is set up, it’s easy to expand on the structure to increase the complexity of the game. A switch statement is an uncomplicated way to control the program flow of the game with C++. It is best used when exact conditions need to be met or when a large list of options is available.

Let’s start by assuming the user will have the same options every time—rock, paper, and scissors. These three options are typically denoted in C++ using three char variables: r, p, and s.

The Logic

To operate the game, a few logical elements are needed:

  • A way to translate a user’s input into a valid option
  • A way to give the user some sort of response
  • Command for the computer to randomly choose one of the valid options

A switch statement will act as the decision-making mechanism. It takes the user’s input, regardless if it’s r, s, or p, and processes it accordingly. This is where the majority of the program will be written.

To randomly select an option, a different mechanism that gets a random number will be necessary. The random number will then be processed and converted into the three valid inputs.

Writing The Code

The following code is a basic version of rock, paper, scissors. The below example uses an if statement for the random generator. However, it can be changed to a switch statement if desired.

int main() {
   char userInput, compInput;
     
   //Get user's input
   cout << "Rock, Paper, Scissors:  ";
   cin >> userInput;
     
   //Randomly generate an option for computer
   int randomNumber = rand() % 3;
     
   if (randomNumber == 0)
      compInput = 'r';
   else if (randomNumber == 1)
      compInput = 'p';
   else
      compInput = 's';
     
   //Compare user's and computer's inputs
   switch (userInput) {
     case 'r':
        if (compInput == 'r') 
           cout << "Draw!" << endl;
        else if (compInput == 'p')
           cout << "Computer wins!" << endl;
        else 
           cout << "You win!" << endl;
        break;
       
     case 'p':
        if (compInput == 'p')
           cout << "Draw!" << endl;
        else if (compInput == 's')
           cout << "Computer wins!" << endl;
        else 
           cout << "You win!" << endl;
        break;
       
     case 's':
        if (compInput == 's')
           cout << "Draw!" << endl;
        else if (compInput == 'r')
           cout << "Computer wins!" << endl;
        else 
           cout << "You win!" << endl;
        break;
       
     default:
        cout << "Illegal move!" << endl;
        break;
   }
   return 0;
}

Now that you have the basics of creating a rock, paper, scissors game using a switch statement, let’s look at some of its applications.

Advanced Switch Statements

Here are a few ideas on how to incorporate a switch statement into a version of rock, paper, scissors:

  • Adding an additional option (like “spock”) to the game
  • Adding two-player capability
  • Adding a user score
  • Outcomes based on difficulty levels

All of these use the same structure as the game’s original switch statement, but with a few additional layer of conditions.

FAQ

What is a switch statement?

A switch statement is a type of programming construct that allows for easy comparison of a variable to a set of predefined values. It allows for the program to execute differently depending on which of the values match.

How do I use a switch statement?

To use a switch statement, you must first create a variable to store the input. Then, you must set up the switch statement and create “case” values to compare against the input. Finally, you must create an action for each case to execute.

Can I add variations to the game?

Yes. You can use conditionals and other logic to vary outcomes within the game.

Are there any books I can read to better understand switch statements?

Yes. Here are some suggestions:

Are there any other alternatives to switch statements?

Yes. Other alternatives include if/else statements, function pointer tables, and lookup tables.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.