简体   繁体   中英

Analysing two arrays for mastermind game for newbies

I know there's a lot of questions for the mastermind game and I still didn't found the help I need. 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.

I've been stuck for weeks now, and kinda desperate.

Any advice??

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM