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
- Strike (S): a digit of your guess that is correct and sits in the correct position.
- Ball (B): a digit of your guess that appears in the secret code but in a different position.
- S + B: how many of the digits you typed belong to the code at all. This sum is useful on its own, because it tells you about the set of digits before you know anything about order.
- Out (0S 0B): none of your digits are in the code. That is one of the most informative results, because you can cross out every digit you just used.
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.
| Digits | Possible secret codes | Different hints a guess can return |
|---|---|---|
| 3 digits | 720 | 9 |
| 4 digits | 5,040 | 14 |
| 5 digits | 30,240 | 20 |
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 received | Codes left | Share |
|---|---|---|
| 0S1B | 1,440 | 28.6% |
| 0S2B | 1,260 | 25.0% |
| 1S1B | 720 | 14.3% |
| 1S0B | 480 | 9.5% |
| Out (0S0B) | 360 | 7.1% |
| 0S3B | 264 | 5.2% |
| 1S2B | 216 | 4.3% |
| 2S0B | 180 | 3.6% |
| 2S1B | 72 | 1.4% |
| 3S0B | 24 | 0.5% |
| 0S4B | 9 | 0.2% |
| 1S3B | 8 | 0.2% |
| 2S2B | 6 | 0.1% |
| 4S0B | 1 | 0.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
- 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.
- 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.
- 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.
- Treat Out as a gift. Strike out all those digits and, in your head, shrink the pool you are drawing from.
- 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.
| Digits | Average guesses | Worst case seen |
|---|---|---|
| 3 digits | about 5.27 | 9 |
| 4 digits | about 5.56 | 9 |
| 5 digits (sample) | about 6.01 | 9 |
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.
| Attempt | Guess | Hint | Candidates before the guess |
|---|---|---|---|
| 1 | 0123 | 0S2B | 5040 |
| 2 | 1045 | 1S2B | 1260 |
| 3 | 1254 | 1S1B | 72 |
| 4 | 1406 | 0S2B | 25 |
| 5 | 5034 | 1S1B | 4 |
| 6 | 7051 | 3S0B | 3 |
| 7 | 8051 | 4S0B | 2 |
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
- Reading S + B as a statement about positions. It is only a count of shared digits; positions come from comparing different arrangements of the same digits.
- Forgetting that a code may begin with 0, so 0 is a full member of the pool in every position.
- Typing full-width digits, which some input methods produce. The check accepts only the plain digits 0 to 9, and the box allows exactly as many characters as the selected length.
- Using the candidate counter for every game. It is a good way to learn, but a win with it switched on is not comparable to a win without it.
- The secret code comes from the browser's Math.random, which is fine for a game and not suitable for security or any kind of draw.
- Nothing is saved. Attempts and history disappear when you reload the page, and there is no syncing between devices.
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.