Bulls and Cows

Two-player code-breaking guessing game (or single-player vs computer) — one player picks a secret 4-digit number with unique digits; the other guesses, receiving 'Bulls' (correct digit + position) and 'Cows' (correct digit, wrong position) feedback until deducing the secret code.

📐paper or digital interface~5 min🌤indoor

What is Bulls and Cows?

Bulls and Cows is a classic code-breaking guessing game predating computers. Origins uncertain — evidence of similar games in 19th-century England as pencil-and-paper amusement; possibly earlier in various forms globally. In modern digital form: coded 1970 for early mainframes as one of first computer games. Also basis of commercial 'Mastermind' game (Mordechai Meirowitz, Israel, 1970) but with colored pegs instead of digits. Two-player version: one player is 'code-maker' who picks secret 4-digit number using UNIQUE digits (no repeats, so 5040 possibilities from 0000-9999); other is 'code-breaker' who guesses. After each guess, code-maker reveals: BULLS = number of digits correct in RIGHT position; COWS = number of digits correct in WRONG position. Code-breaker uses successive guesses + logical deduction to narrow possibilities. Winner = fewest guesses to reach '4 Bulls' (correct). Single-player: play against computer or self-test. Deep mathematics — Donald Knuth's 1976 algorithm proves 5 guesses always sufficient for 4-digit Mastermind (with colored pegs, allowing repeats). Popular internationally: China + Taiwan + Japan especially (called 'Cows and Bulls' 猜数字 in Chinese). Simple to play; teaches deduction, probability, elimination. Educational tool for teaching logic in schools + universities. Base for many computer science 'first program' assignments in intro classes.

Quick facts

Category

Puzzle Games

Type

Individual

Players

1–2

Duration

~5 min

Where played

indoor

Field / court

paper or digital interface

Country of origin

🇬🇧 United Kingdom

Estimated origin

19th-century England pencil-and-paper form; digital 1970 for mainframes; commercial 'Mastermind' variant by Mordechai Meirowitz 1970 Israel

Olympic

No

Popularity

regional

How it works

Objective

Guess the code-maker's secret number in fewest attempts using Bulls/Cows feedback for deduction.

Match structure

Code-maker picks secret 4-digit number with unique digits. Code-breaker submits guesses (typically 4-digit numbers with unique digits). After each guess, code-maker reports: X bulls, Y cows. Game continues until '4 bulls' (correct guess) or turn limit (typically 10-12 guesses).

Rules

Basic rules

  • Code-maker picks secret 4-digit number

    All 4 digits must be UNIQUE (no repeats). E.g. 1234, 5087, 9036 valid; 1123, 4444 invalid.

  • Code-breaker submits guess (4-digit)

    Also 4 unique digits per guess. Each guess is a hypothesis about the secret.

  • Feedback: Bulls + Cows

    BULLS = digits correct AND in right position. COWS = digits correct BUT in wrong position. Report total per category (not which digits).

  • 4 Bulls = correct guess (win)

    When guess = secret, code-maker announces '4 Bulls' — code-breaker wins.

  • Deduce from feedback

    Each guess + feedback narrows possible secrets. Systematic elimination + probability leads to solution.

  • Fewest guesses = winning

    Two-player: switch roles + compare who solved in fewer guesses. Solo: personal best tracking.

  • No hints beyond bulls/cows

    Code-maker only reveals bull/cow totals — never WHICH digits are correct.

Advanced rules

  • Turn limit typically 10-12

    If code-breaker doesn't solve within limit, code-maker wins. Optimal players solve in 6-8 guesses typically.

  • Variants with repeat digits

    Some rules allow digits to repeat in secret (0000, 1123 valid) — 10000 possibilities from 0000-9999.

  • Variants with different digit counts

    3-digit (720 secrets) easier; 5-digit (30,240) harder; 6-digit (151,200) very hard.

  • Colored pegs (Mastermind commercial)

    6-8 colors, 4 positions, repeats allowed — different math. 1296 possibilities for 6 colors × 4 positions with repeats.

  • Knuth's 5-guess algorithm (Mastermind)

    Donald Knuth 1976 proved 5 guesses always sufficient for standard Mastermind (6 colors, 4 pegs, repeats).

  • Minimum-max information gain

    Optimal first guess uses maximum information — patterns like 1122 or 0011 for Mastermind.

  • Simultaneous head-to-head

    Both players pick secret codes + submit guesses simultaneously each round — first to solve opponent's wins.

