Fact-checked by Grok 2 weeks ago

Bulls and cows

Bulls and Cows is a traditional two-player code-breaking typically played with and , in which one player secretly chooses a four-digit number consisting of distinct digits from to 9 (with no ), and the other guesses the number by suggesting four-digit combinations, receiving feedback on each guess in the form of bulls—digits that are correct and in the correct position—and cows—digits that are correct but in the wrong position. The guessing continues until they correctly identify the secret number, with the goal of doing so in as few attempts as possible, and the roles can then reverse for a new round. The game's origins are unclear but trace back at least to the as a simple pen-and-paper pastime, predating more formalized versions by over a century. It gained wider recognition in the mid-20th century through early computer implementations, with the first known program, named , developed by Frank King at the in 1968 on the Titan computer system. This version quickly became popular among students and was soon adapted elsewhere, including a 1970 implementation on the operating system at by Jerrold M. Grochow and John Larmouth, where it saw thousands of plays within months among users. By 1971, strategies for optimal play were being published in academic journals, highlighting the game's logical depth. Bulls and Cows has influenced numerous variants and commercial games, most notably serving as the direct inspiration for , a invented in 1970 by Israeli Mordecai Meirowitz that adapts the core mechanics to colored pegs instead of digits. While the standard numeric version uses four digits, adaptations exist with three to six digits or even words and letters, and it has been analyzed mathematically for optimal guessing strategies, with research showing that the worst-case scenario requires up to seven guesses for a four-digit code. The game remains popular in educational settings for teaching logic, , and basic probability, as well as in programming exercises to implement algorithms for automated solving.

History and Origins

Early Development

Bulls and cows originated as a pen-and-paper code-breaking game that predates the commercial released in 1970, with its roots tracing back to logic puzzles of the . The game involved players guessing a secret sequence of digits, receiving on correct positions and matches, and was enjoyed as a simple deductive exercise without formal commercialization. Early manual versions were typically played in educational or recreational settings, such as , where participants used and to practice , though rules varied and lacked standardization across groups. For instance, children in the 1950s recalled playing it during breaks or as a activity to sharpen problem-solving skills. These informal iterations emphasized mental agility over structured play, often in small groups without timers or scoring aids. The first documented computer implementation occurred in 1968, when Dr. Frank King, an PhD student at University, developed a program called on the at the Computer Laboratory. This version allowed multiple users to compete against the machine or each other via a shared leaderboard, marking an early of the game to computational environments and highlighting its appeal for testing algorithmic deduction. King's directly influenced subsequent digital versions and contributed to the game's eventual impact on later code-breaking games like .

Popularization and Influences

The game Bulls and Cows saw significant popularization in the through early computer implementations that made it accessible to programmers and hobbyists. In 1970, a version known as was implemented on the operating system at by Jerrold M. Grochow and Larmouth, building on its prior implementation at Cambridge University and enhancing its appeal in academic computing environments. Shortly thereafter, it was reimplemented on early Unix systems at , where developers like incorporated it into the nascent operating system's recreational software, further disseminating the game among communities. These ports transformed the traditional pen-and-paper format into an interactive digital experience, contributing to its traction during the rise of personal and time-sharing computing. Bulls and Cows exerted notable influence on commercial code-breaking games and media adaptations in the 1970s and beyond. The board game , released in 1970 by Invicta Plastics, was directly inspired by Bulls and Cows, substituting colored pegs for digits while retaining the core deduction mechanics, and it became a bestseller that introduced the concept to a wider consumer audience. Word-based variants share mechanics with television game shows, such as Lingo, which debuted in 1987 and adapted the feedback system for spelling challenges, airing internationally and popularizing in broadcast entertainment. The game's cultural spread extended into educational contexts during this era, where it was valued for teaching logical deduction and . It appeared in puzzle books aimed at computing enthusiasts, such as David H. Ahl's 101 BASIC Computer Games (1973), which included a programmable version of Bulls and Cows to illustrate and problem-solving skills for students and educators. This integration helped position the game as a tool for fostering analytical thinking in classrooms and settings throughout the 1970s.

Gameplay Mechanics

Core Rules

