简体   繁体   English

解析两个arrays 新手主脑游戏

[英]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.分析 function 中的处理算法是练习的主要兴趣。 I need to compare the player proposition called "essai[]" and the "ref[]".我需要比较名为“essai[]”的玩家命题和“ref[]”。 The "ref[]" is a 4-digit number made by the random function. “ref[]”是由随机 function 生成的 4 位数字。 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}.示例:ref[] = {1, 2, 3, 4} 和 essai[] = {1, 5, 7, 3}。 It will count as 1 number in "exactly right place" and 0 numbers in "wrong place".它将在“完全正确的地方”计为 1 个数字,在“错误的地方”计为 0 个数字。 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.我从正确的地方开始,如果有的话,我用 -1 替换“essai[]”和“ref[]”中的两个数字。 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.我的理解是,我需要制作 2 个 for 循环来比较两个 arrays,但是当有多个“正确的数字错误”时,(例如),循环也会打印多次。 It must only print once.它只能打印一次。 I made a "RefTmp[]" so that the real "ref[]" won't be destroyed in the process.我做了一个“RefTmp[]”,这样真正的“ref[]”就不会在这个过程中被破坏。

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.在考虑处理“正确编号错误位置”实例之前,您应该找到并“擦除”所有“正确编号正确位置”实例(只需要一个循环)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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