Hi I am trying to make a game where you have a secret code that is coded with colors like ROYG (red,orange,yellow,green) and I am having trouble when it tells you when you have a right color in the right spot or a right color in the wrong spot when you guess a color. How can I change my code under the function int comparearray where it will compare pointers to pointers and not integers and give me the correct number of "almost" and "correct".
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 100
#define COLS 4
void generate_code(char codearr[]);
char guess_array[ROWS][COLS];
int comparearray(char codearr[],char guess_array[ROWS][COLS], int check_location[]);
int check_location[];
int main()
{
char codearr[4];
int j;
int win = 0;
srand(time(NULL));
printf("This program has chosen a sequence of 4 colors for you to guess.\n");
printf("What's your curent guess?\n ");
generate_code(codearr);
printf("%s", codearr);
//while (answer == 0)
for (j= 1; j < 100; j++)
{
scanf("%s", &guess_array[j]);
comparearray(codearr, guess_array, check_location);
printf("%d. %s\n", j, guess_array[j]);
}
}
void generate_code(char codearr[])
{
int newrand;
int i=0;
srand(time(NULL));
for(i=0 ; i <4 ; i++)
{
newrand = rand()%6;
if (newrand == 0)
codearr[i]='R';
if (newrand == 1)
codearr[i]='O';
if (newrand == 2)
codearr[i]='Y';
if (newrand == 3)
codearr[i]='G';
if (newrand == 4)
codearr[i]='B';
if (newrand == 5)
codearr[i]='W';
}
}
int comparearray(char codearr[],char guess_array[ROWS][COLS], int check_location[])
{
int i=0, outer=0, t=0;
int right = 0, almost = 0;
for(i=0 ; i <4 ; i++)
{
if(codearr[i] == guess_array[i])
{
check_location[i] = 1;
right++;
}
}
for(outer=0 ; outer < 4 ; outer++)
for(i=0 ; i <4 ; i++) //almost
{
if(codearr[outer] == guess_array[i]&&check_location[outer]==0)
{
for(t = 0 ; t < 4 ; t++)
{
if (check_location[t] == 0)
{
for(i = 0 ; i < 4 ; i++)
{
if(codearr[t] == guess_array[i])
{
almost++;
break;
}
}
}
}
check_location[i] = 1;
almost++;
}
}
printf("Right %d , Almost %d\n", right, almost);
return(right);