Bulls and Cows is a code-breaking game played by two participants in the standard numeric version. Each player secretly selects a four-digit number consisting of unique digits from 0 to 9, with no leading zero. The objective is for each player to deduce the opponent's secret number through iterative guesses. The game proceeds with players alternating turns, where in each turn a player acts as the codebreaker by proposing a four-digit number adhering to the same rules of unique digits and no leading zero, and the opponent, as codemaker, provides feedback. This process continues, with players switching roles each turn, until one player correctly guesses the opponent's secret number. The first to do so wins the game.

Scoring and Feedback

In the Bulls and Cows game, a "bull" refers to a in the guess that matches both the value and the position of a digit in the secret code. For instance, if the secret code is 1234 and the guess is 1256, the digits 1 and 2 are because they are correct in value and position, resulting in a score of two . This feedback helps the guesser identify exact matches without revealing their locations beyond the count. A "cow," in contrast, indicates a digit in the guess that matches the value of a digit in the secret code but appears in the wrong . Using the same example of secret code 1234 and guess 1256, there are zero cows since neither 5 nor 6 appears in the secret code. The feedback provided after each guess consists solely of the combined counts of bulls and cows, without any additional positional details, ensuring the guesser receives limited information to narrow down possibilities. In the standard four-digit version of the game, the total number of bulls plus cows cannot exceed four, as there are only four s to match. The game concludes for a player when they achieve four bulls, signifying a complete match of the opponent's secret code.

Variants and Adaptations

Numeric Versions

The standard numeric version of Bulls and Cows, often simply referred to as the core numeric implementation, involves a secret consisting of four distinct s from 0 to 9, with no permitted for the secret number to ensure it functions as a proper four-digit . This restriction limits the possible secret codes to 4,536 combinations, calculated as 9 options for the first digit (1-9), 9 for the second (0-9 excluding the first), 8 for the third, and 7 for the fourth. Players alternate guessing the opponent's secret, receiving feedback in the form of "bulls" for correct digits in the correct position and "cows" for correct digits in incorrect positions, with the goal of deducing the in the fewest guesses. Variations in code length adapt the game for different durations and difficulty levels, such as using a three-digit secret for quicker play or a five-digit secret for extended challenges, while maintaining the no-repeats rule and no-leading-zero convention unless otherwise specified. These adjustments alter the total number of possible codes—for instance, a three-digit version yields 648 combinations (9×9×8)—but preserve the fundamental deduction mechanics. Although the traditional rules prohibit repeated digits to emphasize positional logic and permutation solving, some house rules permit repeats, requiring adjusted scoring where each digit in the guess matches at most once against the secret to avoid double-counting. This option increases complexity in feedback interpretation but is less common in standard play, as it deviates from the game's origins in combinatorial puzzles.

Alphanumeric and Word-Based Variants

Alphanumeric variants of Bulls and Cows replace the numeric codes with letters, typically forming valid words or sequences with unique characters to maintain the code-breaking challenge. In these versions, players select a secret code consisting of distinct letters from the alphabet, often limited to four or five positions, and provide feedback on guesses using the same bull and cow system: a bull for each letter in the correct position and a cow for each letter present but in the wrong position. This adaptation shifts the focus from numerical logic to linguistic patterns, encouraging guesses that align with possible words while adhering to the positional feedback rules. A common implementation is the five-letter word variant, where the secret is a word such as "BLACK" or "JUMPY," ensuring all letters are unique to avoid repetition ambiguities in scoring. Players alternate guesses, receiving bull and cow counts after each attempt, similar in structure to the numeric original but drawing on vocabulary knowledge for strategic depth. This format has been documented in pencil-and-paper game collections as a direct extension of the core mechanics, promoting educational value in spelling and deduction. While akin to —a where feedback only indicates the total number of matching letters without positional details—the alphanumeric Bulls and Cows variant provides more granular hints, enhancing solvability. The 2021 release of popularized a digital evolution of this word-based approach, tasking players with six attempts to guess a daily five-letter word, with feedback via colored tiles: green for correct position (equivalent to a bull) and yellow for correct letter in the wrong position (equivalent to a cow), while gray indicates absence. Developed by and later acquired by , Wordle's conceptual foundation mirrors the alphanumeric Bulls and Cows by emphasizing iterative refinement through partial matches, though it simplifies scoring to visual cues and limits guesses for daily engagement. This variant's success, with millions of players, underscores the adaptability of the game's logic to word puzzles. Other adaptations extend the framework to board games and applications using symbols or colors alongside letters, such as in digital apps where secret codes incorporate alphanumeric mixes or thematic icons for varied play. For instance, some versions allow three- to five-letter words with unique characters, providing and cow feedback to guess hidden terms, broadening accessibility beyond pen-and-paper. These expansions retain the original's deductive essence while incorporating elements like animations for , appealing to diverse audiences in educational and recreational contexts.

