>>3265
Your code is riddled with issues.
* Don't use pritnf unless you're actually using formatters. Use puts (which inserts a newline) or fputs (which does not insert a newline) to just output a string.
* Use switch statements. If you use cascading ifs, use else if for readability and so changes in logic still keep optimal performance. Switch statements always perform the same or better, and extend more easily.
// Turn this
if(match==TIE){
printf("it was a tie!");
}else if (match==WIN){
printf("you win!");
}else if (match==LOSE){
printf("you lose.");
}
// Into this
switch (match){
case TIE:
printf("it was a tie!");
break;
case WIN:
printf("you win!");
break;
case LOSE:
printf("you lose.");
break;
default:
printf("ERROR");
break;
}
* Recognize the patterns to simplify your algorithm. If you index R-P-S, recognize that (wrapping with a modulus) Player 1 wins if they have 1 index greater than Player 2, loses if they have 2 indexes greater, and tie if they have 0. The basic logic is (3 + first - second) % 3. You should also have some sort of error conditions to help catch regressions in your code.
Your compareRPSMatch function is better written as such:
RPS_match compareRPSMatch(RPS first, RPS second) {
switch ((3 + first - second) % 3)
{
case 0:
return (TIE);
break;
case 1:
return (WIN);
break;
case 2:
return (LOSE);
break;
default:
return (ERROR);
break;
}
}
* You usually don't want to call srand more than once in your entire program, especially with your time function, otherwise two calls to the function in the same second will always yield the same result.
* call time(NULL) unless you are planning on using the time variable you pass to it directly.
* recognize that enums are just integers, indexed at 0. Instead of that switch in getBotChoice, you can just do a `return (rand() % 3)`.
* tolower is already a C function in ctype.h. getchar is a function in stdio.h
* Your getChar function overflows like a mad bitch even in the best-case scenario. %s writes into a char* buffer including the null byte, so it always writes the byte after the location of your char x argument at the very least, and writes as many bytes past that argument as non-whitespace characters are delivered to it in the worst case. It's a formatter designed to push into a char buffer, not a single char variable (and a bad formatter at that, unless you use the m modifier). It's also nonsensical to use your char* real argument to take the address of x, then dereference that same address to return it, instead of just returning x (perhaps you were having issues caused by your scanf clobbering your data). If you put enough data into it, you're guaranteed to get a segfault.