Stone Paper Scissors game in C++

 In this article, we will create a simple Stone Paper Scissors game with the help of C++. In this game, the user competes against the computer and the one with the highest no. of points wins.


Stone paper scissors game code in c++
Stone paper scissors game in C++



The following header files are used in making this game - 
  • iostream - for input or output stream.
  • time.h - to get date and time info.


The following functions are used to include randomness in the choices made by the Computer - 
  • rand() - to generate pseudo-random integers in the range 0 to 32767.
  • srand() -  the rand() function will generate the same random integer every time we run the program. Hence to generate different random numbers, we use srand() function by passing time as the seed.


The following user-defined functions are created - 
  • int computerchooses() - This function randomly generates and returns numbers between 1 and 3 based on which either stone(1) or paper(2) else scissor is selected by the computer. This function takes in no parameters and returns an integer.
  • int results(int n, int cn) - This function takes the choices made by the user and computer and decides the output of the match i.e. whether the user won or the computer won or was the match a tie! This function takes in 2 integer parameters 'n' - the choice made by the user and 'cn' - the choice made by the computer and returns an integer.


Code -
//Stone Paper Scissors game using C++ by Vishruth Codes
#include<iostream>
#include<stdio.h>
#include<time.h>
using namespace std;

int computerchooses()
{
    //Function to randomly generate and return nos. between 1 and 3 based on which either stone(if 1's generated) or paper(if 2's generated) else scissor is selected by the computer
    int res;
    srand(time(0));
    res=1+(rand()%3);
    //cout<<"\nThe r no is :"<<res;
    return res;
}

int results(int n, int cn)
{
    //n - User's choice
    //cn - Computer's choice
    //0 - If player lost
    //1 - If player won
    //2 - The game is a tie
    if(n==1 && cn==2)
    {
        return 0;
    }
    else if(n==1 && cn==3)
    {
        return 1;
    }
    else if(n==1 && cn==1)
    {
        return 2;
    }
    else if(n==2 && cn==3)
    {
        return 0;
    }
    else if(n==2 && cn==1)
    {
        return 1;
    }
    else if(n==2 && cn==2)
    {
        return 2;
    }
    else if(n==3 && cn==1)
    {
        return 0;
    }
    else if(n==3 && cn==2)
    {
        return 1;
    }
    else //if(n==3 && cn==3)
    {
        return 2;
    }
}

//The manager
int main()
{
    int n,i=0,cn,res;
    int maxpts=0;
    char name[15];
    cout<<"\nEnter your Name: ";
    cin>>name;
    cout<<"\nYou are about to play Stone-Paper-Scissor against computer.";
    cout<<"\nEnter the maximum points:- ";
    cin>>maxpts;
    int plrpts=0, cspts=0;
    while(plrpts<=maxpts && cspts<=maxpts)
    {
    i++;
    cout<<"\n\n|-----MATCH "<<i<<"-----|";
    //Player: User
    cout<<"\nEnter -\n1. for Stone\n2. for Paper\n3. for Scissors\n:- ";
    //This is the basis for the choices of both computer and user.
    cin>>n;
    if(n==1)
    {
        cout<<"\nPlayer "<<name<<" chose Stone.";
    }
    else if(n==2)
    {
        cout<<"\nPlayer "<<name<<" chose Paper.";
    }
    else //if(n==3)
    {
        cout<<"\nPlayer "<<name<<" chose Scissor.";
    }
    //Player: Computer
    cn=computerchooses();
    if(cn==1)
    {
        cout<<"\nComputer chooses Stone.";
    }
    else if(cn==2)
    {
        cout<<"\nComputer chooses Paper.";
    }
    else //if(cn==3)
    {
        cout<<"\nComputer chooses Scissor.";
    }
    //Deciding the winner
    res=results(n,cn);
    if(res==0)
    {
        cout<<"\nComputer won!";
        cspts=cspts+1;
        cout<<"\n\nThe points earned by - \n"<<name<<" - "<<plrpts<<"\nComputer - "<<cspts;
    }
    else if(res==1)
    {
        cout<<"\nPlayer "<<name<<" won!";
        plrpts=plrpts+1;
        cout<<"\n\nThe points earned by - \n"<<name<<" - "<<plrpts<<"\nComputer - "<<cspts;
    }
    else //(res==2)
    {
        cout<<"\nThe match was a tie!";
        cout<<"\n\nThe points earned by - \n"<<name<<" - "<<plrpts<<"\nComputer - "<<cspts;
    }
}

//Final winner declaration
if(cspts>=plrpts)
{
    cout<<"\nThe result is - COMPUTER WINS THE GAME. Better luck next time "<<name<<".";
}
else{
    cout<<"\nThe result is - Congratulations! "<<name<<" WINS THE GAME";
}
return 0;
}


Subject - 
In the above example, I have implemented the Stone Paper Scissors game using C++. I have used the concept of functions.


Basic features of the game - 
  • In this game, the user plays against the computer.
  • The user can set the maximum points and play accordingly.
  • When the user wins the match, he/she/they will be awarded 1 point, whereas on losing, the computer is awarded 1 point.
  • The player who crosses the maximum points first will be declared the winner.


Working - 
  1. In this program, the user is asked to choose an object (stone-1, paper-2, or scissor-3) by entering the number associated with it, and the user's choice is displayed accordingly.
  2. To determine the computer's choice, the function computerchooses() is called by the main(), which randomly generates a number between 1 & 3 ([1,3]) and returns it back to the main(). That generated number is compared with the number associated with the objects and the computer's choice is determined and displayed accordingly.
  3. To determine the winner, function results(int n, int cn) is called by the main() by passing the user's (n) and the computer's (cn) choice as arguments. Based on the rules of the game, determines the winner or whether the match was a tie and returns the result back to the main()
  4. The winner receives 1 point or else if the match was a tie, none of the players (user/computer) receive any point. The total points earned by the players are displayed.
  5. This repeats until the maximum points set by the user are crossed by any one of the players. The first one to do so will be declared the winner of the game.


Output - 

Stone paper scissors using c++ - output
Output


Stone paper scissors using c++ - output
Output





Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.

Post a Comment

0 Comments