## Assessment

### Problem: Cows and Bulls

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.

#### 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.

- If the player enters exit then the randomly generated four-digit number is shown and the program ends.
- 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.
- If the four-digit number entered contains repeated digits then an appropriate message must be displayed.
- 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.

- 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.
- 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.
- 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.

## Recent Comments

## Teacher Date: 2019-08-17

## Guest Sep 28, 2014, 10:15 PM