This is a simple C++ program to demonstrate rock, paper, scissors game. In this game, players have to choose one of the three options to beat the opponent or the computer itself.

The following C++ code prints a simple menu showing three options to the user and then user can enter one of the options.

C++ code to print  rock, paper, scissors game menu

Below is the game menu which shows list of options to choose.

rock, paper, scissors game menu options

The C++ code generates a random choice by creating a character to indicate that option. Options are: r is for ‘rock’, p is for ‘paper’, and s is for ‘scissors’.

Once we have both the choices i.e. user’s choice and computer’s choice then our program compares both the choices to see who the winner is.

C++ code choosing the winner.

The C++ function, that compares both the choices, works based on the following criteria.

  1. Rock beats scissors as it smashes Scissors.
  2. Scissors beats paper as it cuts Papers
  3. Paper beats rocks as it wraps the Rock.
  4. Show a tie if both choices match.

Rock, Paper, Scissors Game C++ Source Code

Here is the full C++ code for this game. Feel free to copy this code into your course work/assignments and modify as necessary. In this code, computer’s choice is selected based on random number generator. We also use modulus operator and time seed to generate a different random numbers every time.

#include <iostream>

// Constant variables
const char ROCK = 'r';
const char PAPER = 'p';
const char SCISSORS = 's';

using namespace std;

char getComputerOption() {
    srand(time(0));
    // Random number
    int num = rand() % 3 + 1;

    if(num==1) return 'r';
    if(num==2) return 'p';
    if(num==3) return 's';
}

char getUserOption() {
    char c;
    cout << "Rock, Paper and Scissors Game!" << endl;
    cout << "Choose one of the following options"  << endl;
    cout << "-----------------------------------"  << endl;
    cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl;
    cin >> c;
    
    while (c!='r' && c!='p' && c!='s' )
    {
        cout << "Please enter one of the following options only. " << endl;
        cout << "(r) for rock " << endl << "(p) for paper" << endl << "(s) for scissors " << endl;
        cin >> c;
    }

    return c;
}

void showSelectedOption(char option) {
    if (option == 'r') cout << "Rock" << endl;
    if (option == 'p') cout << "Paper" << endl;
    if (option == 's') cout << "Scissors" << endl;
}

void chooseWinner(char uChoice, char cChoice) {
    if (uChoice == ROCK && cChoice == PAPER) {
        cout << "Computer Wins! Paper wraps Rock."<< endl;
    }
    else if (uChoice == PAPER && cChoice == SCISSORS) {
        cout << "Computer Wins! Scissors cut Paper."<< endl;
        
    }
    else if (uChoice == SCISSORS && cChoice == ROCK) {
        cout << "Computer Wins! Rock smashes Scissors."<< endl;
        
    }
    else if (uChoice == ROCK && cChoice == SCISSORS) {
        cout << "You Win! Paper wraps Rock."<< endl;
        
    }
    else if (uChoice == PAPER && cChoice == ROCK) {
        cout << "You Win! Paper wraps Rock."<< endl;
        
    }
    else if (uChoice == SCISSORS && cChoice == PAPER) {
        cout << "You Win! Scissors cut Paper."<< endl;
    }
    else{
        cout << "Tie. Play again win the Game." << endl;
    }
}

int main() {
    //User's choice
    char uChoice; 
    //Compter's choice
    char cChoice;
    
    uChoice = getUserOption();
    cout << "Your choice is: "<< endl;
    showSelectedOption(uChoice);
    
    cout << "Computer's choice is: "<< endl;
    cChoice = getComputerOption();
    showSelectedOption(cChoice);
    
    chooseWinner(uChoice, cChoice);

    return 0;
}