Task 6 - Cows and Bulls

If you are stuck on how to do this task we have made a separate guidance document available here.

Cows and Bulls is a guessing game where the player tries to guess a number that has been randomly generated. The randomly generated number must contain exactly four digits between 0 and 9 with no duplicates. An example of a valid randomly-generated number is 1234. An example of an invalid randomly-generated number is 1223 as the digit 2 has been used twice in the four-digit number. 

The player is asked to enter a four-digit number. This is compared to the randomly-generated four-digit number. Each individual digit entered by the player is compared to each digit within the randomly generated number. If a digit is in the randomly-generated number and is in the same position in the randomly-generated number as it was in the player’s number then the digit is a “bull”. If the digit is in the randomly-generated number but is in a different position then the digit is a “cow”. 

The player has won the game when they have correctly guessed the four digit randomly-generated number. A record is kept of the number of guesses it took to guess the correct number.

Example

Randomly generated four digit number: 1857 
Player entered four digit number: 8647 
Response: One bull and one cow. 

The digit 8 is in 2nd position in the randomly-generated number and in the 1st position in the player’s number. This is a cow. The digit 7 is in the 4th position in the randomly-generated number and also in the 4th position in the player’s number. This is a bull. No other matches are found.

The program must work in the following way: 

  1. A randomly-generated four digit number is created. This number must have exactly four digits between 0 and 9. All four of the digits must be different to each other.
  2. The player enters either the word ‘exit’ or a four-digit number.
    1. If the player enters exit then the randomly generated four-digit number is shown and the program ends.
    2. If the player does not enter exit then the program must check that the value entered consists of exactly four digits. An appropriate message must be displayed if the value does not meet this requirement. 
    3. If the four-digit number entered contains repeated digits then an appropriate message must be displayed.
    4. Step 2 should be repeated until a valid input is received.
  3. The program should check the player’s number against the randomly-generated number.
    1. If a digit in the player’s number matches a digit in the randomly-generated number and is in the same position then one bull is counted.
    2. If a digit in the player’s number matches a digit in the randomly-generated number but is in a different position then one cow is counted.
    3. An appropriate message should be displayed giving the number of bulls and cows.

Tasks 2 and 3 are repeated until the randomly-generated four digit number has been correctly guessed by the player. If the guess is successful an appropriate “success” message is displayed. A count of the number of guesses the player has taken should be displayed to the player once the game is completed.