HW: 1A2B

  1. Write a program which will play the 1A2B game (also known as Bulls and Cows) with the user.
  2. 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.
  3. The user will input 4 digits. The computer will compare them with the secret digits and give the number of matches.
  4. If the matching digits are in their right positions, they are counted as "A"; if in different positions, they are counted as "B". Example:
  5. All the previous guesses and results will be displayed, so that the user can check easily.
  6. When the user correctly guesses the secret digits (i.e., he/she gets "4A"), the program ends.
  7. The program prints out how many guesses the user have made, and how long he/she spends.
  8. 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".
  9. You may play this example to get a feeling on how it may work.