-5

This code runs just fine. but there is some minor mistake in the program. I don't know what's the problem 'cause it's like I keep on winning the game and the computer just loses the game every time. 3

the game Rock, Paper, Scissors. My problem is if I execute the program, I just keep winning.I need the game to let the computer win. How can I rectify this?

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

int Random(int n) {
    srand(time(NULL));
    printf("%d\n", rand() % 3); }

int greater(char ch1, char ch2) {
    if (ch1 == ch2)
    {
        return -1;
    }
    else if ((ch1 == 'R') && (ch2 == 's'))
    {
        return 1;
    }
    else if ((ch2 == 'R') && (ch1 == 's'))
    {
        return 0;
    }
    else if ((ch1 == 's') && (ch2 == 'p'))
    {
        return 1;
    }
    else if ((ch2 == 's') && (ch1 == 'p'))
    {
        return 0;
    }
    else if ((ch1 == 'p') && (ch2 == 'r'))
    {
        return 1;
    }
    else if ((ch2 == 'p') && (ch1 == 'r'))
    {
        return 0;
    }
}

int main() {
    char playerchar, computerchar, a;
    int playerscore = 0, compscore = 0, b;
    char ch[] = {'R', 'P', 'S'};
    printf("\n");
    printf("~~~~~Welcome to the Rock, Paper, Scissors Game~~~~~\n\n");
    printf("Choose a number\n 1. for Rock\n 2. for Paper\n 3. for Scissiors\n");

    for (int i = 0; i < 3; i++)
    {
        printf("Player's Turn :\n");
        scanf("%d", &b);
        getchar();
        playerchar = ch[b - 1];
        printf("You chose %c\n", playerchar);

        printf("Computer's Turn :\n");
        b = Random(3) + 1;
        computerchar = ch[b - 1];
        printf("Computer chose %c\n", computerchar);

        if ((greater(computerchar, playerchar) == 1))
        {
            compscore += 1;
            printf("Computer got it!\n\n");
        }
        else if ((greater(computerchar, playerchar) == -1))
        {
            compscore += 1;
            playerscore += 1;
            printf("It's a draw\n");
        }
        else
        {
            playerscore += 1;
            printf("You got it!\n\n");
        }

        printf("You     : %d\nComputer: %d\n\n",playerscore,compscore);
    }          

    if (compscore > playerscore)
    {
        printf("Computer win the game!\n\n");
    }
    else if (playerscore > compscore)
    {
        printf("You win the game!\n\n");
    }
    else
    {
        printf("The game is draw\n");
    }

    return 0;}

2 Answers2

1

First of all, srand should be called just once at the beginning of the program. You call it on every call of Random().

But the bigger issue is that your function Random() does not return anything, it just prints the value. This should have grnerated a warning in your compiler. This probably means that Random always returns 0.

To fix the problem, do return rand() % 3; instead of printing it.

anestv
  • 543
  • 6
  • 28
1

There's really no point complex if statements for determining the winner. Just realize that rock paper scissors is basically just a directed cycle graph.

// Returns winner in rock paper scissors
// 0 - Rock, 1 - Paper, 2 - Scissors 
// Return values:
// 0  - draw
// 1  - a win, b loose
// -1 - b win, a loose
int rps(int a, int b) {
    if(a == b) return 0;
    return (a+1) % 3 == b ? -1 : 1;
}

Normal rps (rock, paper, scissors) has 3 nodes in the graph. This can actually easily be generalized to n nodes. The algorithm works like this. Start on node a. If it's the same as b, then it's a draw. Otherwise, check how many steps it is required to to go around the graph to get to b and determine the winner depending on if the number of steps is even or odd.

int rpsn(int a, int b, int n) {
    if(a == b) return 0;
    int steps = 0;
    while((a+steps) % n != b) steps++;
    return steps % 2 ? -1 : 1;
}

Read about Rock, Paper, Scissors, Lizard, Spock for a 5 node version.

I'm fairly confident that the loop could be exchanged for a single clever math operation, but I'm having a brain freeze at the moment. I'll update the post if I find it.

EDIT:

I found a way. Here is a very short rps function with no loops for arbitrary n:

// Returns the number of steps required to go from a to b 
int steps(int a, int b, int n) { 
    return (n+b-a) % n; 
}

int rpsn(int a, int b, int n) {
    if(a == b) return 0;
    return steps(a,b,n) % 2 ? -1 : 1;
}

The reason for doing (n+b-a) % n instead of (b-a) % n is that we need the left operand of % to be nonnegative.

klutt
  • 30,332
  • 17
  • 55
  • 95