Rock-Paper-Scissor Game in C++

     Hey there! Today we'll be building a simple Game: Rock-Paper-Scissor in C++ programming language. You can check it out on Google

Building a game in C++

This is a simple two player game in which players have to choose from rock, paper, and scissor and according to the choice rock wins over scissor, paper wins over rock and scissor wins over paper.

But today we are going to build it to play with computer/bot. Are you excited? So let's go...

 

  • LOGIC:

 The logic for this game is very simple. First we'll make a void function which will execute the logic of the program and then we'll call it in our main function. In logic function, we'll include choices for user, score, number of moves and apply a while loop which will check the choices made by user and bot and increase the score accordingly. When the while loop finish we'll print the winner of the game. Now let's move on to the algorithm behind it.

 

  • ALGORITHM:
  1. Start
  2. Make a void logic function
  3. In the function, declare variables for choices r = rock, p = paper and s = scissor and declare int variable for score initializing with 0.
  4. Also include a counter variable which will count the number of moves for user and let's initialize it with 6.
  5. And take one input from user and let's the first choice of the bot be 's'.
  6. Now apply while loop and input the condition ( comp and player score <5 & counter >0).
  7. In the loop first print the initial scores and then take input from user.
  8. Now apply if-else statements to check who wins and in it increase the score accordingly. Also decrease the number of moves for user.
  9. When the while loop completes, print the result.
  10. After coding the above logic function, call the function in main function.
  11. End


  • CODE:
#include <bits/stdc++.h>
using namespace std;

//logic function
void logic()
{
    //declare variables for choices
    char r='r',p='p',s='s';
    //declare scores
    int comp=0,plr=0;
    //declare inputs
    char in,c='s';
    //declare number of moves counter
    int counter=6

    //while loop execution with score and moves condition
    while((comp<5 && plr<5) && counter>0)
    {
        //print scores and moves left
        cout<<"Score : Bot : "<<comp<<" | Player : "<<plr<<" | Moves Left : "<<counter<<endl;
        cout<<"Enter Choice : ";
        //take input from user
        cin>>in;

        //apply if and else condition to check choices
        if (in==r && c==r)
        {
            cout<<"Draw\n"<<endl;
            //change bot choice
            c=r;
            //in case of draw, decrease the moves 
            counter--;
        }
        else if (in==p && c==p)
        {
            cout<<"Draw\n"<<endl;
            c=s;
            counter--;
        }
        else if (in==s && c==s)
        {
            cout<<"Draw\n"<<endl;
            c=p;
            counter--;
        }
        else if (in==p && c==r)
        {
            cout<<"Player Wins\n"<<endl;
            //change bot choice
            c=in;
            //increase score accordingly
            plr++;
            //decrease moves
            counter--;
        }
        else if (in==s && c==r)
        {
            cout<<"Bot Wins\n"<<endl;
            c=in;
            comp++;
            counter--;
        }
        else if (in==r && c==p)
        {
            cout<<"Bot Wins\n"<<endl;
            c=r;
            comp++;
            counter--;
        }
        else if (in==s && c==p)
        {
            cout<<"Player Wins\n"<<endl;
            c=r;
            plr++;
            counter--;
        }
        else if (in==r && c==s)
        {
            cout<<"Player Wins\n"<<endl;
            c=p;
            plr++;
            counter--;
        }
        else if (in==p && c==s)
        {
            cout<<"Bot Wins\n"<<endl;
            c=in;
            comp++;
            counter--;
        }
        else
        {
            //in case of invalid input from user 
            cout<<"Invalid Input"<<endl;
        }
        
    }

    /*if player reaches score 5 first
    then player is the winner*/
    if (plr==5)
    {
        //print score and win statement
        cout<<"Score : Bot : "<<comp<<" | Player : "<<plr<<endl;
        cout<<"You Are Winner";
    }
    //in case player run out of moves
    else if (counter==0)
    {
        cout<<"Score : Bot : "<<comp<<" | Player : "<<plr<<" | Moves Left : "<<counter<<endl;
        cout<<"You Ran Out Of Moves"<<endl;
        cout<<"Bot Is Winner, Try Again";
    }
    //else bot wins
    else
    {
        cout<<"Score : Bot : "<<comp<<" | Player : "<<plr<<endl;
        cout<<"Bot Is Winner, Try Again";
    }
    
}

int main()
{
    //header for the game
    cout<<"\n Rock-Paper-Scissor\n";
    cout<<"---------------------\n";
    cout<<"Choose r:rock, p:paper, s:scissor\n"<<endl;

    //call the logic function
    logic();
    
    return 0;
}
 

 

  • OUTPUT:


 Rock-Paper-Scissor
---------------------
Choose r:rock, p:paper, s:scissor

Score : Bot : 0 | Player : 0 | Moves Left : 6
Enter Choice : p
Bot Wins

Score : Bot : 1 | Player : 0 | Moves Left : 5
Enter Choice : r
Bot Wins

Score : Bot : 2 | Player : 0 | Moves Left : 4
Enter Choice : s
Bot Wins

Score : Bot : 3 | Player : 0 | Moves Left : 3
Enter Choice : p
Bot Wins

Score : Bot : 4 | Player : 0 | Moves Left : 2
Enter Choice : s
Player Wins

Score : Bot : 4 | Player : 1 | Moves Left : 1
Enter Choice : r
Draw

Score : Bot : 4 | Player : 1 | Moves Left : 0
You Ran Out Of Moves
Bot Is Winner, Try Again  

 

That's all for today. I hope you enjoyed. Still if you've any problem then don't hesitate to ask.

If you enjoyed then don't forget to 

Share, Subscribe and keep coding...

Thanks.

Comments