Write a program which will play the 1A2B game (also known as
Bulls and Cows) with the user.
When the program starts, it randomly chooses 4 different digits
among 0..9, and keeps them in secret.
It then prompts the user to guess.
The user will input 4 digits. The computer will compare them with
the secret digits and give the number of matches.
The minimum requirement of this homework is that, you may assume
the user always supply a valid input. That is, the user will not
input only 3 digits, or non-numeric characters.
However, you are encouraged to implement a function to check
whether the input is valid.
If the matching digits are in their right positions, they are
counted as "A";
if in different positions, they are counted as "B". Example:
Secret number: 4271
User's guess: 1234
Result: 1A2B
All the previous guesses and results will be displayed, so that the
user can check easily.
When the user correctly guesses the secret digits (i.e., he/she
gets "4A"), the program ends.
The program prints out how many guesses the user have made, and how
long he/she spends.
Hint: You may use the time()
function to get current Unix clock time.
Hint: You may design a function ordinal() to display the ordinal number.
You may test it with the following code:
for ($i=1; $i<30; $i++)
print ordinal($i) . "\n";
Please note it should show "12th" instead of "12nd".
You may play
this example to get a feeling on how it may work.