Reputation: 29
I'm trying to write a bulls and cows game. The program generates a 4-digit number and the user needs to guess the digits. If the user guessed a number and its position, it's a Bull. If the user only guessed the number, its a hit. I need to send it to our teacher in 3 files: function.h, function.c and main.c
I can't figure out how to separate the code into 3 different files. Every time I'm trying to make a function out of an action it doesn't work like the simple code. The teacher asked us to write a function for the code generator, the validating of the guess, the Bulls and the hits. I would appreciate any help.
The simple code:
#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;
}
function.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);
Upvotes: 2
Views: 291
Reputation: 362
Here. I improved it a bit.
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;
}
Upvotes: 1