I was browsing through some of the problems at Rosetta Code earlier today and came across a fun game called Cows and Bulls. The premise of the game is straight-forward: 1) You make a guess using a 4 digit number with unique digits; 2) You're told the number of bulls (correct digits in the correct position) and the number of cows (correct digits in the wrong position); 3) Rinse and repeat until you guess the number (ie 4 bulls). I have the funny feeling this is similar to the Master Mind game that we were discussing in another thread, but I haven't verified it. The game sounded like fun and someone had already implemented it in Common Lisp, so I decided to give it a try... I've included some output below.
One of the best things about Rosetta Code is that you're bound to learn something new and interesting if you examine the some of the solutions. This includes languages in which you're already proficient. For example, the Java implementation of this game uses an Exception called InputMismatchException in the utils package. I've been programming in Java for ~8 years now and this may be the first time that I've seen someone use that Exception. Of course, knowledge of a programming language is only half the battle in computation -- memorizing a English->Spanish language dictionary doesn't make you fluent in Spanish, right?! The Java solution, for example, is quite a kludge compared to the CL implementation -- darn street programmers! =)
Anyways, I would encourage everyone to take a look at one or more of the implementations and POST something here if you see anything clever or kludgie or whatever. If the solution in your favorite language is missing (or badly written), I would also encourage you to implement it or fix it (but you may want to post your code here first to get some feedback). If you come across another one of the problems that you find interesting and would like to discuss, then by all means start a new thread.
For the more adventurous, there is actually a related task on Rosetta Code where you implement the Bulls and Cows player. I was thinking about implementing it in Common Lisp and Prolog (probably not this week though... or maybe even this month).
Code:
CL-USER> (play-game)
Guess a 4-digit number: 1234
Score: 1 cows, 1 bulls.
Guess a 4-digit number: 1243
Score: 0 cows, 2 bulls.
Guess a 4-digit number: 5643
Score: 0 cows, 1 bulls.
Guess a 4-digit number: 3241
Score: 1 cows, 1 bulls.
Guess a 4-digit number: 1342
Score: 2 cows, 0 bulls.
Guess a 4-digit number: 5246
Score: 0 cows, 1 bulls.
Guess a 4-digit number: 5236
Score: 1 cows, 1 bulls.
Guess a 4-digit number: 5263
Score: 0 cows, 2 bulls.
Guess a 4-digit number: 7283
Score: 0 cows, 3 bulls.
Guess a 4-digit number: 7293
Score: 1 cows, 2 bulls.
Guess a 4-digit number: 9283
Correct, you win!
NIL
CL-USER>
Yeah, I could have probably done a better job at 'guessing' the correct number.