Implementations

Manual and Pen-and-Paper Play

Manual and pen-and-paper play of Bulls and Cows involves simple, accessible materials that make it ideal for informal settings without requiring any specialized equipment. Players typically need only a sheet of and a to record secret codes, guesses, and feedback, allowing both the codemaker and codebreaker to track progress visually. Optional score sheets or tracking grids can be used to organize multiple guesses and eliminate possibilities systematically, enhancing the game's structure during longer sessions. In social play, the game supports two or more participants, often in pairs or small groups, where players alternate between the roles of codemaker—who secretly selects a with no repeating digits—and codebreaker—who proposes guesses and receives feedback on correct digits in the right (bulls) or wrong (cows) positions. This turn-based format fosters interaction and competition, with rounds lasting 10 to 20 minutes, and groups can extend play by rotating roles or competing to solve fastest. The game's manual format lends itself well to educational contexts, such as classrooms or family gatherings, where it promotes logical thinking, deduction skills, and by encouraging players to refine guesses based on iterative feedback. In school settings, it serves as a short warm-up activity (5–10 minutes) to build , place value understanding, and basic , while group play facilitates collaboration and strategy discussion among students.

Digital and Computer Versions

The first known digital implementation of Bulls and Cows, known as , was developed by Frank King in for the computer at the Cambridge University Computer Laboratory in 1968. This program maintained a league table of players and was an early example of interactive gaming on mainframe systems. In 1970, J. M. Grochow and John Larmouth implemented on the operating system at , as described in a 1972 publication in Software—Practice and Experience. This adaptation expanded accessibility on systems. Around the same period, adaptations appeared on early Unix systems at , with strategy investigations by around 1971. Open-source implementations emerged later, such as the puzzle game, which uses "A" to indicate bulls (correct digit in the right position) and "B" for cows (correct digit in the wrong position). Released under the GNU General Public License, supports console play and is available for Linux distributions like . Post-2010, mobile versions proliferated on and platforms, including "Bulls and Cows - " by Vist Software, which generates random four-digit codes and provides feedback on guesses. Similarly, "Guess a Number - Bulls & Cows" by Paskov.biz offers single-player modes against the computer with adjustable difficulty. Contemporary applications include web-based versions, such as the interactive on MathsIsFun.com, where users input guesses via a to break a hidden four-digit code. AI solvers have also been developed, like the Bayesian network-inspired tool on by vpavlenko, which iteratively queries for bulls and cows feedback to deduce the secret number. In educational contexts, integrations appear in software for low-cost devices; for instance, a proposal for the (OLPC) project outlined a Bulls and Cows activity to enhance skills among students.

Mathematical Analysis

Combinatorics and Possibilities

In the standard numeric variant of Bulls and Cows, the secret code consists of four distinct from 0 to 9, with no . This restriction ensures the code represents a valid four-digit number. The number of possible secrets is calculated as the number of choices for the first digit (1 through 9, giving 9 options), followed by 9 remaining choices for the second digit (0 through 9, excluding the first digit), 8 for the third, and 7 for the fourth, yielding $9 \times 9 \times 8 \times 7 = 4536 possible codes. The system in Bulls and Cows—indicating the number of bulls (correct digits in correct positions) and cows (correct digits in wrong positions)—partitions the space of possible secrets into subsets after each guess. This partitioning allows for systematic elimination of inconsistent codes. Under optimal play, the game's , which branches based on possible feedback outcomes (from 0A0B to 4A0B, where A denotes bulls and B cows), has a maximum depth of 7. Consequently, any secret can be solved in at most 7 guesses, as no path in the tree exceeds this length. The average game length under an optimal strategy is approximately 5.21 turns. This figure arises from exhaustive computation of the minimum expected guesses across all secrets, totaling 26,274 guesses for the 5,040 permutations of four distinct digits (including those with leading zeros), divided by the number of secrets: \frac{26274}{5040} \approx 5.21. Adjusting for the no-leading-zero constraint reduces the secret space to 4,536 but yields a nearly identical average due to the similarity in partitioning behavior.

