• Register
search
Log In

Intro

We often play rock paper scissors for fun activities. Since computers can generate random numbers so they can make random decisions. So, with this ability computers can do a lot of things and in this article we are going to write a fun program of a game called rock paper scissor.

Approach

Here is the quick overview of what is going to happen in the program.

  • Giving choice to the user for selecting Rock, Paper and Scissor.

  • Getting the input for the choice and showing back what the user chose.

  • Showing the results of the computer.

  • And in the end, showing the results of the game.

 

Algorithm

  • Declare a variable computer, computer generates random number in between 1 and 3.

  • Declare a variable user, user stores the decision made by the user during the game.

  • Define three different strings: rock, paper and scissor.

  • Greetings with the name of the game “Rock Paper Scissor”.

  • Ask users for their choice stored as “user”. Show the choice.

  • Computer makes a random choice. Stored as “computer”. Show the choice.

  • If the user is equal to the computer, then the match is drawn otherwise proceed to the next step.

  • If user = rock and computer = scissor, display “You won”.

  • If user = paper and computer = rock, display “You won”.

  • If user = scissor and computer = paper, display “You won”.

  • Else display “You lose or computer won”.

  • End of the program.

Program

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

 int main()
 {
     std::srand(std::time(0));

     int computer = rand() % 3 + 1;
     int user;
     string rock = "1. Rock";
     string paper = "2. Paper";
     string scissor = "3. Scissor";

     cout << "======================================================" << endl;
     cout << "ROCK PAPER SCISSOR" << endl;
     cout << "======================================================" << endl;
     
     cout << "\n CHoose an Option" << endl;
     cout << rock << "\n" << paper << "\n" << scissor << endl;

     cout << "Enter your choice here : ";
     cin >> user;
     cout << "\n You chose ";

     //displaying choice of user
     switch (user)
     {case 1:
         cout << rock << endl;
         break;
      case 2:
         cout << paper << endl;
         break;
      case 3:
         cout << scissor << endl;
         break;
     
     default:
     cout << "Invalid Choice!!!" << endl;
         break;
     }

     //displaying computer's choice
     switch (computer)
     {
     case 1:
         cout << "Computer chose " << rock << endl;
         break;
     case 2:
         cout << "Computer chose " << paper << endl;
         break;
     case 3:
         cout << "Computer chose " << scissor << endl;
         break;
     
     default:
     cout << "Invalid CHoice!!!" << endl;
         break;
     }

     //displaying results
     if (user == computer)
     {
         cout << "==========================================" << "\n" << "!!!Match draw!!!" << "\n ================================================" << endl;

     }
     else if (user == 1 && computer == 3)
     {
         cout << "=========================================" << "\n !!! You Won !!!" << "\n ==============================================" << endl;

     }
     else if (user==2 && computer==1)
     {
         cout << "=========================================" << "\n !!! You Won !!!" << "\n ==============================================" << endl;

     }
     else if (user==3 && computer==2)
     {
         cout << "=========================================" << "\n !!! You Won !!!" << "\n ==============================================" << endl;

     }
     else
     {
         cout << "=========================================" << "\n !!! You Lose !!!" << "\n ==============================================" << endl;
     }
//just for holding the program when we run it without IDE as an application in windows
//you can ignore this
     int b;
     cin >> b;
     
     return 0;
 }

Output

======================================================
ROCK PAPER SCISSOR
======================================================

 CHoose an Option
1. Rock
2. Paper
3. Scissor
Enter your choice here : 1

 You chose 1. Rock
Computer chose 3. Scissor
=========================================
 !!! You Won !!!
 ==============================================
posted Jun 22 in c++ 100 points