Code of Rock, Paper, Scissor, Shoot!! – CoderInMe

 

In your childhood or may be some people today also play this game Rock, Paper, Scissor. It is easy to play and consume very less time but yes it is addicted so it may go longer.

So let’s start with its introduction,

“Rock, Paper, Scissor” (“Paper, Scissor & Stone” OR “Ro-Sham-Bo” OR “Bato-Bato-Pick”) is a simple game played with hands no need of any equipment or prompt all over the world with different names but same rules. The rules require that competing players use one hand to form one of three shapes at an agreed-upon time(sometimes cheated one!). The person that plays the strongest “object” is the winner of the game. It’s that easy!

 

Rock, Paper, Scissor in Coderinme

 

 

Rock, Paper, Scissor is divided into few parts like:

Part 1: The most important one, “The issue you want to solve”, from the last pizza slice to late night Maggi making.

Part 2: Now face off against each other. One hand in front and another one at your back to play it randomly. It should be played by two people at a time.

Part 3: Count Down Begins!!  Now, as per both of the agreement start playing the game like “Rock, Paper, Scissor, Shoot!! On the word “shoot,” both players have to reveal the mysterious object they choose through their hand.

  • Tap your closed playing hand against your palm as you count down. This will help you and your opponent get synchronized.
  • Be sure that you’re on the same page as far as timing you plays.

Part 4: Finally, Choose the winner according to the outcome of the both player.

 

Hence you get the idea how this game works, now let’s put this GAME ON in the form of code that’s also by JavaScript

Rock,Paper,Scissor Comparison Coderinme

 

 

var userChoice = prompt("Do you choose rock, paper or scissors?");

if(userChoice != "rock" || userChoice != "scissors" || userChoice != "paper")

userChoice = prompt("please choose rock, paper or scissors?");

var computerChoice = Math.random();

if (computerChoice < 0.34) {

computerChoice = "rock";

} else if(computerChoice <= 0.67) {

computerChoice = "paper";

} else {

computerChoice = "scissors";

} console.log("Computer: " + computerChoice);





var compare = function( choice1 , choice2 ){

if(choice1 === choice2 )

return "The result is a tie!";

else if( choice1 === "rock"){

if(choice2 === "scissors")

return "rock wins";

else

return "paper wins";

}

else if(choice1 === "paper"){

if(choice2 === "rock")

return "paper wins";

else

return "scissors wins";

}

else if(choice1 === "scissors"){

if(choice2 === "rock")

return "rock wins";

else if(choice2 === "paper")

return "scissors wins";

}

};

compare(userChoice ,computerChoice);

 

From the bunch of above code Let’s understand it by splitting in different portion:

First:

var userChoice = prompt("Do you choose rock, paper or scissors?");

if(userChoice != "rock" || userChoice != "scissors" || userChoice != "paper")

userChoice = prompt("please choose rock, paper or scissors?");

var computerChoice = Math.random();

if (computerChoice < 0.34) {

computerChoice = "rock";

} else if(computerChoice <= 0.67) {

computerChoice = "paper";

} else {

computerChoice = "scissors";

} console.log("Computer: " + computerChoice);

 

In this,

First of all, defining a variable userChoice and declaring the question to enter the user’s choice and storing it in the defined variable.After having the user’s choice its time for the computer choice so by using Math.random() function we can have a random choice of the computer.

As Math.random provide us with a number so we range them among three decimal number groups and store & use them as characters as you can see in the program.

 

Second:

var compare = function( choice1 , choice2 ){

if(choice1 === choice2 )

return "The result is a tie!";

else if( choice1 === "rock"){

if(choice2 === "scissors")

return "rock wins";

else

return "paper wins";

}

else if(choice1 === "paper"){

if(choice2 === "rock")

return "paper wins";

else

return "scissors wins";

}

else if(choice1 === "scissors"){

if(choice2 === "rock")

return "rock wins";

else if(choice2 === "paper")

return "scissors wins";

}

};

 

In this second portion, we are comparing the User’s choice with computer’s. If the user’s and computer’s choice are same then it will be declared as a “TIE”, otherwise, it will compare as:

If user’s choice is “Rock” and the computer is “Scissor” then user i.e. ROCK win as Rock crushes Scissor.

Rock Win in Rock Paper Scissor

 

Similarly, if the User choice is “Scissor” and computer is “Paper” then computer wins i.e. PAPER as Scissor cuts Paper

Scissor Wins in Rock,Paper,Scissor

and last but not least

If User choice is “Paper” and the computer is “Rock” then User wins i.e. Paper as Paper covers Rock.

Paper wins in Rock,Paper,Scissor Comparison

 

compare(userChoice ,computerChoice);

 

The last portion of the program is the most important one because it will start our comparison again and again by calling the function “function( choice1, choice2 )” in the second portion of the code.

Solution in C++

#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

void game(string c, string u){
    if(c==u)
        cout<<"Game Draw, try again";
    else if( c == "Rock"){
            if(u == "Scissor")
                cout<< "Computer wins";
            else
                cout<< "you win";
            }

            else if(c == "Paper"){
                    if(u == "Rock")
                        cout<< "Computer wins";
                    else
                        cout<< "you win";
                    }
            else if(c == "Scissor"){
                    if(u == "Rock")
                        cout<< "you win";
                    else if(u == "Paper")
                        cout<< "Computer wins";
                    }
}

int main(){
    string computer, user;
    int ran;
    srand(time(0));
    cout<<"Please enter your choice :"<<endl;
    do{
        cout<<"only Rock Paper or Scissor"<<endl;
        cin>>user;
    }
    while(user!="Rock" && user!="Paper" && user!="Scissor" );

    ran= rand()%100;

    if(0<=ran<=33)
        computer="Rock";
    else if(34<=ran<=67)
         computer="Paper";
    else
         computer="Scissor";

    game(computer, user);

    return 0;
}

 

You can also watch our video on our YouTube Channel “Coder InMe”

Note: All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A Salesforce Developer at AlmaMate Info Tech PVT LTD. An Aligarian and also your query solver in database field.This site mark the difference as Black or White to make you choose the best for you. Don't feel pressurized, feel confident..!!

Leave a reply:

Your email address will not be published.