Search code examples
cfunctiontimeenumssrand

rock paper scissors game in c. Can someone tell where my fault is?


The computer always gives the same input.

For example :example or example2

As in the example, I always choose the rock and the computer always chooses the paper. What should I do to make the computer give a different result?

Probably there is something wrong with the last function but I couldn't find it. Can someone help me please?

enum Move {ROCK = 1, PAPER, SCISSORS}input;

int input, computerInput = 0;
int rock = 1;
int paper = 2;
int scissors = 3;
int computerPoint = 0, userPoint = 0;

printf(" ###### Welcome to Rock-Paper-Scissors Game ###### \n");
printf(" ### Rules: \n ### Press 1 for Rock \n ### Press 2 for Paper \n ### Press 3 for Scissors \n ### First one to reach 3 points will win. \n");

printf("Input your move : ");
scanf("%d", &input);


while(input < 1 || input > 3)
{
printf("Your input must be 1, 2 or 3 \n");
printf("Input your move : ");
scanf("%d",&input);
}
    
while(input == computerInput)
{
        printf("It's draw. \n");
        printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
        printf("Input your move : ");
        scanf("%d",&input);
            
}


while(input != computerInput && userPoint < 3 && computerPoint < 3){
    
    switch (input)
    {
        
        case ROCK:
        if(computerInput == 2){
            printf("You picked Rock. Computer picked Paper. Computer got 1 point(s). \n");
            computerPoint++;            
        }
        else if(computerInput == 3){
            printf("You picked Rock. Computer picked Scissors. You got 1 point(s). \n");
            userPoint++;
        }
        break;
        case PAPER:
        if(computerInput == 1){
            printf("You picked Paper. Computer picked Rock. You got 1 point(s). \n");
            userPoint++;
        }else if(computerInput == 3){
            printf("You picked Paper. Computer picked Scissors. Computer got 1 point(s). \n");
            computerPoint++;
        }    
        break;
        case SCISSORS:
        if(computerInput == 1){
            printf("You picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
            computerPoint++;
        }else if(computerInput == 2){
            printf("You picked Scissors. Computer picked Paper. You got 1 point(s). \n");
            userPoint++;                
        }   
        break;
        return 0;
    }
   
   
   if(computerPoint == 3){
    printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
    printf("\n!!Computer won the game!!");
    return 0;
    }

    if(userPoint == 3){
    printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
    printf("\n!!You won the game!!");
    return 0;
    }
   
    printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
    printf("Input your move : ");
    scanf("%d",&input);
}
return 0;

enum Move getRandomMove(){
    int computerInput;
    srand(time(NULL));
    computerInput  = 1 + rand() %3;
    return computerInput;
}
  

Solution

  • After all the changes, this is the correct working and cleared version. Thank you very much to everyone who helped.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void){
    
    enum Move {ROCK = 1, PAPER, SCISSORS}input;
    srand(time(NULL));
    int computerInput;
    int computerPoint = 0, userPoint = 0;
    
    enum Move getRandomMove()
    {
    return (enum Move)(1 + rand() % SCISSORS);
    }
    
    
    printf(" ###### Welcome to Rock-Paper-Scissors Game ###### \n");
    printf(" ### Rules: \n ### Press 1 for Rock \n ### Press 2 for Paper \n ### Press 3 for Scissors \n ### First one to reach 3 points will win. \n");
    
    printf("Input your move : ");
    scanf("%d", &input);
    
    while (computerPoint < 3 && userPoint < 3)
    {
        while(input < 1 || input > 3)       
        {
            printf("Your input must be 1, 2 or 3 \n");
            printf("Input your move : \n\n");
            scanf("%d",&input);
        }
        
        
        computerInput = getRandomMove();
        
        if(input == computerInput)
        {  
            printf("\nIt's draw. \n");
            printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
            printf("Input your move : ");
            scanf("%d",&input);
            continue;
        }
        
        
        switch (input)
        {
            
            case ROCK:
            if(computerInput == PAPER){
                printf("\nYou picked Rock. Computer picked Paper. Computer got 1 point(s). \n");
                computerPoint++;            
            }
            else if(computerInput == SCISSORS){
                printf("\nYou picked Rock. Computer picked Scissors. You got 1 point(s). \n");
                userPoint++;
            }
            break;
            case PAPER:
            if(computerInput == ROCK){
                printf("\nYou picked Paper. Computer picked Rock. You got 1 point(s). \n");
                userPoint++;
            }else if(computerInput == SCISSORS){
                printf("\nYou picked Paper. Computer picked Scissors. Computer got 1 point(s). \n");
                computerPoint++;
            }    
            break;
            case SCISSORS:
            if(computerInput == ROCK){
                printf("\nYou picked Scissors. Computer picked Rock. Computer got 1 point(s). \n");
                computerPoint++;
            }else if(computerInput == PAPER){
                printf("\nYou picked Scissors. Computer picked Paper. You got 1 point(s). \n");
                userPoint++;                
            }   
            break;
        }
        
        
        if(computerPoint == 3){
        printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
        printf("\n!!Computer won the game!!");
        return 0;
        }
    
        if(userPoint == 3){
        printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
        printf("\n!!You won the game!!");
        return 0;
        }
       
        printf("Your Point is %d and Computer's point is %d \n",userPoint, computerPoint);
        printf("Input your move : ");
        scanf("%d",&input);
    }
    return 0;   
    }