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.
Rock Paper Scissors Game: The Rule
There are 3 simple rules:
- Rock crushes Scissors
- Scissors cuts Paper
- 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:
- If the number is between 0 and 32, the computer picks Rock.
- If the number is between 33 and 66, it picks Paper.
- 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:
1if the player wins,-1if the player loses,0if it’s a tie.
Let’s see the game flow:
The game operates in a loop, allowing the player to play multiple rounds:
- The computer makes a random choice.
- The player enters their choice.
- The program compares the two choices and announces whether the player wins, loses, or ties.
- 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;
}
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.