2

I have to make a Rock Paper Scissors game, and I'm having trouble trying to how to take user input from player()and randomly generated value from computer()to game(), which is supposed to determine the return value. (Might have a same problem with game() --> outcome()) Also, I'm not allowed to take user inputs in main().

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

char computer(char);
char player(char);
int game(char, char);
void outcome(char, char);

int main()
{
   char p, c;
   computer(c);
   player(p);
   game(p, c);
   outcome(p, c);
}

char computer(char comp_choice) {
    
    srand(time(0));
    int n = (rand() % 3) + 1;
    
    if(n == 1) {
        comp_choice = 'R';
    }
    else if(n == 2) {
        comp_choice = 'P';
    }
    else {
        comp_choice = 'S';
    }
    
    return comp_choice;
}

char player(char player_choice) {
    
    printf("Enter(R)ock/(P)aper/(S)cissors:");
    scanf("%c", &player_choice);
    
    return player_choice;
}

int game(char player_choice, char comp_choice)
{
    
    if(player_choice == 'R' && computer_choice == 'S') {
        return -1;
    }
    else if(player_choice == 'S' && comp_choice == 'P') {
        return -1;
    }
    else if(player_choice == 'P' && comp_choice == 'R') {
        return -1;
    }
    else if(player_choice == 'S' && comp_choice == 'R') {
        return 0;
    }
    else if(player_choice == 'P' && comp_choice == 'S') {
        return 0;
    }
    else if(player_choice == 'R' && comp_choice == 'P') {
        return 0;
    }
    else if(player_choice == comp_choice) {
        return 1;
    }
    else {
        return 2;
    }
}  

void outcome(char p, char c) {
    
    int result = game(p, c);
    
    if(result == -1) {
        printf("You Win");
    }
    else if(result == 0) {
        printf("You lose");
    }
    else if(result == 1) {
        printf("It's a tie");
    }
}



I tried to do if (player(player_choice) == 'R' && computer(comp_choice) == 'S' but I think that runs player() more than once. What can I do to properly pass the values to game()? Maybe using pointers is the answer but I do not understand them well enough yet.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Alee
  • 23
  • 3
  • 1
    Side note: I suggest that you move the function call to `srand` to the start of `main`, because if you later change your program to call `computer` more than once (for example if you want to play multiple games per program executation), then you will be also calling `srand` multiple times, so that the same sequence of random numbers will be generated. You may want to read this: [srand() — why call it only once?](https://stackoverflow.com/q/7343833/12149471) – Andreas Wenzel Apr 11 '23 at 04:40

2 Answers2

0

I think you have confused the concept of functions and parameters. For example you have a function "char Player()" which has to return a char value and has zero parameters. Into that function you will ask for user input then return it. So in another function you will have a variable equal to that function like this

int main() {
    char choice = player();
}

char player() {
    char choice;
    printf("Enter(R)ock/(P)aper/(S)cissors:");
    scanf("%c", &choice);
    
    return choice;
}

Moreover if you want to have a function calculating the result you can use parameters

int main() {
    char result = result('P', 'S'); //Result will be equal to computer
}

char result(char player, char comp) {
    if(player == 'S' && comp == 'P'){
        return 'W'
    }else if(player == 'P' && comp == 'S'){
        return 'L'
    }//...More conditions to be added

    return 'E';
}
Stelian
  • 11
  • 2
  • Were you referring to the double quotes this whole time? – Stelian Apr 11 '23 at 06:41
  • Yes. Comments removed now that this _almost_ makes sense... `'Error'` is not a single character, although that's what your `result()` function claims to return... The other returns still try to return strings... – Fe2O3 Apr 11 '23 at 09:35
  • Either write pseudo-code or write correct C code. It is confusing to have C code with beginners mistakes. – nielsen Apr 11 '23 at 10:35
0

First the code below has an error computer_choice is not declared and should turn to comp_choice

if(player_choice == 'R' && computer_choice == 'S') {
        return -1;
    }

Second, every variable used in the code below is not initialized:

int main()
{
   char p, c;
   computer(c);
   player(p);
   game(p, c);
   outcome(p, c);
}

Third a lot of your functions do not need to have parameters like: computer ..

Finaly i changed your code to:

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

char computer();
char player();
int game(char, char);
void outcome(int);

int main()
{
   outcome(game(computer(), player()));
}

char computer(void) {

    char comp_choice;

    srand(time(0));
    int n = (rand() % 3) + 1;

    if(n == 1) {
        comp_choice = 'R';
    }
    else if(n == 2) {
        comp_choice = 'P';
    }
    else {
        comp_choice = 'S';
    }

    return comp_choice;
}

char player(void) {

    char player_choice;
    printf("Enter(R)ock/(P)aper/(S)cissors:");
    scanf("%c", &player_choice);

    return player_choice;
}

int game(char player_choice, char comp_choice)
{

    if(player_choice == 'R' && comp_choice == 'S') {
        return -1;
    }
    else if(player_choice == 'S' && comp_choice == 'P') {
        return -1;
    }
    else if(player_choice == 'P' && comp_choice == 'R') {
        return -1;
    }
    else if(player_choice == 'S' && comp_choice == 'R') {
        return 0;
    }
    else if(player_choice == 'P' && comp_choice == 'S') {
        return 0;
    }
    else if(player_choice == 'R' && comp_choice == 'P') {
        return 0;
    }
    else if(player_choice == comp_choice) {
        return 1;
    }
    else {
        return 2;
    }
}

void outcome(int result) {
    if(result == -1) {
        printf("You Win");
    }
    else if(result == 0) {
        printf("You lose");
    }
    else if(result == 1) {
        printf("It's a tie");
    }
}

Hope that helps

Learning
  • 72
  • 8
  • Funny that the "computer" and "player" are 'swapped' between the function call and its definition... Losing much? (Of course, we never know WHAT the computer chose, do we?) – Fe2O3 Apr 11 '23 at 04:26