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.