首页 > 解决方案 > Bulls and Cows - 将简单的代码变成函数

问题描述

我正在尝试编写公牛和奶牛游戏。该程序生成一个 4 位数字,用户需要猜测这些数字。如果用户猜到了一个数字和它的位置,那就是公牛。如果用户只猜到了这个数字,那么它就成功了。我需要通过 3 个文件将它发送给我们的老师:function.h、function.c 和 main.c

我不知道如何将代码分成 3 个不同的文件。每次我试图从一个动作中创建一个函数时,它都不像简单的代码那样工作。老师让我们为代码生成器、猜测的验证、公牛和命中写一个函数。我将不胜感激任何帮助。

简单的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    char code[5];
    char guess[5];
    int bulls, hits;
    int i, j;

    srand(time(NULL));

    for (i = 0; i < 4; i++) {
        code[i] = rand() % 9 + '0';
        for (j = 0; j < i; j++) {
            if (code[j] == code[i]) {
                i--;
            }
        }
    }

    for (i = 0; i < 4; i++) {
        printf("%c", code[i]);
    }
    putchar('\n');

    do {
        bulls = 0;
        hits = 0;
        
        scanf("%s", guess);

        for (i = 0; i < 4; i++) {
            for (j = i + 1; j < 4; j++) {
                if (guess[i] == guess[j]) {
                    printf("Invalid entry. The digits have to differ\n");
                    
                    break;
                }
            }
        }

        for (i = 0; i < 4; i++) {
            for (j = 0; j < 5; j++) {
                if (code[i] == guess[j]) {
                    if (i == j) {
                        bulls++;
                    } else {
                        hits++;
                    }
                }
            }
        }

        printf("%d bulls %d hits\n", bulls, hits);
    } while (bulls != 4);
    printf("you won");

    return 0;
}

函数.h:

//A function that gets an array of 5 chars and contains the random 4 digits:
void GenerateCode (char code[], int i, int j);
//A function that gets an array of 5 chars and checks if the guess is valid:
int validateGuess (char guess[], int i, int j);
//A function that counts the bulls (if the user guessed the number and its position):
int CountBull(char code[], int len1, char guess[],int len2);
//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);

标签: cfunction

解决方案


这里。我改进了一点。

function.h

#pragma once

char code[5];
char guess[5];
int bulls, hits;
int i, j;

//A function that gets an array of 5 chars and contains the random 4 digits:
void GenerateCode();

//A function that gets an array of 5 chars and checks if the guess is valid:
int ValidateGuess();

//A function that counts the bulls (if the user guessed the number and its position):
int CountBullsAndHits(char code[], char guess[]);

//A function that prints the number of hits and bulls:
void PrintScore();

function.c

#include "function.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void GenerateCode() {
    srand(time((void *)0));

    for (i = 0; i < 4; i++)
    {
        code[i] = rand() % 9 + '0';
        for (j = 0; j < i; j++)
        {
            if (code[j] == code[i])
            {
                i--;
            }
        }
    }

    printf("%s\n", code); // Remove this line if you want a true guessing game!
    printf("Guess the number! : ");
    ValidateGuess(guess);
    printf("You won!\n");
}

int ValidateGuess() {
    do {

    bulls = 0;
    hits = 0;

    scanf("%s", guess);

    for (i = 0; i < 4; i++)
    {
        for (j = i + 1; j < 4; j++)
        {
            if (guess[i] == guess[j])
            {
                printf("Invalid entry. The digits have to differ\n");

                break;
            }
        }
    }

    CountBullsAndHits(code, guess);
    PrintScore();
    } while (bulls != 4);
}

int CountBullsAndHits(char code[], char guess[]) {
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            if (code[i] == guess[j])
            {
                if (i == j)
                {
                    bulls++;
                }
                else {
                    hits++;
                }
            }
        }
    }
}

void PrintScore() {
    printf("%d bulls, %d hits\n", bulls, hits);
}

main.c

#include "function.h"

int main()
{
    GenerateCode();
    return 0;
}

推荐阅读