Rock Paper Scissors Game in C++ is a classic game where players choose one of three items—Rock Paper 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.
Game Logic: Rock Paper Scissors Game
The game is based on simple rules:
- Rock crushes Scissors
- Scissors cuts Paper
- Paper covers Rock
A tie happens when both players choose the same option.
Key Components
Random Choice for the Computer: In the program, the computer selects its move randomly. This 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, making the game more interesting.
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.
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.
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.
- A prompt to replay the game or exit.
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; } if (p == 'r' && c == 'p'){ return -1; } else if (p == 's' && c == 'p'){ return 1; } else if (p == 'p' && c == 'r'){ return 1; } else if (p == 's' && c == 'r'){ return -1; } else if (p == 'r' && c == 's'){ return 1; } else if (p == 'p' && c == 's'){ return -1; } } int main(){ char computer; char player; char playmore; cout << "\t\t\t\t"; for(int i = 0; i < 50; i++){ cout << "-"; } cout << endl; cout << "\t\t\t\t"; cout << "\t Welcome to Rock, Paper and Scissors Game" << endl; cout << "\t\t\t\t"; for(int i = 0; i < 50; i++){ cout << "-"; } cout << endl; cout << "\t\t\t\t"; cout << "\t Note: " << endl; cout << "\t\t\t\t"; cout << "\t\t r : Rock" << endl << "\t\t\t\t" << "\t\t p - Paper" << endl << "\t\t\t\t" << "\t\t scissor" << endl << "\t\t\t\t"<< endl << endl; cout << "\t\t\t\t"; for(int i = 0; i < 50; i++){ cout << "-"; } cout << endl; do{ int number = 0; srand(time(0)); // initialized time to 0 number = rand() % 100; // will choose a number in range 0 - 99 // r - for rock // p - for paper // s - for scissors if (number < 33) { computer = 'r'; } else if (number > 66) { computer = 's'; } else { computer = 'p'; } // cout << "Note: \"r\" for \"Rock\", \"p\" for \"Paper\", \"s\" for \"Scissor\"." << endl; cout << "\t\t\t\t"; cout << "Enter your choice: "; cin >> player; int result = rule(player, computer); if(result == 1){ cout << "\t\t\t\t"; cout << "You won! Hurray" << endl; } else if(result == -1){ cout << "\t\t\t\t"; cout << "You lose! Bad Luck" << endl; } else{ cout << "\t\t\t\t"; cout << "Woah! That's Tie!" << endl; } cout << "\t\t\t\t"; cout << "Do you want to Play Again?" << endl; cout << "\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.