Analysing two arrays for mastermind game

150 Views Asked by At

My main default is the logic for the analysis. What is demanded. The processing algorithm in the analysis function is the main interest of the exercise.

I need to compare the player proposition called essai[] and the ref[]. The ref[] is a 4-digit number made by the random function. One number counted as "exactly in the right place" can't be counted as "right number in wrong place". Exemple: ref[] = {1, 2, 3, 4} and essai[] = {1, 5, 7, 3}. It will count as 1 number in "exactly right place" and 0 numbers in "wrong place". Cannot be counted more then once. Same for the other way. I started with the right place, if there is then I replace both numbers, in essai[] and ref[] with -1. I did the same for "right number in wrong place".

Here's my code so far:

void analyse(int ref[], int essai[]) {
int i, j, o, RefTmp[4], NbPos, NbChif;

for(i = 0; i < 4; i++){
  RefTmp[i] = ref[i];
}

for (j = 0; j < 4; j++) { // [1] = 5 : [1] = 2
    for (o = 0; o < 4; o++) {
        if (RefTmp[j] == essai[o]) {
            if (j == o) {
                ++NbPos;
                RefTmp[j] = essai[o] = -1;
                printf("One number in the right place\n");
                break;
            }
            else {
                ++NbChif;
                RefTmp[j] = essai[o] = -1;
                printf("One number in the wrong place\n");
                break;
            }
        }
    }
}
}

What I understand is that I need to make 2 for loops to compare both arrays, but when there's multiple "right numbers in wrong", (for example), the loop prints multiple times too. It must only print once. I made a RefTmp[] so that the real ref[] won't be destroyed in the process.

1

There are 1 best solutions below

5
On

You should find & "erase" all of the "right number right place" instances (which only needs a single loop) before even considering handling the "right number wrong place" instances.