Strategies and Optimal Play

In Bulls and Cows, an effective initial strategy involves selecting a guess with distinct digits that cover a broad range, such as 0123, to maximize the informational value of the first response by testing multiple unique digits across positions. This approach partitions the initial set of 5,040 possible codes into response categories that guide subsequent guesses efficiently. A seminal computational strategy, adapted from Donald Knuth's algorithm for , applies to Bulls and Cows by selecting each guess that minimizes the worst-case number of remaining possible codes after any bulls-and-cows response. This method uses exhaustive search with pruning to evaluate guesses, ensuring balanced reduction of the possibility space and achieving an average of 5.213 guesses per game across all 5,040 secret codes. Advanced tactics emphasize avoiding repeated digits in early guesses, as the secret code contains none, and leveraging to eliminate options: bulls confirm correct digit-position pairs, while cows indicate digits present elsewhere, allowing systematic exclusion of invalid positions and absent digits. Heuristic variants of the approach, such as those minimizing a weighted of remaining response sizes, yield slightly higher averages of 5.24 guesses, highlighting the gap between optimal and practical human play, which often falls in the 5-6 turn range.

References

  1. [1]
    13. Mastermind / Bulls and Cows | Applications - Python-course.eu
    The game goes back to the 19th century and can be played with paper and pencil. Bulls and Cows -- also known as Cows and Bulls or Pigs and Bulls or Bulls and ...Missing: origin | Show results with:origin
  2. [2]
    [PDF] Strategies for playing MOO, or “Bulls and Cows
    Bulls and Cows has been played as a paper-and-pencil game for a century or more. I first played a computer version in 1968 on Titan, the Cambridge University ...
  3. [3]
    None
    ### Summary of MOO Game Content
  4. [4]
    [PDF] A Mathematical Approach to Simple Bulls and Cows - viXra.org
    Nov 26, 2015 · The first computer program related to the game was written by Frank King at the University Of Cambridge. [3] as MOO, and was similarly ...<|control11|><|separator|>
  5. [5]
  6. [6]
    [PDF] Short Communications - Multics
    Since the game of MOO had proved quite popular at Cambridge, it was, of course, on Dr. Larmouth's mind and we decided to implement a simple version of it on ...Missing: Unix Bell Labs
  7. [7]
    The Earliest Unix Code: An Anniversary Source Code Release - CHM
    Oct 17, 2019 · Unix command for listing file names. pp. 26−34. PDP-7 assembly listing for “moo”. Number guessing game, available in 1970 on Multics and in 1968 ...Missing: 1972 | Show results with:1972<|separator|>
  8. [8]
    Wordle: The Party Game isn't really the first Wordle board game
    Jul 15, 2022 · Much like the original color-based Mastermind (or Cows and Bulls ... TV game show that premiered in Canada in the late '80s. Lingo's basic ...
  9. [9]
    Number Bulls & Cows - Pencil and Paper Games
    One player, the Chooser, thinks of a four-digit number and the other player, the Guesser, tries to guess it. At each turn the Guesser tries a four digit number.
  10. [10]
    Bulls and Cows - Maple Help - Maplesoft
    Bulls and Cows is an old paper-and-pencil game for two players that involves code-breaking. How To Play: •. On a sheet of paper ...Missing: manual | Show results with:manual
  11. [11]
    Bulls & Cows Game - Technoblogy
    Mar 9, 2015 · This article describes a pocket Bulls & Cows game, based on an Arduino Uno or ATmega328, which plays Bulls & Cows with you.
  12. [12]
    299. Bulls and Cows - In-Depth Explanation - AlgoMonster
    In-depth solution and explanation for LeetCode 299. Bulls and Cows in Python, Java, C++ and more. Intuitions, example walk through, and complexity analysis.<|control11|><|separator|>
  13. [13]
    Bulls and Cows — Rules
    You score a cow for each digit that matches a digit of the target but in a different position. For example: If my 'number' is "01234" and your guess is "13794", ...<|control11|><|separator|>
  14. [14]
    Bulls and Cows - Ludozofi
    A simple yet fun code-breaking game with origins dating back to at least 19th century. It is also the predecessor of the famous board game Mastermind. Bulls ...
  15. [15]
    None
    Nothing is retrieved...<|separator|>
  16. [16]
    Word Bulls & Cows - Pencil and Paper Games
    If either word has repeated letters the rule is that each letter can only count towards the score once, and Bulls are counted before Cows. Example. For ...
  17. [17]
    Word Bulls & Cows - Pencil and Paper Games
    ... five-letter Bulls & Cows is: BLACK, FIGHT, JUMPY, DROWN, and VEXES. which use 24 of the 26 letters with one letter repeated. If anyone can find five five ...
  18. [18]
    Wordle's massively popular (Jewish) predecessors - The Forward
    Jan 11, 2022 · An earlier version of Mastermind, of mysterious origin, was called “Bulls and Cows,” and was numbers-based. Like Jotto it was played with a ...<|separator|>
  19. [19]
    Wordle - The New York Times Help Center
    Wordle is a daily word game where players have six attempts to guess a five letter word. Feedback for each guess is given in the form of colored tiles.
  20. [20]
    Cows & Bulls – Guess the Word - App Store - Apple
    Rating 1.5 (4) · Free · iOSCows & Bulls is a classic game of guessing the secret word. It has 3-Letters, 4-Letters and 5-Letters version. The game is played by guessing a word and the ...
  21. [21]
    Teachers' Guide (History, Classroom Tips, Pencil & Paper)
    Bulls & Cows began as a pen-and-paper code-breaking pastime whose exact origin is murky but is often traced to at least the early 20th century. It spread under ...
  22. [22]
    hansklav/MOO: The game of MOO (Bulls and Cows) - GitHub
    The first computer implementation of Bulls and Cows was the MOO program written in 1968 in machine-code by Dr. Frank King for TITAN, the Cambridge University ...
  23. [23]
    Debian Games Main packages
    4digits is a guess-the-number puzzle game. It's called Bulls and Cows, and in China people simply call it Guess-the-Number. The game's objective is to guess ...
  24. [24]
  25. [25]
  26. [26]
    Bulls and Cows - Math is Fun
    Play Bulls and Cows. Discover the hidden code! Click, or use arrow keys, to change your guess in each box. (Bulls and Cows is similar to, but came before, ...
  27. [27]
    Solver for "Bulls and cows" inspired by Akinator Bayesian ... - GitHub
    For every question please answer two numbers: bulls and cows. Example: if your secret number is 1234 and my question is 1453, you should answer '1 2'. Question ...
  28. [28]
    Bulls and cows - The OLPC Wiki
    Mar 29, 2008 · This game focusses on improving mental ability of students.The most obvious advantage of playing 'Bulls and cows' is acquiring deductive ...
  29. [29]
    GRIN - A Mathematical Approach to the Simple Bulls and Cows Code Breaking Game
    **Summary of 4-Digit Bulls and Cows (No Repeats, No Leading Zero):**
  30. [30]
    [PDF] An optimal MOO strategy - arXiv
    Jul 8, 2022 · Calculating the number of bulls and cows from two MOO numbers is called the MOO product below. You will get four bulls if you guess the ...
  31. [31]
    The Minimum Strategy and the Strongest Strategy of the Number ...
    Calculating the number of bulls and cows from two MOO numbers is called the MOO product below. If you guess the MOO number written by your opponent, you ...
  32. [32]
    404 Not Found
    Insufficient relevant content. The requested URL (https://www-cs-faculty.stanford.edu/~knuth/papers/master-mind.pdf) returns a "Not Found" error, indicating the document is unavailable. No sections related to Bulls and Cows, strategies, minimax algorithm, initial guesses, or average number of guesses for the numeric version can be extracted.
  33. [33]
    Bulls and Cows — Rules
    Suppose our average number of guesses is 5. This could be the average of: 2 ... A human player tries to construct a next guess which will yield some ...