Code Snippets

Rock, Paper & Scissors Game

                
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int generateRandomNumber(int n)
    {
        srand(time(NULL)); // srand takes seed as an input and is defined inside stdlib.h
        return 1 + (rand() % n);
    };
    
    int main()
    {
        int a = 0, b = 0, option, rounds = 3, computer = generateRandomNumber(3);
        char name[20];
    
        printf("\n**********Welcome to Rock, Paper and Scissor game made by Tiger**********\n\n");
    
        printf("Enter your name: ");
        scanf("%s", &name);
        getchar();
    
        printf("Enter the number of rounds: ");
        scanf("%d", &rounds);
        getchar();
    
        while (rounds > 0)
        {
            printf("\n******************************\n");
            printf("\n%s's turn\n", name);
            printf("\n1 - Rock");
            printf("\n2 - Paper");
            printf("\n3 - Scissor");
            printf("\n\nChoose your option: ");
            scanf("%d", &option);
            printf("\n");
    
            if (option == 1 && computer == 3)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Rock vs Scissor\n");
                printf("Rock wins\n\n");
                printf("%s wins!!!\n", name);
                printf("%s gets 1 point.\n", name);
                a++;
            }
            else if (option == 1 && computer == 2)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Rock vs Paper\n");
                printf("Paper wins\n\n");
                printf("Computer wins!!!\n");
                printf("Computer gets 1 point.\n");
                b++;
            }
            else if (option == 1 && computer == 1)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Rock vs Rock\n");
                printf("Draw\n");
                printf("Neither gets any point.\n");
            }
            else if (option == 2 && computer == 3)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Paper vs Scissor\n");
                printf("Scissor wins\n\n");
                printf("Computer wins!!!\n");
                printf("Computer gets 1 point.\n");
                b++;
            }
            else if (option == 2 && computer == 1)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Paper vs Rock\n");
                printf("Paper wins\n\n");
                printf("%s wins!!!\n", name);
                printf("%s gets 1 point.\n", name);
                a++;
            }
            else if (option == 2 && computer == 2)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Paper vs Paper\n");
                printf("Draw\n");
                printf("Neither gets any point.\n");
            }
            else if (option == 3 && computer == 2)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Scissor vs Paper\n");
                printf("Scissor wins\n\n");
                printf("%s wins!!!\n", name);
                printf("%s gets 1 point.\n", name);
                a++;
            }
            else if (option == 3 && computer == 1)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Scissor vs Rock\n");
                printf("Rock wins\n\n");
                printf("Computer wins!!!\n");
                printf("Computer gets 1 point.\n");
                b++;
            }
            else if (option == 3 && computer == 3)
            {
                printf("Computer's turn: %d\n\n", computer);
                printf("Scissor vs Scissor\n");
                printf("Draw\n");
                printf("Neither gets any point.\n");
            }
            else
            {
                printf("Choose a valid option!!!\n");
                rounds++;
            }
            rounds--;
            computer = generateRandomNumber(3);
        }
    
        if (a > b)
        {
            printf("\nCongratulations, %s wins the match with %d scores!!!\n", name, a - b);
        }
        else if (a < b)
        {
            printf("\nComputer wins the match with %d scores!!!\n", b - a);
        }
        else
        {
            printf("\nNeither wins the match!!!\n");
        }
    
        printf("\nSee you soon, have a nice day :)");
    
        return 0;
    };