Rock-Paper-Scissor Game In C (Project-1)

Rock-Paper-Scissor Game Project using C Language:


In this tutorial we will learn how to create Rock-Paper-Scissor game project using C language.


  Quick links:




In these program we used two new header files one is <stdlib.h> and second is <time.h><stdlib.h> header file  used because

in the program we have used srand() function and rand() function. This two functions are defined in <stdlib> header file.

<time.h> header file used because, inside the our program we have used time() function.  time() function gets current system time as a structure.



Ex: How to use random number function in C. Using rand() function make a Rock-Paper-Scissor game for a project. Write a C program which gives you Rock-Paper-Scissor game.


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{ 

        srand(time(NULL));
        
       /*Variable declaration/initialization*/
        int lim;
        int yscore = 0, cscore = 0;
        int c=0;
        int num;
        
        
        /*Print welcome message*/
        printf("*WELCOME To ROCK PAPER SCISSOR GAME*\n\n");
        
        printf ("Enter how many matches you want to play: ");
        scanf("%d",&lim);
        
        /*Run loop for game matches (limit)*/
        for(int i=1;i<=lim;i++)
        {       
              printf("\n\nEnter 1 for ROCK\nEnter 2 for PAPER\nEnter 3 for SCISSOR\n: ");        
              scanf("%d",&num);
       
       
        /*Add number inside the switch and use 3 numbers cases*/      
       switch(num)
       {
         case 1: printf("You have choosed ROCK");
         break;
       
         case 2: printf("You have choosed PAPER");
         break;
       
         case 3: printf("You have choosed SCISSOR");
         break;
       
         default:
         printf("Enter correct choice");
         c=1;
         break;
        
       }
       
              
       //For random number between 1 to 3    
    	int randN = 0;

        randN = rand()%(3)+1;
       
       if(c==0)
       {
       
       if(randN==1)
       {
         printf("\nComputer have choosed ROCK ");
       }
      else if(randN==2)
       {
         printf("\nComputer have choosed PAPER ");
       }
     else if(randN==3)
       {
         printf("\nComputer have choosed SCISSOR");
       }
       
       if(randN==num)
       {
          printf("\nGAME IS TIE");
       }
       else if(num==1 && randN==2)
       {
         printf("\nYOU LOSE!");
         cscore++;
       }
       else if(num==1 && randN==3)
       {
         printf("\nYOU WIN!");
         yscore++;
       }
       else if(num==2 && randN==3)
       {
         printf("\nYOU LOSE!");
         cscore++;
       }
       else if(num==2 && randN==1)
       {
         printf("\nYOU WIN!");
         yscore++;
       }
       else if(num==3 && randN==1)
       {
         printf("\nYOU LOSE!");
         cscore++;
       }
       else if(num==3 && randN==2)
       {
         printf("\nYOU WIN!");
         yscore++;
       }                          
      }
    }
    
       if(lim>1&&yscore>cscore)
       {
         printf("\nYour Score is %d\nComputer score is %d\nYOU WIN THIS GAME!",yscore,cscore);
       }
       else if(lim>1&&cscore>yscore)
       {
         printf("\nYour Score is %d\n Computer score is %d\nYOU LOSE THIS GAME!",yscore,cscore);
       }
       else if(lim>1)
       {
         printf("\nYour Score is %d\n Computer score is %d\nGAME IS TIE!",yscore,cscore);
       }
        
} 
  


Above program shows the following output:

Rock-Paper-Scissor game project in C


If you enjoyed it, then please share it with your friends and don't forgot to comment your thoughts below in the comment section :)


  You May Also Like:



No comments:

Post a Comment

If you have any doubts, please discuss here...👇