cplusplus
cplusplus

 

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 the cstdlib library to simulate the computer’s random selection of Rock, Paper, or Scissors. The time(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:

    1. Copy the C++ code into a text editor.
    2. Save the file with a .cpp extension, for example, rock_paper_scissors.cpp.
    3. Open your terminal or command prompt and navigate to the folder where the file is saved.
    4. 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
    1. Run the compiled program with the following command:
./rock_paper_scissors
  1. The program will prompt you to choose Rock, Paper, or Scissors, and then it will display the result.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)