如何在c中的主脑猜谜游戏中正确统计“白色”?

时间:2010-01-05 11:34:52

标签: c arrays

“白色”是检查错误位置的正确数字。 但我不知道如何正确计算它。

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int answer[4];
    int guess[4];
    int count = 0;

    srand(time(NULL)); 
    /*answer[0] = (rand() % 6)+1;
    answer[1] = (rand() % 6)+1;
    answer[2] = (rand() % 6)+1;
    answer[3] = (rand() % 6)+1;*/

    answer[0] = 3;
    answer[1] = 3;
    answer[2] = 5;
    answer[3] = 2;

    printf("%d %d %d %d\n", answer[0],  answer[1],  answer[2],  answer[3]);
    printf("          B W\n"); 

    do
    {
        int black = 0;
        int white = 0;
        count++;
        printf("Enter 4 numbers, this is your %d guess: ", count);
        scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);
        printf("\n%d %d %d %d\n", guess[0], guess[1], guess[2], guess[3]);

        int g2[2][6];

        for (int a = 0;a < 4;a++)
            g2[0][a]=answer[a];

        for (int i = 0;i < 4;i++)
            g2[1][i]=guess[i];

        if (answer[0]==guess[0])
            black++;
        if (answer[1]==guess[1])
            black++;
        if (answer[2]==guess[2])
            black++;
        if (answer[3]==guess[3])
            black++;

        if (answer[1]==guess[0] || answer[2]==guess[0] || answer[3]==guess[0])
            white++;
        if (answer[0]==guess[1] || answer[2]==guess[1] || answer[3]==guess[1])
            white++;
        if (answer[0]==guess[2] || answer[1]==guess[2] || answer[3]==guess[2])
            white++;
        if (answer[0]==guess[3] || answer[1]==guess[3] || answer[2]==guess[3])
            white++;

        if (black==4)
            white=0;

        g2[1][4]=black;
        g2[1][5]=white;

        for (int n = 0;n < 6;n++)
            printf(" %d",g2[1][n]); 

        printf("\n");
    }
    while (answer[0]!=guess[0] || answer[1]!=guess[1] ||
           answer[2]!=guess[2] || answer[3]!=guess[3]);

    printf("BINGO!!!\n");

    return 0;
}

更新2:

for (int slot=0;slot<4;slot++)
{
    if (guess[slot] == answer[slot]) 
        black++;
    else 
        for (int s=0;s<4;s++)
            if (s != slot) 
            {
                if (guess[slot] == answer[s]) 
                    white++;
                break;
            }
}

更新3:

for (int x=0;x<4;x++)
flag[x]=0;

for (int slot = 0;slot < 4;slot++) 
{ 
    if (guess[slot] == answer[slot])  
        if (flag[slot]==1)
            black++; 
    else  
        for (int s=0;s < 4;s++) 
            if (s != slot)  
            { 
                if (guess[slot] == answer[s])
                    if (flag[s]==1)
                    {
                        white++; 
                        break; 
                    }
            } 
} 

更新4

for (int x=0;x<4;x++)
flag[x]=0;

for (int slot = 0;slot < 4;slot++) 
{ 
    if (guess[slot] == answer[slot])  {
        black++; 
        flag[slot]=1;
    }
else  
    for (int s=0;s < 4;s++)
        if (s != slot)
        { 
            if (guess[slot] == answer[s])  
            {
                white++; 
                flag[s]=1;
                break; 
            }
        } 
} 

更新5:

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int answer[4];
    int guess[4];
    int flag[4];
    int count = 0;

    srand(time(NULL)); 
    /*answer[0] = (rand() % 6)+1;
    answer[1] = (rand() % 6)+1;
    answer[2] = (rand() % 6)+1;
    answer[3] = (rand() % 6)+1;*/

    answer[0] = 1;
    answer[1] = 2;
    answer[2] = 3;
    answer[3] = 4;

    printf("%d %d %d %d\n", answer[0],  answer[1],  answer[2],  answer[3]); 

    do
    {
        int black = 0;
        int white = 0;
        count++;
        printf("Enter 4 numbers, this is your %d guess: ", count);
        scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);

        int g2[2][6];

        for (int a = 0;a < 4;a++)
            g2[0][a]=answer[a];

        for (int i = 0;i < 4;i++)
            g2[1][i]=guess[i];

        for (int x=0;x<4;x++)
            flag[x]=0;

        for (int slot = 0;slot < 4;slot++) 
        { 
            if (guess[slot] == answer[slot])  
                black++; 
            else   
                for (int s=0;s < 4;s++)  
                    if (s != slot && guess[slot] == answer[s] && !flag[s]) 
                    {  
                        white++;  
                        flag[s]=1; 
                        break;  
                    }
        } 

        g2[1][4]=black;
        g2[1][5]=white;

        printf("Guess %d: ", count);

        for (int n = 0;n < 4;n++){
            printf(" %d",g2[1][n]);
        }

        printf(" Black: %d White: %d\n", g2[1][4], g2[1][5]);
        printf("\n");
    }
    while (answer[0]!=guess[0] || answer[1]!=guess[1] ||
           answer[2]!=guess[2] || answer[3]!=guess[3]);

    printf("BINGO!!!\n");

    return 0;
}

4 个答案:

答案 0 :(得分:9)

Knuth在 The Computer as Master Mind 中回答这个问题,并承认很难完全定义它。他暗示:

  1. 通过将答案中的每个位置与当前猜测中的相同位置进行比较来计算黑色数量。
  2. 计算白人加黑人的数量,如下所示。
  3. 从(2)中减去(1)得到白人数。
  4. 计算白人加黑人:

    1. 制作两个数组ansguess,每种颜色都有一个插槽。
    2. 对于每种颜色,使用答案中该颜色的挂钩数填充ans。同样,使用当前猜测中该颜色的挂钩数填充guess
    3. 为每个min(ans[i], guess[i])添加i。这是白人加黑人。

答案 1 :(得分:1)

建议替代方案(伪代码):

white = 0;
black = 0;
for slot=0 to 3
  if guess[slot] == answer[slot]
    black++
  else
    for s=0 to 3
      if s != slot
        if guess[slot] == answer[s]
          white++
          break

更新(明细)

 else  
    for (int s=0;s < 4;s++) 
      if (s != slot && guess[slot] == answer[s] && !flag[s])
      { 
          white++; 
          flag[s]=1;
           break; 
      }

我认为应该这样做:我们正在检查另一个不是当前的插槽,一个不是完美(黑色)匹配的插槽,并且之前没有用过标记黑色或白色。

答案 2 :(得分:1)

试试这个:

    for (int x=0;x<4;x++)
        flag[x]=guess[x]==answer[x]?1:0;

    for (int slot = 0;slot < 4;slot++) 
    { 
        if (guess[slot] == answer[slot])  
            black++; 
        else   
            for (int s=0;s < 4;s++)  
                if (!flag[s] && guess[slot] == answer[s]) 
                {  
                    white++;  
                    flag[s]=1; 
                    break;  
                }
    } 

答案 3 :(得分:1)

该评论指出Knuth提供了一个4步算法来确定 在步骤4中,白色和黑色钉的数量不正确。

步骤4简单地返回4 - 步骤3中返回的数字。步骤4返回“未命中”的数量,即不是黑色或白色的挂钩。

只需使用第3步计算总共白人和背部的数量;使用简单的

计算黑钉的数量
for i = 0 to 3:
   if(a[i] == g[i]) b = b + 1 

当然白色=第3步 - 黑钉数量(b)。