首页 > 解决方案 > Bulls and cows - c语言​​ - 比较数组

问题描述

你在上一个问题上帮助了我很多,现在我又被困住了。

我正在尝试为“公牛和母牛”游戏编写一个程序。游戏说明:

该程序有一个秘密的 4 位数字。

用户必须猜测数字。

如果数字是:1235 而使用猜测:1592,

公牛的数量是 1,因为用户猜对了一个数字和它的位置,而奶牛的数量是 3,因为用户只猜了数字。

我必须使用讲师给我们的功能。

我正在尝试比较数组以查看其中一个函数是否有共同数字,但输出始终显示多头数为 0。

尝试了3天后,我绝望了,

感谢您的阅读和回答!

function.h 代码:

//A function that gets an array of 5 chars and contains the random 4 digits:
void GenerateCode (char code[], int size);
//A function that gets an array of 5 chars and checks if the guess is valid:
int validateGuess (char guess[], int size);
//A function that counts the bulls (if the user guessed the number and its position):
int CountBull (char code[], char guess[], int size);
//A function that counts the hits (if the user guessed  only the number, not the position):
int CountHits (char code[], char guess[], int size);
//A function that prints the number of hits and bulls:
void PrintScore (int bulls, int hits);

function.c 文件:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include "function.h"
////////////////////////////////////////////////////////////////////////////
void GenerateCode(char code[], int size)
//A function that generates 4 random numbers into the array
{
srand(time(0));
    for (size=0; size<4; size++)
        {
             code[size] = rand() % 10;
             printf("%d", code[size]);
             
        }
printf("\n");
getch();
return;
}
////////////////////////////////////////////////////////////////////////////////////////
//now i'm going to ask the user to choose his numbers
int validateGuess(char guess[], int size)
//A function that gets 4 chars from the user and checks if they're valid

{
    int again;
    again:
    printf("please enter a 4 digit integer (different digits)\n");
for (size=0;size<4; size++) {
    scanf("%s", &guess[size]);
        printf("Your guess is:\n");
for (size=0;size<4; size++) {
    printf("%c", guess[size]);
                   }
int temp;
for (size=0;size<4; size++) {
    if(guess[size]!=-1)
    { 
        for (temp=size+1; temp<5; temp++)
        {
            if(guess[size]==guess[temp])
            {
              printf("\nInvalid entry, the digits should be different. Try again: \n"); 
              goto again;
            }
        } 
    }
}
}
getch();
return 0;
}

////////////////////////////////////////////////////////////
/*int CountBull (char code[], char guess[], int size)
{

}
*/
int CountBull (char code[], char guess[], int size)

{
   int x=0, y=0, count=0;
   for (x=0; x<4; ++x)
{
    for (y=0; y<4; ++y)
    {
        if(code[x]==guess[y])
        {
            ++count;
        }
    }
}             printf("\nThe number of bulls is: %d\n", count);
    printf("hi");

    return 0;
    }

main.c 代码:

//including the libraries to help me use scanf, printf, time and my functions.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include "function.h"
/////////////////////////////////
int main()
{
/*//A function that generates a 4 digit number
{
char code[5] = {0};
    int sizee=0;
    GenerateCode(code ,sizee);
}
//A function that checks if the guess is valid
{
char guess[5]= {0};
int size=0;

printf("%d", validateGuess(guess ,size));
}
*/
/////////////////////////////////////////////
//A function that counts the bulls
{
    char code[5] = {0};
    int sizee=0;
    char guess[5]= {0};
int size=0;
GenerateCode(code ,sizee);
validateGuess(guess ,size);
CountBull(code, guess, size);
getch();
}
}

标签: arraysc

解决方案


这似乎是一个逻辑问题。算法应该是这样的:

  • 迭代用户的猜测。
  • 如果您检查的数字是公牛,请增加公牛计数器,然后继续查找。
  • 否则,如果它不是公牛,那就去检查它是否是牛。

像这样的东西:

for(int i=0; i<4; i++)
{
  if(code[i] == guess[i])
  {
    bulls++;
  }
  else
  {
    for(int j=0; j<4; j++)
    {
      if(code[i] == guess[j])
      {
        cows++;
        break; // stop looking if found
      }
    }
  }
}

推荐阅读