Open In App

Rock Paper Scissor Game in C++

Last Updated : 12 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Stone Paper Scissor or Rock Paper Scissor is a game played between two players where each player in this game forms one of three shapes and the winner is decided as per the rules of the game.

Description of the Project

It is a simple command line interface project for Rock, Paper, and Scissors games. The user gets the first chance to pick the Rock, Paper, or Scissors. After that computer makes a choice randomly and the winner is decided as per the game rules.

rock paper scissor in C++

 

Game Rules

We have three scenarios to consider:

  • Rock vs. Paper -> Paper wins.
  • Paper vs. Scissor -> Scissor wins.
  • Scissor vs. Rock -> Rock wins.

If both players use the same shape, it is considered a draw.

Prerequisites: C++ Basics, Functions, Random Number Generation, Conditional Statements

Components of the Program

The game is implemented with the help of the following components:

  • Header Files: A necessary standard template library has been added for performing operations such as Random number generation, input/output, etc.
  • getResults() Function: It compares the input of the computer and the player and gives the result of 1 for the win, 0 for the tie, and -1 for the loss.
  • getComputerMove() Function: This function uses a random number generator and returns the computer move based on the randomly generated number.
  • main() Function: In this, the main logic of the code is written, the player needs to choose ‘s’ for (stone), ‘p’ for ( paper), or ‘z’ for (scissors). Then random input will be generated by the computer and the game function is called. this function will return an integer for the result and based on that result winner will be shown.

C++ Program to Implement Rock-Paper-Scissors

C++




// C++ Program to Implement Rock-Paper-Scissors
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
  
// get the computer move
char getComputerMove()
{
    int move;
    // generating random number between 0 - 2
    srand(time(NULL));
    move = rand() % 3;
  
    // returning move based on the random number generated
    if (move == 0) {
        return 'p';
    }
    else if (move == 1) {
        return 's';
    }
    return 'r';
}
  
// Function to return the result of the game
int getResults(char playerMove, char computerMove)
{
    // condition for draw
    if (playerMove == computerMove) {
        return 0;
    }
  
    // condition for win and loss according to game rule
    if (playerMove == 's' && computerMove == 'p') {
        return 1;
    }
    if (playerMove == 's' && computerMove == 'r') {
        return -1;
    }
    if (playerMove == 'p' && computerMove == 'r') {
        return 1;
    }
    if (playerMove == 'p' && computerMove == 's') {
        return -1;
    }
    if (playerMove == 'r' && computerMove == 'p') {
        return -1;
    }
    if (playerMove == 'r' && computerMove == 's') {
        return 1;
    }
      
    return 0;
}
  
// driver code
int main()
{
  
    char playerMove;
  
    cout << "\n\n\n\t\t\tWelcome to Stone Paper Scissor "
            "Game\n";
  
    cout << "\n\t\tEnter r for ROCK, p for PAPER, and s "
            "for SCISSOR\n\t\t\t\t\t";
  
    // input from the user
    while(1) {
        cin >> playerMove;
        if (playerMove == 'p' || playerMove == 'r' || playerMove == 's') {
            break;
        }
        else {
            cout << "\t\t\tInvalid Player Move!!! Please Try Again." << endl;
        }
    }
      
      
  
    // computer move
    char computerMove = getComputerMove();
  
    int result = getResults(playerMove, computerMove);
  
    // priting result based on who won the game
    if (result == 0) {
        cout << "\n\t\t\tGame Draw!\n";
    }
    else if (result == 1) {
        cout << "\n\t\t\tCongratulations! Player won the "
                "game!\n";
    }
    else {
        cout << "\n\t\t\tOh! Computer won the game!\n";
    }
  
    // showing both  moves
    cout << "\t\t\tYour Move: " << playerMove << endl;
    cout << "\t\t\tComputer's Move: " << computerMove << endl;
  
    return 0;
}


Output

Output of the Program

Related Articles



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads