Number Baseball

The computer has picked a secret number made of different digits. Using only Strike and Ball hints, how many guesses will it take you to crack it?

Attempts: 0

What this game asks of you

The computer secretly chooses several different digits from 0 to 9 and arranges them in a row. You type a guess of the same length, and the only feedback you get is a count of Strikes and Balls. There is no partial credit and no hint about which position is wrong, so every guess has to be read like a small logic puzzle. In essence this is a game of elimination: out of thousands of possible codes, each hint removes the ones that could not have produced it. You can win by luck on the first try, but players who pay attention to how much each guess shrinks the field finish in noticeably fewer attempts at every difficulty. Internationally the same family of game is usually called Bulls and Cows (a bull is a right digit in the right place, a cow is a right digit in the wrong place), and the pen-and-paper version has been played for a long time.

How to read a hint

In this tool both the secret code and every guess must use different digits, and any digit from 0 to 9 may come first, so 0123 is a legal code. A guess with repeated digits, a wrong length, or anything other than the plain digits 0-9 is rejected with a message and does not count as an attempt. The attempt counter only moves for valid guesses, and the history list shows the newest guess at the top.

How many codes are there?

Counting ordered arrangements of n different digits gives 10 x 9 x 8 x ... for n factors. The number of distinct hints a guess can return is the number of (Strike, Ball) pairs with S + B no larger than n, minus the single pair that can never occur: n-1 strikes with exactly one ball. The table below is computed when the page is built, not typed in by hand.

DigitsPossible secret codesDifferent hints a guess can return
3 digits7209
4 digits5,04014
5 digits30,24020

One guess can never carry more information than the number of possible replies. With 4 digits that means 5,040 codes can be split into at most 14 groups per guess. Three guesses can distinguish at most 14 x 14 x 14 = 2,744 reply patterns, which is fewer than 5,040, so no strategy can guarantee a win in three guesses on 4 digits. Cracking the code in one or two guesses is therefore a matter of luck, not skill.

What a single first guess does

Suppose your opening guess on 4 digits is 0123. The table shows, for every one of the 5,040 possible secrets, which hint it would produce (computed during the page build by trying all of them).

Hint receivedCodes leftShare
0S1B1,44028.6%
0S2B1,26025.0%
1S1B72014.3%
1S0B4809.5%
Out (0S0B)3607.1%
0S3B2645.2%
1S2B2164.3%
2S0B1803.6%
2S1B721.4%
3S0B240.5%
0S4B90.2%
1S3B80.2%
2S2B60.1%
4S0B10.0%

The most common reply, 0 strikes and 1 ball, covers more than a quarter of all codes, and a lucky 4-strike reply is exactly one code out of 5,040. A hint with a large number of leftover codes is an unlucky result because it removes little; a rare hint such as 1S3B or 2S2B removes almost everything. Looking at the distribution also shows why the hint 3S1B is missing from the table: it cannot happen.

A practical strategy

  1. Spend the first two or three guesses finding which digits are in the code. Use guesses that share few digits and add up the S + B totals. For example, if 0123 returns a total of 2 and 4567 returns a total of 1, then 8 and 9 together account for the last one.
  2. From then on, only guess codes that are consistent with every hint so far. Such a guess might be the answer, and if it is not, it still removes candidates. The checkbox above counts those codes for you, so you can see how well each guess cut down the field.
  3. Move digits between positions to find their places. Compare the replies to two guesses that differ only by swapping two digits: whatever changes in the Strike count tells you something about exactly those two positions.
  4. Treat Out as a gift. Strike out all those digits and, in your head, shrink the pool you are drawing from.
  5. Do not repeat information. A guess that only re-tests something you already know uses up an attempt without shrinking the candidate list.

How many guesses does a simple method need?

To get a benchmark, a very plain strategy was run on every possible secret: always guess the smallest code (in numeric order) that is still consistent with all earlier replies. For 3 and 4 digits every secret was tested; for 5 digits the run used every tenth code (3,024 of 30,240) to keep the computation reasonable.

DigitsAverage guessesWorst case seen
3 digitsabout 5.279
4 digitsabout 5.569
5 digits (sample)about 6.019

These are measurements of that one simple method, not a statement about the best possible play. A cleverer choice of guesses, for instance one that splits the candidates more evenly, can do better, and the exact optimum is not given here because it was not checked for this page.

Worked examples

Example 1: reading hints. With the secret 1234, the guess 1243 earns 2 strikes (the 1 and the 2 are in place) and 2 balls (the 4 and the 3 are present but swapped). The guess 4321 contains all four digits but none in place, so it returns 0 strikes and 4 balls. The guess 5678 shares nothing and returns Out.

Example 2: a four-digit solve with the secret 8051. Always guessing the smallest consistent code needs seven attempts here.

AttemptGuessHintCandidates before the guess
101230S2B5040
210451S2B1260
312541S1B72
414060S2B25
550341S1B4
670513S0B3
780514S0B2

The very first reply, 0S2B, cuts 5,040 codes to 1,260, and after the third guess only a few dozen are left. The last stretch is slow because the final candidates differ only in one position.

Example 3: a three-digit solve with the secret 729. The same method goes 012 (0S1B), 134 (Out), 256 (0S1B), 507 (0S1B), 680 (Out), then 729, six attempts. The candidate count falls 720, 252, 80, 24, 6, 1. The two Out replies did most of the work because each one removed three digits from consideration at once.

Common mistakes and limits

Using what you learn

After a game, read the history from the bottom (first guess) to the top and ask, for each line, how many candidates it really removed. A guess that left the count almost unchanged probably repeated information you already had. When you can finish 3-digit games in about five attempts without the counter, move to 4 digits, then try 5. The same habit of choosing the guess that tells you the most is useful in any deduction puzzle.

Frequently Asked Questions

Can I enter the same digit twice?

No. The code has all-different digits, so a guess with a repeated digit is rejected with a message and does not use up an attempt.

Can the first digit be 0?

Yes. The code is drawn by shuffling the digits 0-9 and taking the first few, so 0123 is a valid code. That is why there are 10 x 9 x 8 = 720 three-digit codes rather than 648.

Why can I never get 3 strikes and 1 ball on 4 digits?

If three digits are exactly right, the fourth position of the code is the only place left. Your fourth digit is either that digit (and then it is a strike) or it is not in the code at all. There is no spare position for a ball, so that hint is impossible.

Why is it called baseball?

The names come from baseball scoring: a correct digit in the correct place is a strike and a correct digit in the wrong place is a ball. In English-speaking circles the same kind of game is better known as Bulls and Cows, and house rules and the exact history vary by source, so no single origin story is claimed here.

How many guesses is a good result?

The simple method measured above averages about 5.6 attempts on 4 digits, which makes a reasonable benchmark. Single games vary a lot with luck and with the first guess, so judge yourself over several games instead of one.

More deduction and board games

If you like this kind of puzzle, try Hangman or the Word Guess Gamefor letters instead of digits, Gomoku and Tic-Tac-Toe for head-to-head strategy, or Sudoku for a solo logic grid.