What this game is
Bulls and Cows is a race between two code makers. Each side hides four different digits, and both are trying to break the other code before their own is broken.
There are exactly 5040 such codes. That sounds like a lot, but a well-chosen question cuts most of them away at once: every answer of so many bulls and so many cows splits what is left into groups, and you get exactly one of them. A well-played game takes five or six guesses, a badly played one drags on twice as long.
Rules
- Each side hides a code of four different digits from 0 to 9. A leading zero is allowed, so 0421 is a legal code.
- You enter your own code first — by hand or with the Random button — and press Start. The computer has already picked its own.
- A guess is four different digits. The answer comes back at once: so many bulls and so many cows.
- A bull is a right digit in the right place. A cow is a right digit in the wrong place. A digit counts once: either as a bull or as a cow.
- Four bulls means the code is broken.
- Turns alternate: you first, then the computer. Your guesses collect in the left column and its guesses in the right one; your own code stays visible above the right column so you can check its answers.
- Whoever gets four bulls first wins. You move first, so with an equal number of guesses the win is yours.
- The card on the side counts the guesses made, yours and the computer's.
How to play better
- Do not agonise over the first guess. Before the first answer every guess is equivalent: renaming the digits maps any of them onto any other, so the opening can be thrown out without thought.
- Bulls plus cows tells you nothing about places and everything about contents: how many of your digits appear in the code at all. Until you know the contents, chase that sum rather than the bulls.
- Split the problem in two. First work out which four digits the code uses, then arrange them — the two halves barely interfere.
- An answer of zero bulls and zero cows is the most valuable of all: it strikes four digits out of ten in one go.
- Only guess what is still possible. A combination that contradicts any earlier answer is a wasted turn.
- Once you know the contents, do not shuffle at random. Pick the arrangement that the surviving candidates answer differently: a guess that splits them evenly is worth two random ones.
A bit of history
Bulls and Cows is a pencil-and-paper game, and it is considerably older than its best known descendant. People were playing it long before the mechanics arrived in a box.
The box appeared in 1970: Mastermind, devised by Mordecai Meirowitz, replaced the digits with coloured pegs and the bulls and cows with black and white ones. It sold in the millions and, along the way, turned into a problem mathematicians took seriously.
In the mid-1970s Donald Knuth published "The Computer as Master Mind" with an algorithm that cracks Mastermind in at most five guesses. The idea is simple and strong: at every turn examine all possible guesses, see how each one splits the surviving candidates into groups, and pick the guess whose largest group is smallest. In other words, prepare for the worst answer. That is exactly what the Expert level does here.
This version follows the applet that ran on this site from 2005 and keeps its defining twist: this is not one-way guessing but a duel. The computer breaks your code with the same turns you use to break its own, and the right to move first settles ties in your favour.
About the levels
The level decides exactly one thing: how cleverly the computer narrows the set of codes still consistent with every answer it has been given. All three know how to discard the inconsistent ones — the difference is what they do with the remainder.
Easy takes a random code from the consistent ones. It will never name a combination it already knows to be impossible, but it makes no attempt to choose among the possible ones.
Hard picks the guess that will on average leave the fewest candidates: for each one it sorts the remainder into answer groups and takes the guess whose sum of squared group sizes is smallest. It only looks at codes that could themselves be the answer, and if there are too many of them for its budget of a million pairs per turn it works from a random sample.
Expert plays Knuth's way: it scores a guess not by the average but by the worst case — the largest group — and takes the one whose worst case is smallest. Its budget is eight times larger, so it also examines probing combinations that can no longer be the code themselves but split the remainder better. On a tie it prefers a guess that might still be the code, on the chance of winning outright.
The first guess is random at all three levels, and that is not leniency: with no answers in hand there is genuinely nothing to choose between.