Hello everyone! I have a small problem in C.
My whole code is below: (It is the game known as rock-scissor-pencil-paper)
Code:
What I have tried:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
char* CPU_random_choice(char **cpu_array);
int player_input();
int check_hand_in_array(char **array , char *input);
int increase_score(int *score);
void result(int *for_CPU , int *for_player);
void win(char **array , char *player_hand , char *cpu_hand , int *CPU_score , int *player_score);
int main(void)
{
int CPU_score = 0;
int player_score = 0;
char *Hands[4] = {"Rock" , "Scissor" , "Pencil" , "Paper"};
result(&CPU_score , &player_score);
char *cpu_choice = CPU_random_choice(Hands);
printf("\nCPU hand: %s\n\n" , cpu_choice);
int player_hand = player_input();
printf("\nPlayer hand: %s\n" , Hands[player_hand - 1]);
win(Hands , Hands[player_hand - 1] , cpu_choice , &CPU_score , &player_score);
printf("\n\n");
result(&CPU_score , &player_score);
}
char* CPU_random_choice(char **cpu_array)
{
srand(time(NULL));
int i = rand() % 4;
return cpu_array[i];
}
int player_input()
{
printf("1: Rock\n2: Scissor\n3: Pencil\n4: Paper\n");
printf("Player give a hand: ");
int player_hand;
scanf("%d" , &player_hand);
do
{
if(player_hand < 0 && player_hand > 4)
{
printf("Invalid hand...\nTry again: ");
scanf("%d" , &player_hand);
}
}
while(player_hand < 0 && player_hand > 4);
return player_hand;
}
void result(int *for_CPU , int *for_player)
{
printf("PLAYER - CPU\n==============\n");
printf(" %d\t %d\n" , *for_CPU , *for_player);
}
int increase_score(int *score)
{
return (*score)++;
}
void win(char **array , char *player_hand , char *cpu_hand , int *CPU_score , int *player_score)
{
if((strcmp(player_hand , CPU_random_choice(array))) == 0)
{
printf("Draw!");
}
else if(strcmp(player_hand , "Rock") == 0 && (strcmp(CPU_random_choice(array) , "Scissor") || strcmp(CPU_random_choice(array) , "Pencil")) == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(CPU_random_choice(array) , "Rock") == 0 && (strcmp(player_hand , "Scissor") || strcmp(player_hand , "Pencil")) == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(player_hand , "Rock") == 0 && strcmp(CPU_random_choice(array) , "Paper") == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(CPU_random_choice(array) , "Rock") == 0 && strcmp(player_hand , "Paper") == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(player_hand , "Scissor") == 0 && strcmp(CPU_random_choice(array) , "Rock") == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(CPU_random_choice(array) , "Scissor") == 0 && strcmp(player_hand , "Rock") == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(player_hand , "Scissor") == 0 && (strcmp(CPU_random_choice(array) , "Pencil") || strcmp(CPU_random_choice(array) , "Paper")) == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(CPU_random_choice(array) , "Scissor") == 0 && (strcmp(player_hand , "Pencil") || strcmp(player_hand , "Paper")) == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(player_hand , "Pencil") == 0 && (strcmp(CPU_random_choice(array) , "Rock") || strcmp(CPU_random_choice(array) , "Scissor")) == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(CPU_random_choice(array) , "Pencil") == 0 && (strcmp(player_hand , "Rock") || strcmp(player_hand , "Scissor")) == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(player_hand , "Paper") == 0 && (strcmp(CPU_random_choice(array) , "Pencil") || strcmp(CPU_random_choice(array) , "Scissor")) == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(CPU_random_choice(array) , "Paper") == 0 && (strcmp(player_hand , "Pencil") || strcmp(player_hand , "Scissor")) == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(player_hand , "Paper") == 0 && strcmp(CPU_random_choice(array) , "Rock") == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(CPU_random_choice(array) , "Paper") == 0 && strcmp(player_hand , "Rock") == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
else if(strcmp(player_hand , "Pencil") == 0 && strcmp(CPU_random_choice(array) , "Paper") == 0)
{
printf("Player wins!");
increase_score(player_score);
}
else if(strcmp(CPU_random_choice(array) , "Pencil") == 0 && strcmp(player_hand , "Paper") == 0)
{
printf("CPU wins!");
increase_score(CPU_score);
}
}
I fixed a previous problem I had with another way,but now my problem is that program doesn't return the correct solution(winner) and doesn't increase the the score of the winner(sometimes it does ,sometimes increase the score of loser,sometimes does nothing).I do not know what is going on...
I need your help...
Thanks!!!