jeudi 25 mars 2021

RockPaperScissor game not getting expected result - C programming

I was writing c program on rock paper and scissor game but could not get the expected result.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
    char player[50], choice[10];

    puts("Enter your name: ");
    gets(player);

    int player1 = 0, comp = 0;

    for (int i = 0; i < 3; i++)
    {
        srand(time(NULL));

        printf("%s: ", player);
        scanf("%s", choice);

        int computer = rand()%3;

        if (computer == 0){
            printf("computer: rock\n");
        } else if(computer == 1){
            printf("computer: paper\n");
        }else{
            printf("computer: scissor\n");
        }

        if (computer == 0 && choice == "paper"){
            player1++;
        }else if (computer == 0 && choice == "rock"){
            continue;
        }else if (computer == 0 && choice == "scissor"){
            comp++;
        }else if (computer == 1 && choice == "paper"){
            continue;
        }else if (computer == 1 && choice == "rock"){
            comp++;
        }else if (computer == 1 && choice == "scissor"){
            player1++;
        }else if (computer == 2 && choice == "paper"){
            player1++;
        }else if (computer == 2 && choice == "rock"){
            comp++;
        }else if (computer == 2 && choice == "scissor"){
            continue;
        }else {
            printf("Invalid Entry.");
        }
        printf("\n");
    }

    if (player1 > comp){
        printf("%s wins.", player);
    }else if (player1 == comp) {
        printf("%s and computer draws.", player);
    }else{
        printf("%s loses.", player);
    }
    
    return 0;
}

The program works but I am not getting the expecteed result in all 3 runs of the for loop I get only invalid entry as output due to which I can't count the score and the result as both player and comp variable are idle due to this.

output:

Enter your name:
Sam
Sam: rock
computer: scissor
Invalid Entry.   
Sam: scissor
computer: scissor
Invalid Entry.   
Sam: paper
computer: paper
Invalid Entry.
Sam and computer draws.

I have checked to best my skill and could not find any mistake I don't know why if else-if is acting weirdly, Help me to get correct output and explain me what the problem and where the mistake is.




Aucun commentaire:

Enregistrer un commentaire