Stone-Paper-Scissor Game In C Language

Hey Friends.!
Welcome to my Blog…!!!
Come-On Friends Let’s proceed to another fantastic topic. In this Post, we will learn about how to code a Stone-Paper-Scissor Game in the C Language (User V/S Computer Mode).

We will use following ADDITIONAL Pre-Processor Directives along with the regular existing one in our program:
#include<dos.h>
This Pre-Processor Directive is use to
add functions like delay().This function take arguments in milliseconds.
Eg: delay(2000);
When This function is called the program waits for 2seconds (1 SECOND=1000 MILLISECOND) and then it executes the further part of program.

#include<time.h>
This Pre-Processor Directive is use to
update the values generated by rand() fuction everytime it’s called. When you run the rand() function it generates a random number. But when you want to generate random number for more than 1 time then you can’t use rand() function alone as it will display the same number everytime that was generated initially.
So, to update our values that are to be generated by rand() function; we will use “srand(time(NULL));” function & this function is supported by initializing the Pre-Processor Directive #include<time.h>


So, the main objective of our program is, we will conduct a 5 Round Game of Stone-Paper-Scissor between User and Computer in which the user will enter his choice either by typing 1 or 2 or 3 for stone or paper or scissor respectively where-else on the other hand Computer will also simultaneously generate a Random Option and the result will be generated for each round and this process will occur for 5 times. Also, the final result will be generated based on the performance of the player in the previous 5 rounds.


Also the following conditions are added
so as to know who won & who lost the game:
Condition for the USER
to WIN this game are:
1) If User Selects Rock
& Computer Selects Scissor (OR)
2) If User Selects Paper
& Computer Selects Rock (OR)
3) If User Selects Scissor
& Computer Selects Paper.

Where-else, the conditions for
COMPUTER to WIN this game are:
1) If Computer Selects Rock
& User Selects Scissor (OR)
2) If Computer Selects Paper
& User Selects Rock   (OR)
3) If Computer Selects Scissor
& User Selects Paper.

So, now let’s see
The code and it’s output:
Code:

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>

int main()
{
 char a;
 int i,j,k,user=0,comp=0;
 clrscr();
 printf("Hey Welcome To The 5 Round Game Of : \n");
 printf("--> STONE --- PAPER --- SCISSOR <--");
 printf("\n----- User V/S Computer Mode -----");
 delay(4000);
 printf("\n\nAre You Ready To Play This Game ?\nPress 'Y' For Yes & 'N' For No : ");
 scanf("%c",&a);
 if(a=='Y'||a=='y')
 {
  for(i=1;i<6;i++)
  {       printf("\nType 1 For: ROCK\nType 2 For: PAPER\nType 3 For: SCISSOR");
   printf("\nRound %d:",i);
   printf("\nYour Choice : ");
   scanf("%d",&k);
   srand(time(NULL));
   j=(rand()%3)+1;
   printf("Computer's Choice : %d",j);
   if(k==j)
    printf("\nIt's A Tie.!");
   else if((k==1 && j==2)||(k==2 && j==3)||(k==3 && j==1))
   {
    printf("\nComputer Wins This Round");
    comp=comp+1;
   }
   else if((k==1 && j==3)||(k==2 && j==1)||(k==3 && j==2))
   {
    printf("\nUser Wins This Round");
    user=user+1;
   }
   delay(2000);
   printf("\n\nResults After Round %d:",i);
   printf("\nUser : %d",user);
   printf("\nComputer : %d",comp);
   printf("\n\n------------------------------");
   if(i<5)
   {
    printf("\n\nGet Ready For The Round %d",i+1);
    delay(8000);
   }
   if(i==5)
   {
    printf("\n\nFinal Results:");
    if(user>comp)
     printf("\nUser Wins By %d - %d",user,comp);
    else if(user==comp)
     printf("\nIts's A Tie Between User & Computer By %d - %d",user,comp);
    else
     printf("\nComputer Wins By %d - %d",comp,user);
   }
  }
 }
 else
  printf("OK. Will Play Later");
 getch();
 return 0;

}

Output:






Thank-You.!!
Jai-Hind.!!  

Comments