Bulls and Cows against the computer

Bulls and Cows online: a code-breaking duel against the computer. Guess its secret number before it guesses yours. Free, no sign-up, in the browser.

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

How to play better

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.

Where to go from here