Scoring

Fewest guesses to reach 4 Bulls wins. In head-to-head: first to solve opponent's code, or fewer guesses when both solve.

ActionPoints
4 Bulls (correct guess)WIN — count guesses used
3 Bulls + 0 CowsVery close — 1 digit wrong or misplaced
Exceed turn limit without solvingLOSS (or draw)

Win condition · Solve opponent's secret code in fewer guesses than they solve yours (head-to-head) OR solve in fewer total attempts (comparative).

Playing area

Called

paper or digital interface

Dimensions

4-digit secret number + successive guesses; typically 8-10 guesses to solve.

Paper-and-pencil version: guesses + feedback recorded on paper. Digital version: any app, browser game, calculator interface with guess input + feedback display.

Equipment

  • Paper + pencil

    Traditional version — free / DIY. Draw table with guess + feedback columns.

  • Bulls and Cows apps

    Countless mobile + browser apps. Play against computer or human.

  • Mastermind commercial version

    $15-25 Hasbro/Pressman — pegboard + colored pegs (allows repeat colors, 6+ colors, more possibilities than digit version).

  • Programming exercise

    Common intro-CS assignment for algorithm implementation.

Officiating

Self-officiated. Honor system for code-maker reporting correct bulls/cows honestly. Computer version enforces automatically.

Strategies

  • First guess: pick informative digits

    1234 or 5678 — 4 unique digits. Feedback tells you how many of those 4 digits are in secret + how many positioned correctly.

  • Second guess: complementary digits

    If first was 1234, try 5678 as second — combined feedback covers all 10 digits (or 8, revealing which 4 are in secret).

  • Systematic digit elimination

    After 2-3 guesses, you should know WHICH 4 digits are in secret. Remaining task: determine POSITIONS.

  • Position deduction

    Once digits known, use bulls count to test position combinations. E.g. swap 2 digits — if bulls change by 2, both were wrong; by 0, both were right or both wrong.

  • Avoid wasted guesses

    Don't repeat digits or patterns; each guess should be maximally informative.

  • Track all previous feedback

    Write down every guess + feedback; systematically cross-reference to eliminate impossibilities.

  • Endgame: try likely combinations

    When narrowed to 2-3 possibilities, guess one to differentiate.

Terminology

Bull
Correct digit in correct position.
Cow
Correct digit in wrong position.
4 Bulls
Complete correct guess = win.
0 Bulls + 0 Cows
No digits in secret — narrows possibilities significantly.
4 Cows
All 4 digits correct but ALL in wrong positions — extremely informative + rare.
Secret / Code
The 4-digit number the code-maker chooses.
Guess / Query
4-digit number the code-breaker proposes.
Code-maker / Chooser
Player who picks the secret.
Code-breaker / Guesser
Player who guesses to find secret.
Mastermind
Commercial version with colored pegs instead of digits (Meirowitz 1970).
Feedback
Bulls + cows response after each guess.
Elimination
Removing impossible codes based on feedback.
Optimal strategy
Guess sequence minimizing expected/worst-case guesses to solve.

Major competitions

  • No world championship — casual game

    n/a · n/a

  • Mastermind Tournaments (informal, board game conventions)

    occasional · worldwide · since 1980

  • Programming competitions (algorithm challenges)

    ongoing · computer science academic · since 1980

Where it is played

Governing bodies

  • No formal governing body — folk game / educational tool

    n/a

  • Hasbro/Pressman — owns Mastermind commercial trademark

    Founded 1900 · USA

Famous names & records

Famous athletes

  • No professional Bulls and Cows players — casual + educational game
  • Mordechai Meirowitz (Israel) — inventor of commercial Mastermind version 1970
  • Donald Knuth (USA) — computer scientist who proved 5-guess algorithm for Mastermind 1976
  • Various programming championship contestants — Bulls & Cows AI algorithms popular contest problem

Records

  • Minimum guesses for standard Mastermind

    Donald Knuth (algorithm)5 guesses always sufficient (proven 1976) (1976)

  • Bulls & Cows 4-digit unique (mathematical minimum)

    Algorithm analysis~4-5 guesses average with optimal strategy (2020)

  • Total possible secrets (4-digit unique)

    n/a5040 (10 × 9 × 8 × 7) (2020)

  • Total possible secrets (Mastermind 6 colors × 4 pegs, repeats)

    n/a1296 (6^4) (2020)

