הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Bulls And Cows Game

Bulls and Cows -- also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots -- is an old code-breaking paper and pencil game for two players, predating the similar commercially-marketed board game Master Mind.
It is a game with numbers that may date back a century or more. It is played by two opponents. The game can also be played with 3 digits instead of 4.
(More info on the game is available in its wikipedia page)

Gameplay and rules

the first player chooses a 4-digit secret number. The digits must be all different.
Each round, the other player tries to guess the secret number, and the game tells her how well she did by using two numbers: bulls and cows. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Example:

  • Secret number: 4271
  • Opponent's try: 1234
  • Answer: 1 bull and 2 cows. (The bull is "2", the cows are "4" and "1".)

If the secret was not revealed in 20 rounds - the game is lost.

Your task: Implement this game in perl.

Bonus

Implement an AI player that will compete against you, and win in no more than 6 rounds.