Cows and Bulls

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only




Problem Description

Cows and Bulls is an the predecessor the game of Mastermind which uses the digits 0-9 instead of colored pegs.

Background & Techniques

"Cows and Bulls" (aka Bulls and Cows) is a version of the Game of Mastermind  previously published here on DFF but played with integer digits instead of colored pegs. The objective is to guess a secret pattern given feedback after each guess about the number of correct digits in the correct position (the Bulls), and the number of correct digits in the wrong position (the Cows).

According to Wikipedia, Cows and Bulls predates Mastermind and was probably the inspiration for that game.

There are now 10 digits to choose from in selecting or guessing a secret pattern. This version allows
patterns 3, 4, or 5 digits long. Zeros are valid as leading digits.

Choose whether you want to be the "Hider" (program searches for a pattern you choose) or the
"Seeker", (you try to find a secret number selected by the program). Select a digit length, default is 4, and
click the "Start" button to begin. pattern

For information about the programming and mathematics techniques used in the program, see the mastermind link above.  Because of the number of patterns to analyze and the algorithm used, the "Smarter than you" option for 5 digit numbers may take the program a few minutes to find its 2nd & 3rd guesses.

Addendum February 1, 2008:  Version 2 was posted today with several enhancements:

bulletPatterns up to 6 digits long are handled now,
bulletRepeated digits within the pattern may be permitted or not under user control. The  patterns for this option are generated using  TComboSet to create  permutations.  The TComboset class is contained in UComboV2 unit found in the DFFLibrary zip file available for download here.
bullet A  "Show progress" checkbox allows display of a summary of solution steps as the game progresses plus a display of up to 1000 patterns which have not been eliminated as solutions by preceding guesses.
bulletIn addition to selecting a role as "Hider" or "Seeker", the user can now also select to play "Both" roles.  This was added to assist in debugging, but may be useful as a learning tool to see the effect of pattern guesses and the resulting scores on the set of patterns which are possible solutions.
bulletSearches for 5 and 6 digit numbers are significantly faster than in Version 1. 

Addendum December 5, 2010:  Another display bug fix today caused by incorrect scaling for high resolution screens and posted as Versions 3.  Another bug discovered by my 6 year-old "beta tester: was also fixed: the program now gives an error  message rather than looping forever when a blank guess is entered!

   Running/Exploring the Program 

bullet Download  executable
bullet Download source (One time download of DFF Library required to recompile)
bullet Download current DFF Library Source (DFFLibV15 )

Suggestions for Further Explorations

bullet Done Feb, 2008:  In the "official" rules, digits may not be repeated in the pattern but in this program they can be.  It would be an easy addition to allow the player to optionally restrict the pattern to unique digits.
bullet Improve the efficiency of the min-max algorithm used in the "Smarter than you" IQ level which now may make take several minutes for the early guess when there are up to a million possible solutions remaining.


Original Date: September 24, 2007 

Modified: May 15, 2018


  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.