Rock Paper and Scissor Game Using C++

Rock Paper and Scissors Game using C++ (With Source Code)

 Rock Paper and Scissors Game using C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a Rock Paper Scissors Game using C++ (With Source Code).

Rock Paper and Scissor Game Using C++

A “Rock, Paper, Scissors” game implemented using C++ programming language is a simple program to automate the traditional game of rock, paper, scissors. This project will let you play this game with the computer without the computer getting bored anytime, so you can play as many times as you want.

The program basically allows the user to select one of three options (rock, paper, or scissors) and then produces a random selection for the computer. It then calculates and shows the winner depending on the game rules.

Creating a School Fees Enquiry System using C++ (With Source Code)

Features of the game:

  • The user is asked to choose an option (rock, paper, or scissors),
  • Computer chooses one option at random.
  • The program decides and shows the winner depending on the choices.

Overview of the UI:

Rock, Paper and Scissors Game!
Choose one of the following options
(r) for rock
(p) for paper
(s) for scissors

The user will choose an option to perform the operation of their choice. User will then enter a values according to the prompts that will be displayed on the console to perform that operation.

Rock , Paper and Scissor Game Source Code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include <iostream>
// Constant variables
const char ROCK = 'r';
const char PAPER = 'p';
const char SCISSORS = 's';
using namespace std;
char getComputerOption() {
    // 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;
        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;
    cout << "Computer's choice is: "<< endl;
    cChoice = getComputerOption();
    chooseWinner(uChoice, cChoice);
    return 0;

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream  (input and output stream), ctime and time.h (time management).
  • Now we will declare constant variables of character type. The variables are – ROCK = ‘r’, PAPER = ‘p’, SCISSORS = ‘s’.
  • Write the using namespace std; line to avoid writing std:: everytime we user streams of various libraries.
  • Creating functions:
  • The getComputerOption() function will use  srand() and rand() functions from the cstlib library to get a random integer between 1 and 3. It returns a character reflecting the computer’s option based on the produced number: ‘r’ for rock, ‘p’ for paper, or ‘s’ for scissors.
  • The getUserOption() function will declare a char type variable c for the user input of r, p, or s. The function will print the game name – Rock, Paper and Scissors Game!, along with a prompt for the user to choose among – (r) for rock, (p) for paper, (s) for scissors. We will write a while loop to the user’s choice.
  • The showSelectedOption() function will take char type variable called option as an argument. We will be writing if statements for the option selections and printing it out on the console, while option user has selected.
  • The chooseWinner function will take uChoice – user’s choice and cChoice – computer’s choice as arguments. Creating an if-else if control statements block to check for the win. Based on the game rules, the function will determine the winner or if it’s a tie and displays the result using cout statements.
  • For example: If the choice of the user is same as the computer’s choice, then the game ties.
  •  The main() function will declare variables – uChoice and cChoice to store the user’s choice and the computer’s choice, respectively.
  • To get the user’s choice we will call the getUserOption() function, and the selected option is displayed using the showSelectedOption(uChoice) function.
  • The getComputerOption() function will be used to generate the computer’s decision, while the showSelectedOption() function is used to display the selected option. To identify and show the winner, the chooseWinner function is called with the user’s option and the computer’s decision.
  • At last, the main function returns 0, indicating successful execution of the program.

Snake Game in C++

Final Output:-

Here is an example to show how this project works.

Rock Paper and Scissor Game Using C++

Rock Paper and Scissor Game Using C++

Video Output:


We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Simple Address Book Project Using C++ Source Code

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow : CodeWithRandom

Leave a Reply