Cows and Bulls
This game has appeared in many different forms under many different names for more years than I care to remember. The computer chooses a random selection of letters, and the player takes a guess at what the list may be. The computer then reports the number of bulls (correct letters in correct places) and the number of cows (correct letters in incorrect places). The idea is to get the correct solution in as few attempts as possible.
In my version, you can choose how many distinct letters can be chosen, and how long the list should be. For example if the game is initiated by the command 4 COWBULL 5 a vector of five letters will be selected from A,B,C,D (i.e. there must be at least one double). On the other hand 5 COWBULL 4 will select four letters from A,B,C,D,E.
 M COWBULL N;BULLS;COWS;ENTRY;I;INDEX;SOURCE;TARG2;TARGET  ⍝ M IS NO.OF LETTERS ALLOWED, N IS NUMBER OF VALUES IN TARGET  SOURCE←M↑'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ⍝ Define available letters  TARGET←SOURCE[?N⍴M] ⍝ Generate target string  I←1 ⍝ Initialise attempt number  LOOP:'ATTEMPT ',⍕I ⍝ Start loop: prompt for entry  ENTRY←⍞ ⍝ Enter attempt  →((⍴ENTRY)≠⍴TARGET)/ERROR ⍝ Check length of entry  →(M<⌈SOURCE⍳ENTRY)/ERROR2 ⍝ Check validity of entry letters  TARG2←TARGET  BULLS←+/TARG2=ENTRY ⍝ Count number of bulls  →(BULLS=N)/END ⍝ Stop if entry is correct  INDEX←TARG2≠ENTRY  TARG2←INDEX/TARG2 ⍝ Remove bulls from target  ENTRY←INDEX/ENTRY ⍝ Remove bulls from entry  TARG2←+/SOURCE∘.=,TARG2 ⍝ Freq. of letters in TARG2  ENTRY←+/SOURCE∘.=,ENTRY ⍝ Freq. of letters in ENTRY  COWS←+/TARG2⌊ENTRY ⍝ Calculate no. of cows  (⍕BULLS),' BULLS, ',(⍕COWS),' COWS.' ⍝ Print no. of bulls and cows  I←I+1 ⍝ Increment attempt number  →LOOP ⍝ End of loop  ERROR:'WRONG NUMBER OF LETTERS' ⍝ Error: wrong length entry  →LOOP  ERROR2:'INVALID LETTER(S)' ⍝ Error: wrong letters entered  →LOOP  END:'CORRECT! YOU TOOK ',(⍕I),' ATTEMPTS.' ⍝ Successful completionError messages are produced (a) if the length of any attempt is not equal to the length of the target (specified by the right argument) and (b) if any letters are used which are not in the allowed list (specified by the left argument).
Let’s see how the game is played.
4 COWBULL 4 ATTEMPT 1 ABCD 0 BULLS, 3 COWS. ATTEMPT 2 BABC 2 BULLS, 0 COWS. ATTEMPT 3 DAAC 3 BULLS, 0 COWS. ATTEMPT 4 DADC CORRECT! YOU TOOK 4 ATTEMPTS.
Our choice of 4 COWBULL 4 produces a vector of four letters chosen from A,B,C,D. Our first attempt, ABCD, produced 0 bulls and 3 cows. In other words, three of the letters are correct, but all are in the wrong place. Clearly one letter must be dropped, and another must be doubled up to take its place.
Our second effort, BABC, is consistent with what we know so far. We have tried dropping D and doubling up on B. The result is very useful - 2 bulls and 0 cows. B was the wrong one to double; there must be at least one D.
The third attempt, DAAC, is consistent with the previous results, and produces 3 bulls and 0 cows. The D must now be in the right place, as must the first A and the C. The only possible solution is now DADC.
If you find this logic a bit trivial, and you have a few hours to spare, try 10 COWBULL 10 or even 26 COWBULL 26.
Finally, for all you programmers, I’ve done the easy bit. With my program you can play the game and the computer keeps score. Now it’s your turn - how about a program in which the computer decides the strategy?
APL Health Warning: the game COWBULL can be addictive!
(webpage generated: 5 December 2005, 18:45)