A browser game where the computer guesses your secret Mastermind code. Plain HTML and JavaScript, no build step, no dependencies. Works on desktop and mobile.
- Open
index.htmlin a browser (or serve the folder with any static server). - Think of a secret code: 4 digits, each from 1 to 6, repeats allowed (1296 possible codes).
- The computer shows a guess. Tell it how many bulls (right digit, right place) and cows (right digit, wrong place) it scored.
- Repeat until it answers with 4 bulls. It never needs more than 5 guesses.
Enter the score by tapping the number chips under each field, or type both numbers and press Enter.
The game checks every answer before using it:
- Values outside 0–4, or bulls + cows above 4, are rejected.
- 3 bulls and 1 cow is rejected as logically impossible.
- An answer that no code could produce, given your earlier answers, is rejected with an explanation. Use Undo to step back and correct a previous answer.
The guesser uses Knuth's five-guess algorithm (1977):
- Start with all 1296 codes as candidates.
- For every possible guess, partition the candidates by the feedback each would return. The largest partition is that guess's worst case.
- Play the guess with the smallest worst case (minimax). Ties prefer a guess that is itself still a candidate, then the lexicographically smallest.
- Eliminate every candidate inconsistent with the real feedback and repeat.
Across all 1296 secrets this solves every code in at most 5 guesses, 4.476
on average. The implementation is a JavaScript port of the Zig Game struct
from a companion multi-language solver (Python, V and Zig) and produces the
same guess sequence as those versions.
| File | Purpose |
|---|---|
index.html |
Markup and styles, including the mobile layout and dark mode |
script.js |
Knuth solver (Game class) and the UI logic |
MIT, see LICENSE.