This if (strcmp(strlwr(p1Output), symbols[0]) != 0 || strcmp(strlwr(p1Output), symbols[1]) != 0 || strcmp(strlwr(p1Output), symbols[2]) != 0) If statementis executing even if I enter rock, paper and scissor.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
char *p1Output, *p2Output;
char *p1Name = "Player 1", *p2Name = "Player 2";
char *symbols[] = {"rock", "paper", "scissor"};
unsigned int lenSymbArr = sizeof(symbols) / sizeof(symbols[0]);
unsigned int maxLen = 0, nRand = 0;
unsigned int p1 = 0, p2 = 0; //initial value is 0
char ask;
int flag = 1;
for (int i = 0; i < lenSymbArr; i++) { //Max length string in array name symbols
if (maxLen < strlen(symbols[i]))
maxLen = strlen(symbols[i]);
}
maxLen += 2;
srand(time(NULL));
while (flag > 0) {
for (int i = 0; i < 3; i++) {
p1Output = (char *) malloc(maxLen * sizeof(char));
printf("%s turn: ", p1Name);
fgets(p1Output, maxLen, stdin);
p1Output[strcspn(p1Output, "\n")] = 0;
if (strcmp(strlwr(p1Output), symbols[0]) != 0 || strcmp(strlwr(p1Output), symbols[1]) != 0 || strcmp(strlwr(p1Output), symbols[2]) != 0) {
while (strcmp(strlwr(p1Output), symbols[0]) != 0 || strcmp(strlwr(p1Output), symbols[1]) != 0 || strcmp(strlwr(p1Output), symbols[2]) != 0) {
free(p1Output);
p1Output = (char *) malloc(maxLen * sizeof(char));
printf("You have entered wrong input, please enter from (%s, %s and %s) only.\n", symbols[0], symbols[1], symbols[2]);
printf("%s turn: ", p1Name);
fgets(p1Output, maxLen, stdin);
p1Output[strcspn(p1Output, "\n")] = 0;
if (strlen(p1Output) == 8)
p1Output[strlen(p1Output) - 1] = '\n';
}
}
else {
if (strlen(p1Output) == 8)
p1Output[strlen(p1Output) - 1] = '\n';
p1Output[strcspn(p1Output, "\n")] = 0;
nRand = rand() % lenSymbArr; //This will generate random choice for CPU
p2Output = symbols[nRand];
printf("%s turn: %s\n", p2Name, p2Output);
}
//p1 p2 win condition
if (strcmp(strlwr(p1Output), symbols[0]) == 0 && strcmp(p2Output, symbols[1]) == 0) {
printf("\n%s Wins!\n\n", p2Name);
free(p1Output);
p2++;
}
else if (strcmp(strlwr(p1Output), symbols[0]) == 0 && strcmp(p2Output, symbols[2]) == 0) {
printf("\n%s Wins!\n\n", p1Name);
free(p1Output);
p1++;
}
else if (strcmp(strlwr(p1Output), symbols[1]) == 0 && strcmp(p2Output, symbols[0]) == 0) {
printf("\n%s Wins!\n\n", p1Name);
free(p1Output);
p1++;
}
else if (strcmp(strlwr(p1Output), symbols[1]) == 0 && strcmp(p2Output, symbols[2]) == 0) {
printf("\n%s Wins!\n\n", p2Name);
free(p1Output);
p2++;
}
else if (strcmp(strlwr(p1Output), symbols[2]) == 0 && strcmp(p2Output, symbols[0]) == 0) {
printf("\n%s Wins!\n\n", p2Name);
// free(p1Output);
p2++;
}
else if (strcmp(strlwr(p1Output), symbols[2]) == 0 && strcmp(p2Output, symbols[1]) == 0) {
printf("\n%s Wins!\n\n", p1Name);
free(p1Output);
p1++;
}
else {
printf("\nDraw!\n\n");
free(p1Output);
}
}
if (p1 > p2)
printf("%s Wins!!! with %d point\n", p1Name, p1);
else if (p2 > p1)
printf("%s Wins!!! with %d point\n", p2Name, p2);
else
printf("Match Draw!\n");
printf("Do you want to play again? Y/N for Yes or No: ");
scanf("%c", &ask);
getchar();
while (ask != 'Y' || ask != 'y' || ask != 'N' || ask != 'n') {
if (ask == 'N' || ask == 'n') {
flag = 0;
break;
}
else if (ask == 'Y' || ask == 'y')
break;
else {
printf("Please enter choice from Y or N only: ");
scanf("%c", &ask);
getchar();
}
}
}
return 0;
}