Skip to content

codehelping.com

Best Platform to learn AI & Coding

  • Home
  • Projects
  • Notes
  • Blog
  • Contact Us

Build a Rock Paper Scissors Game in C++ | Full Guide & Code

Posted on February 1, 2025August 13, 2025 By Omkar Pathak No Comments on Build a Rock Paper Scissors Game in C++ | Full Guide & Code
C++ Projects, Project

Rock Paper Scissors Game in C++ is a game where players choose one of three items: Rock, Paper and Scissors and the winner is determined based on predefined rules.

In this blog, we will look at a simple implementation of the game in C++ that lets the player compete against the computer.

Build a Rock Paper Scissors Game in C++

Rock Paper Scissors Game: The Rule

There are 3 simple rules:

  1. Rock crushes Scissors
  2. Scissors cuts Paper
  3. Paper covers Rock

A tie happens when both players choose the same option.

The Components in this game?

A) Random Choice for the Computer:

The computer selects its move randomly which is done by generating a random number between 0 and 99, which is then mapped to either Rock ('r'), Paper ('p'), or Scissors ('s') based on specific ranges.

For example:

  1. If the number is between 0 and 32, the computer picks Rock.
  2. If the number is between 33 and 66, it picks Paper.
  3. If the number is between 67 and 99, it picks Scissors.

This randomness ensures that the computer’s choices aren’t predictable.

Player’s Input: The player is prompted to enter their choice: Rock ('r'), Paper ('p'), or Scissors ('s'). This is compared to the computer’s choice to determine the outcome of the round.

Outcome Determination: The function rule checks the two choices and determines the result. It returns:

  • 1 if the player wins,
  • -1 if the player loses,
  • 0 if it’s a tie.

Let’s see the game flow:

The game operates in a loop, allowing the player to play multiple rounds:

  1. The computer makes a random choice.
  2. The player enters their choice.
  3. The program compares the two choices and announces whether the player wins, loses, or ties.
  4. The player is then asked if they want to play again. If they enter ‘n’, the game ends; otherwise, it continues.

Building user Interface:

The game includes simple text-based prompts, with the following features: Instructions for the player, a display of the result after each round, the program’s output makes it easy for the player to follow along and decide whether to play again.

Rock Paper Scissors Game C++ 2025 Source Code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int rule(char p, char c) {
    if (p == c) {
        return 0; // tie
    }
    if ((p == 'r' && c == 's') || 
        (p == 'p' && c == 'r') || 
        (p == 's' && c == 'p')) {
        return 1; // player wins
    }
    return -1; // computer wins
}

int main() {
    char computer, player, playmore;

    cout << "\t\t\t\t";
    for (int i = 0; i < 50; i++) cout << "-";
    cout << "\n\t\t\t\t Welcome to Rock, Paper and Scissors Game\n\t\t\t\t";
    for (int i = 0; i < 50; i++) cout << "-";
    cout << "\n\t\t\t\t Note:\n\t\t\t\t";
    cout << "\t\t r : Rock\n\t\t\t\t\t p : Paper\n\t\t\t\t\t s : Scissors\n\n\t\t\t\t";
    for (int i = 0; i < 50; i++) cout << "-";
    cout << endl;

    srand(time(0)); // seed once, outside the loop

    do {
        int number = rand() % 100; // 0-99
        if (number < 33)
            computer = 'r';
        else if (number > 66)
            computer = 's';
        else
            computer = 'p';

        cout << "\t\t\t\tEnter your choice: ";
        cin >> player;

        int result = rule(player, computer);
        cout << "\t\t\t\tComputer chose: " << computer << endl;

        if (result == 1)
            cout << "\t\t\t\tYou won! Hurray\n";
        else if (result == -1)
            cout << "\t\t\t\tYou lose! Bad Luck\n";
        else
            cout << "\t\t\t\tWoah! That's a Tie!\n";

        cout << "\t\t\t\tDo you want to Play Again?\n\t\t\t\t";
        cout << "Note: Press 'n' to exit! Press Anything to continue: ";
        cin >> playmore;

        cout << "\t\t\t\t";
        for (int i = 0; i < 50; i++) cout << "-";
        cout << endl;

    } while (playmore != 'n');

    return 0;
}
Build a Rock Paper Scissors Game in C++ | Full Guide & Code

Conclusion

This Rock Paper Scissors game in C++ is a fun and simple implementation that allows you to practice basic programming concepts like conditionals, loops, and random number generation. Whether you’re a beginner or just looking for a small project to practice your skills, this game is an excellent starting point.

Thanks for visiting codehelping.com, here you can find more such projects.

Post navigation

❮ Previous Post: DeepSeek-R1 vs. ChatGPT: The Next AI Revolution?
Next Post: A Simple Student Management System in C++: 2025 | Source Code ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact Us
  • Privacy Policy
  • Disclaimer

Copyright © 2025 codehelping.com.

Theme: Oceanly by ScriptsTown

Social Chat is free, download and try it now here!