Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Program Bulls and cows is a code-breaking game for two players, which may date back a century or more. The commercial adaptation is famously

C Program

Bulls and cows is a code-breaking game for two players, which may date back a century or more. The commercial adaptation is famously known under the name Mastermind.

In this game, one player (here played by the computer) chooses a secret number composed of 4 digits (from 0 to 9), all different from one another. The other player (here played by the user) has 10 tries to guess the secret number. Each time the user supplies a guess, the computer outputs the number of matching bulls and cows:

A bull (X) means that one of the supplied digits is in its right position.

A cow (O) means that one of the supplied digits exists in the secret, but in another position.

For example, if the secret number is 4293 and the supplied guess is 1234, then the computer would respond XOO-, meaning that there are one bull (the digit 2 is properly positioned), two cows (digits 3 and 4 exist in the secret number but at other positions), and one digit that doesnt exist in the secret number.

Write program moo.c which implements the bulls and cows game, as illustrated in the following example.

$ ./moo What is your guess? 1234 O--- What is your guess? 5678 XXO- What is your guess? 1249 ---- What is your guess? 3675 You guessed correctly in 4 tries! $ ./moo What is your guess? 1a2b3c4d O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- What is your guess? 1234 O--- You lose! $ 

Your code should follow certain constraint(s):

Just like with jojojeu.c, you should initialize the random number generator at the beginning of your program.

/* Initialize the random number generator */ srand(time(0)); 

Before uploading on Gradescope, change the line to srand(0) to make the random generator be deterministic.

Your program should not use any global variables, all variables should be passed to functions via arguments (including arrays).

For simplicity, the key and guess should be represented by arrays of digit characters (instead of numbers, or arrays of number digits).

When reading a guess from the user, each of the 4 character digits should be read separately with scanf(). If the user inputs an invalid character (that is a character not between 0 and 9), the invalid digit should be silently ignored.

Hint(s):

I suggest to write a function that prints what the secret key is, after it has been determined. It should surely help debug the rest of the program, until everything works correctly.

It should be helpful to write two helper functions:

One function that checks whether a certain digit character is located in an array at a certain index

One function that checks whether a certain digit character is included in an array (at any of the 4 positions). Note that this function could call the first one.

Leave comments to show how code works if possible

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started