A number-guessing game like Bulls and Cows (called Hit & Blow in Japan) needs almost no equipment, and it is easy to make yourself. Here is how to play on paper and how to make it with a program.
What to decide
Before you make it, decide these 3 things.
- Number of digits: 3 digits to start. 4 digits once you are used to it.
- Symbols to use: Usually the digits 0 to 9. If you make a rule of not using 0 (only 1 to 9), there are a few fewer candidates.
- Whether the same symbol can repeat: In Bulls and Cows, the symbols are all different. If you let the same symbol repeat, blows get harder to count, so we recommend not allowing it at first.
With 0 to 9 and all-different symbols, there are 720 candidates for 3 digits and 5040 for 4 digits (How Many Possible Answers?).
Making it on paper
All you need is paper and a pencil.
- The person who picks the answer (the quizmaster) writes all-different digits on paper and turns it face down.
- The guesser says a guess (or writes it on paper).
- The quizmaster compares it with the answer and gives back only the number of hits and blows. They do not say which digits are hits.
- The guesser writes each guess and clue, one row at a time.
- When the hits equal the number of digits, it is solved. Record how many guesses it took.
If the quizmaster gives a wrong clue, the reasoning falls apart. Before giving it back, check one digit at a time by pointing with a finger. If two people take turns being the quizmaster, it becomes a head-to-head game (Bulls and Cows for Two Players).
Making it with a program
To have a computer be the quizmaster, you only need two things: “make an answer” and “judge.” In JavaScript, it looks like this.
// Make an answer of all-different digits
function makeAnswer(digits) {
const pool = [...'0123456789'];
for (let i = pool.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[pool[i], pool[j]] = [pool[j], pool[i]];
}
return pool.slice(0, digits).join('');
}
// Count the hits and blows
function judge(answer, guess) {
let hit = 0, blow = 0;
for (let i = 0; i < guess.length; i++) {
if (guess[i] === answer[i]) hit++;
else if (answer.includes(guess[i])) blow++;
}
return `${hit} hit, ${blow} blow`;
}
const answer = makeAnswer(3);
console.log(judge(answer, '012')); makeAnswermixes up 0 to 9 well (a shuffle) and takes as many digits as you need from the front. This way, the same digit never appears twice.judgecounts a hit if the place matches, and a blow if the symbol is in the answer even though the place does not match.
When we made 20,000 3-digit answers with this program, all 720 possible answers came up. Change '012' on the last line to any guess you like and run it, and the clue is printed.
If you add a screen for typing a guess and a part that shows the guesses and clues side by side, your number-guessing game is done. A program that has the computer solve it is introduced in Solving It with a Computer.
Ideas to make it more fun
- Set a target number: For example, “within digits plus 3 guesses passes.” That is the same as the target number in the app “Bulls and Cows.”
- Change the symbols: Use color or picture cards instead of numbers.
- Make a memo area: Line up 0 to 9 so you can mark numbers not in the answer with ✗ and numbers that are in it with ○.
If you want to try it right away, you can play on the playable board on this site.
