Project Description

Bulls and Cows is an old English game whose object is guessing a secret number using a minimum number of attempts. The digits of this secret number have to be all different, thus 1234 is a legal secret number but 1123 is not legal and should not be used in this game.

The game involves two players: one chooses the secret number and the other one tries to guess the secret number. After each guess, the opponent player informs the player about:

  • the number of digits which were correctly guessed but in the wrong place (cows),
  • the number of digits both correctly guessed and in the right place. (bulls) For example, if the secret number is 1294 and the guess is 4239, then the program scores this as 2 cows and 1 bull.

Your task is writing a C program to simulate the described game. The computer will play the role of the player who chooses the secret number and the user will play the game by attempting to guess.

What will be the digit count of the secret number?

The digit count of the secret number will be indicated by the user. Therefore, if you are using a string to store the secret number -this is a suggestion-, it should be allocated dynamically according to the user-defined digit count.

How will the program generate the secret number with all different digits?

This is a good sub-problem and various kinds of algorithms can be designed for that purpose. Actually, the algorithm is up to you but it can be pointed out that you should use the random number generator by recalling rand() and srand() functions, as you can also predict.

How will the program direct the user after each guess is evaluated?

As in the description of the game, the program will score each guess by counting cows and bulls.

Under which conditions the user will lose the game?

Either the maximum number of attempts is exceeded, or the user gives up by typing "quit"

How should the program behave, when a malformed guess is encountered?

Any guess that are malformed should be rejected and a new guess is requested from user by the program. Here is the word "malformed" implies only the following two conditions:

  • if the guess digit count is either less or more than the digit count of the secret number,
  • if the guess contains non-numbers (non-digits)

In what condition the program will be terminated?

After the user wins or lose, the program should ask her that whether she want to continue with a new game or exit. If she types "yes", your program will let her to play a new game by repeating the same steps: getting the digit count from user, generating a new secret number, evaluating each guess comes from the user...etc. If the user types no, the program will be terminated.

***We want your program to record the information about each game (let's say "hand") played by user, to a history.txt file. For this purpose, define a structure to represent the hands. A hand record should contain the following members:

  • index : It is an integer to store the which hand it is (sequence number of the hand)
  • secret: Secret number generated for the subjected hand
  • guess_list: An array to store each valid (not malformed) guess made by user (each attempt)
  • attempt_count: The number of attempts till the game ends.
  • result: It is set as 1 if the user won, 0 otherwise.

Program Specifications

  • You should choose meaningful variable, function names.
  • Put comments to the appropriate parts of your code.
  • Do not use inadvisable global variables.
  • Implement functions other than main() function such as a function that generates secret number, another function that validates guess, another one that evaluates guess and returns cows and bulls counts...etc.
  • This project also requires you to use:
    • at least 1 dynamically allocated structure, string OR array,
    • at least 2 source and 1 header files. (as in the Chapter 15 - Writing Large Programs)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.