Learn this sport

beginner

  1. 1

    Play with paper + pencil

    Learn rules with family/friend. Focus on understanding bulls vs cows distinction.

  2. 2

    Try 20+ games

    Get comfortable interpreting feedback + tracking possibilities.

  3. 3

    Systematic first guesses

    Start with 1234, then 5678 to identify which 8 of 10 digits are in secret.

intermediate

  1. 1

    Learn elimination + inference

    After each guess, write down remaining possibilities; eliminate systematically.

  2. 2

    Solve consistently in 6-7 guesses

    Intermediate players solve most 4-digit unique games in 5-7 guesses.

  3. 3

    Try Mastermind commercial version

    Colored pegs (6-8 colors, repeats allowed) — different math + more variety.

advanced

  1. 1

    Study Knuth's 5-guess algorithm

    Landmark 1976 paper by Donald Knuth on optimal Mastermind strategy.

  2. 2

    Program a Bulls & Cows solver

    Classic intro-CS project; implement AI opponent using minimax.

  3. 3

    Compete in programming challenges

    Codewars, LeetCode have Bulls & Cows algorithm problems + variations.

Frequently asked

How do you play Bulls and Cows?+

Two players. Code-maker picks secret 4-digit number with UNIQUE digits (e.g. 1234, 5087). Code-breaker guesses 4-digit numbers. After each guess, code-maker gives feedback: BULLS (correct digit AND correct position) + COWS (correct digit BUT wrong position). E.g. secret 1234, guess 1425 → 1 Bull (the '1' in position 1) + 2 Cows (the '4' + '2' correct digits but wrong positions). Code-breaker keeps guessing until reaching '4 Bulls' (correct). Fewest guesses wins.

How is Bulls and Cows different from Mastermind?+

Bulls and Cows uses DIGITS (0-9) with UNIQUE digits in secret (no repeats) — 5040 possible secrets. Commercial Mastermind (Meirowitz 1970) uses COLORED PEGS (6-8 colors) with REPEATS ALLOWED — 1296 possible secrets (6 colors × 4 positions). Different math but same core deduction mechanic. Mastermind has more variety (8-color hard version); Bulls & Cows has larger secret space (5040 vs 1296). Bulls and Cows predates Mastermind but Mastermind became commercially dominant version worldwide 1970s onwards.

How many guesses are needed?+

MATHEMATICAL MINIMUM: 4-5 guesses with optimal strategy for 4-digit unique. Donald Knuth (1976) proved 5 guesses always sufficient for standard Mastermind (6 colors, 4 pegs, repeats). HUMAN AVERAGE: 5-8 guesses for competent players; beginners often need 8-10 guesses. RANDOM GUESSING: 15+ guesses average. Turn limit typically 10-12 in casual play. Playing with kids: no limit + focus on teaching deduction.

What's the best first guess?+

1234, 5678 (any 4 unique digits) — feedback reveals how many of those 4 digits are in secret + positioning info. Combined with second guess 5678 (or 0-3 complement), feedback covers all 10 digits — you'll know WHICH 4 digits are in secret. Then focus 3rd+ guesses on determining positions. For Mastermind with 6 colors: optimal first guess is 1122 or 0011 (pairs of colors) per Knuth's analysis — maximizes information gain.

Where did Bulls and Cows come from?+

Uncertain — evidence of similar games in 19th-century England as pencil-and-paper amusement; likely earlier in various forms. Digital version coded 1970 for early mainframes as one of first computer games. Commercial 'Mastermind' with colored pegs invented same year by Mordechai Meirowitz (Israel, 1970) — became massively popular globally. In China + Taiwan, called '猜数字' (Cai Shuzi — Guess Number) + widely played as school + family game. Sometimes attributed to ancient Persian/Indian origins but no verified evidence.

Can I play Bulls and Cows solo?+

Yes — many single-player versions: (1) Mobile + browser apps let you play vs computer (computer picks secret, you guess). (2) Programming exercise — write algorithm that solves Bulls & Cows; test on random codes. (3) Self-testing: pick secret, then try to solve it optimally without cheating. (4) Timed challenges — solve as fast as possible or minimum guesses. Solo play useful for learning + practicing without partner. Also common intro-CS assignment (implement Bulls & Cows in any language).

Sources & provenance

Last verified 2026-08-25.community