× Home
Next Lec → ← Previous Lec

Coding Rock, Paper, Scissors Exercise

here we will include <time.h> as we are going to use some of their function

generating random numbers

                        

                         #include <stdio.h>
                         #include <stdlib.h> //to use srand() is guess
                         #include <time.h>
                         int main()
                         {
                            srand(time(NULL)); //srand takes seed as an input and is defined inside stdlib.h
                            //if we pass no. it is going to generate only one value that's why we pass time(NULL) which returns
                            //seconds and as time keeps on changing we get different value each time.
                            printf("The random between 0 to 100 is %d\n",rand()%100);
                            printf("The random between 0 to 30 is %d\n",rand()%30);
                            return 0;
                         }
                        
                    

we have to create Rock, Paper and Scissors game
Player 1 → choose rock
Player 2 (computer) → chose scissors → then player 1 gets 1 point
rock vs scissors - rock wins
paper vs scissors - scissors wins
paper vs scissors - paper wins

Write a C program to allow a user to play this game three times with computer. Log the scores of computer the player. Display the name of the winner at the end.
notes: You have to display name of the player during the game. Take user name as an input from the user.

                    

                            #include <stdio.h>
                            #include <stdlib.h>
                            #include <string.h>
                            #include <time.h>
                            int score_user = 0;
                            int score_comp = 0;
                            int genR()
                            {
                                srand(time(NULL));
                                return rand() % 3;
                            }
                            int whoWon(int guess, int ran, char name[])
                            {
                                if (guess == 0)
                                {
                                    if (ran == 0)
                                    {
                                        printf("Tie between both");
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                    else if (ran == 1)
                                    {
                                        printf("computer won this round");
                                        score_comp++;
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                    else if (ran == 2)
                                    {
                                        printf("%s won this round", name);
                                        score_user++;
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                }
                                else if (guess == 1)
                                {
                                    if (ran == 0)
                                    {
                                        printf("%s won this round", name);
                                        score_user++;
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                    else if (ran == 1)
                                    {
                                        printf("Tie between both");
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                    else if (ran == 2)
                                    {
                                        printf("computer won this round");
                                        score_comp++;
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                }
                                else if (guess == 2)
                                {
                                    if (ran == 0)
                                    {
                                        printf("Computer won this round");
                                        score_comp++;
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                    else if (ran == 1)
                                    {
                                        printf("%s won this round", name);
                                        score_user++;
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                    else if (ran == 2)
                                    {
                                        printf("Tie between both");
                                        printf("\n***Scoreboard***\n %s = %d and computer = %d", name, score_user, score_comp);
                                    }
                                }
                            }
                            
                            int main()
                            {
                                char options[3][10] = {
                                    "rock",
                                    "paper",
                                    "scissors"};
                            
                                int guess;
                                int ran;
                                char name[20];
                                int i=0;
                            
                                printf("*********** ROCK PAPER SCISSORS GAME ********************\n");
                                printf("Enter player name :");
                                scanf("%s", &name);
                                printf("%s is playing against computer\n", name);
                            
                                while (i < 3)
                                {
                                    printf("\n**********  ROUND %d  *********\n", i + 1);
                                    printf("%s \nPress 0 for selecting rock\nPress 1 for selecting paper\nPress 2 for selecting scissors : ", name);
                                    scanf("%d", &guess);
                                    if (guess == 0)
                                    {
                                        ran = genR();
                                        printf("%s : %s\n", name, options + guess);
                                        printf("Computer : %s\n", options + ran);
                                        whoWon(0, ran, name);
                                    }
                                    else if (guess == 1)
                                    {
                                        ran = genR();
                                        printf("%s : %s\n", name, options + guess);
                                        printf("Computer : %s\n", options + ran);
                                        whoWon(1, ran, name);
                                    }
                                    else if (guess == 2)
                                    {
                                        ran = genR();
                                        printf("%s : %s\n", name, options + guess);
                                        printf("Computer : %s\n", options + ran);
                                        whoWon(2, ran, name);
                                    }
                                    i++;
                                }
                                if(score_user>score_comp)
                                {
                                    printf("\n%s is the winner!!\n",name);
                                } else if(score_comp>score_user)
                                {
                                    printf("\nComputer is the winner\n");
                                }else{
                                    printf("\nIts a Tie between both\n");
                                }
                                return 0;
                            }