Introduction
Rock, Paper, Scissors is a simple hand game commonly played between two players. Each player simultaneously forms one of three shapes with an outstretched hand. The possible shapes are:
- Rock (a fist)
- Paper (an open hand)
- Scissors (a fist with the index and middle fingers extended, forming a V)
The objective of the game is to choose a shape that defeats the opponent’s shape, as follows:
- Rock crushes Scissors
- Scissors cuts Paper
- Paper covers Rock
In this program, we will simulate a single-player game where the computer randomly selects one of the shapes, and the user must input their choice. The program will then determine who wins the game based on the rules above.
Objective
The objective of this program is to develop a simple C++ application where a user can play the Rock, Paper, Scissors game against the computer. The program should:
- Allow the user to choose Rock, Paper, or Scissors.
- Have the computer randomly select one of the three choices.
- Determine the winner based on the standard rules of the game.
- Display the results of the game to the user.
Code Implementation
#include
#include
#include
using namespace std;
int main() {
// Seed the random number generator
srand(time(0));
// Display game instructions
cout << "Welcome to the Rock, Paper, Scissors Game!" << endl;
cout << "Please choose one of the following:" << endl;
cout << "1. Rock" << endl;
cout << "2. Paper" << endl;
cout << "3. Scissors" << endl;
// Get the user's choice
int userChoice;
cout << "Enter your choice (1-3): "; cin >> userChoice;
// Validate user input
if (userChoice < 1 || userChoice > 3) {
cout << "Invalid choice! Please choose 1, 2, or 3." << endl;
return 1;
}
// Generate a random choice for the computer (1: Rock, 2: Paper, 3: Scissors)
int computerChoice = rand() % 3 + 1;
// Display choices
cout << "You chose: ";
if (userChoice == 1) cout << "Rock" << endl;
else if (userChoice == 2) cout << "Paper" << endl;
else cout << "Scissors" << endl;
cout << "The computer chose: ";
if (computerChoice == 1) cout << "Rock" << endl;
else if (computerChoice == 2) cout << "Paper" << endl;
else cout << "Scissors" << endl;
// Determine the winner
if (userChoice == computerChoice) {
cout << "It's a tie!" << endl;
} else if ((userChoice == 1 && computerChoice == 3) ||
(userChoice == 2 && computerChoice == 1) ||
(userChoice == 3 && computerChoice == 2)) {
cout << "You win!" << endl;
} else {
cout << "Computer wins!" << endl;
}
return 0;
}
Explanation of Program Structure
The Rock, Paper, Scissors game is implemented with the following structure:
- Random Number Generation: We use
rand()from thecstdliblibrary to simulate the computer’s random selection of Rock, Paper, or Scissors. Thetime(0)function is used to seed the random number generator, ensuring different results each time the program runs. - User Input: The program prompts the user to choose an option by entering a number from 1 to 3. It checks whether the input is valid (between 1 and 3) and handles invalid input gracefully.
- Decision Logic: The program compares the user’s choice to the computer’s choice based on the rules of the game. It checks if both choices are the same (tie), or if one beats the other, and displays the result accordingly.
How to Run the Program
To run this program, follow these steps:
-
- Copy the C++ code into a text editor.
- Save the file with a .cpp extension, for example, rock_paper_scissors.cpp.
- Open your terminal or command prompt and navigate to the folder where the file is saved.
- Compile the program using a C++ compiler. For example, if you’re using g++, the command would be:
g++ rock_paper_scissors.cpp -o rock_paper_scissors
-
- Run the compiled program with the following command:
./rock_paper_scissors
- The program will prompt you to choose Rock, Paper, or Scissors, and then it will display the result.


I precisely needed to thank you very much once more. I do not know what I would’ve undertaken without those tips and hints provided by you regarding such a question. This was a very intimidating concern in my opinion, however , taking a look at this well-written fashion you dealt with the issue forced me to jump for fulfillment. Now i’m happy for this service and then wish you really know what an amazing job that you’re getting into educating some other people through the use of your blog post. I’m certain you’ve never come across all of us.