When building this Bulls and Cows Google Sheets game, one of the first decisions I faced was whether to use formulas alone or combine them with Google Apps Script for a smoother gameplay experience.
After experimenting with both approaches, I chose a hybrid setup.
This Bulls and Cows Google Sheets game is built primarily with formulas, while lightweight Google Apps Script is used only for random secret code generation and instant game reset. All core gameplay features—including Bulls and Cows scoring, dynamic play area highlighting, and win messages—are handled directly within the spreadsheet.
If you prefer, you can build the game entirely without Apps Script. However, I personally recommend the hybrid approach, as it creates a smoother and more interactive playing experience.

Download Bulls and Cows in Google Sheets
Before learning how to play or build this Bulls and Cows Google Sheets game from scratch, you can preview and download the template below and start playing right away.
Since this template includes Google Apps Script, you may see the following authorization message the first time you click the Reset button:
A script attached to this document needs your permission to run.
This is completely expected. The script is used only for random secret code generation and instant game reset.
You can safely authorize it. If you’d like to review the code before granting permission, I’ve included the complete Apps Script later in the Build the Bulls and Cows Game Step by Step section of this tutorial.
Preview and Download Bulls and Cows Template
How to Play the Game
Watch the quick demo below to see how the Bulls and Cows Google Sheets game works in action before we go through the rules.
Now that you’ve seen how it works, let’s understand the rules in detail.
This Bulls and Cows Google Sheets game is inspired by the classic Bulls and Cows code-breaking game.
In this version, the computer generates a secret code, and your goal is to crack it using the clues provided:
Bulls – Correct digit in the correct position
Cows – Correct digit in the wrong position
To begin, select a difficulty mode from cell J1.
Available Difficulty Modes
Easy – Guess a unique 3-digit secret code (0–9) in 8 attempts
Normal – Guess a unique 4-digit secret code (0–9) in 10 attempts
Hard – Guess a unique 5-digit secret code (0–9) in 12 attempts
Expert – Guess a 4-digit secret code (0–9), where repeated digits are allowed, in 10 attempts
The game area (B2:F13) automatically adjusts based on the selected difficulty mode.
After selecting a mode, click the Reset button in the top-right corner. This will:
- Generate a new secret code
- Clear the previous game data
- Prepare the play area for a new game
After each game, simply select a mode and click Reset to start again.
Gameplay Steps
- Enter your guess in the first available row
- Check the Bulls and Cows clues on the right
- Use the clues to refine your next guess
- Continue until you crack the code or run out of attempts
If you fail to guess the code within the allowed attempts, the secret code will be revealed automatically.
If you guess the code correctly, you’ll see a winning message.
That’s all it takes to play Bulls and Cows in Google Sheets.
Build the Bulls and Cows Game Step by Step
To build this Bulls and Cows Google Sheets game, start by opening a blank Google Sheets file. By default, it contains one sheet named Sheet1. Rename this sheet to Play.
Next, click the + icon at the bottom to add a new sheet, and rename it Helper.
Step 1: Format the Play Sheet
Resize and Organize the Sheet
Delete all columns after column N.
Then adjust the column widths as follows:
- Select columns A:H, right-click any selected column letter, and set the width to 45
- Set column I width to 180
- Set columns J:N width to 90
Next:
- Delete all rows below row 13
- Select rows 1:13, right-click any selected row number, and set the row height to 30
Merge Cells
Merge the range J2:N13.
Create the Difficulty Drop-down
In cell J1, create a drop-down list with the following options:
Easy, Normal, Hard, Expert
To do this:
- Click Insert → Drop-down
- Add the items above
- Click Advanced options
- Enable Reject the input
- Enable Arrow
Apply Basic Formatting
Select the entire sheet, then align all text both horizontally and vertically to the center using the toolbar.
Next, select columns B:I, then apply:
- Font size: 12
- Bold
Then select the range J2:N13, and set:
- Font size: 20
Apply Font Colors
Set the font color to white for the following ranges:
- B1:F13
- K1
- M1
- J2:N13 (merged range)
Then:
- Enable text wrapping for J2:N13
- Go to View and uncheck Show gridlines
- Select the entire working area and apply Light Grey 2 as the border color
Step 2: Add the Formulas
Before entering the formulas for your Bulls and Cows Google Sheets game:
- Select any difficulty mode in cell J1 (for example, Easy)
- Enter the following field labels in G1:I1:
Bulls | Cows | Check
Now enter the following formulas.
Return the Number of Digits (K1)
=IFNA(IFS(J1="Easy", 3, OR(Play!J1="Normal", Play!J1="Expert"), 4, Play!J1="Hard", 5))
This formula returns the number of digits based on the selected difficulty mode.
Return the Number of Attempts (M1)
=IFNA(IFS(J1="Easy", 8, OR(Play!J1="Normal", Play!J1="Expert"), 10, Play!J1="Hard", 12)," ")
This formula returns the maximum number of attempts allowed for the selected mode.
Generate the Attempt Sequence (A2)
=IFERROR(SEQUENCE(M1))
This formula generates the attempt numbers vertically.
Bulls and Cows Formula (G2)
=BYROW(
INDIRECT("B2:"&CHAR(65+$K$1)&$M$1+1), LAMBDA(row,
LET(
guess, row,
code, TOROW(Helper!A2:F2, 1),
black, SUMPRODUCT(guess=code),
total_matches, SUM(MAP(UNIQUE(guess, TRUE), LAMBDA(c, IF(c="", 0, MIN(COUNTIF(guess, c), COUNTIF(code, c)))))),
white, total_matches - black,
IFERROR(IF(
IF(J1="Expert", COUNTA(guess)<K1, COUNTUNIQUE(guess)<K1),,
{black, IF(J1="Expert", white, SUMPRODUCT(COUNTIF(code, guess))-black)}
))
)
)
)
This is the core formula of the Bulls and Cows Google Sheets game. It compares each guess with the secret code and automatically returns the Bulls and Cows clues.
Check for Duplicate Digits (I2)
=ArrayFormula(BYROW(
INDIRECT("B2:"&CHAR(65+$K$1)&$M$1+1), LAMBDA(r,
LET(
dupl, SUM(COUNTIF(r, r)),
IFNA(IF(AND($J$1<>"Expert", dupl>$K$1), "👈 Enter Unique Digits",))
)
)
))
This formula displays a warning when duplicate digits are entered in game modes that require unique digits.
Winning or Losing Message (J2)
=IF(
LEN(B1), "🎉 AMAZING! YOU BROKE THE CODE! 🏆",
IF(
AND(LEN(B1)=0, COUNTA(G2:G)=M1),"Mission Failed. 🕵️ The code remains a secret: ["&TEXTJOIN("", TRUE, Helper!A2:F2)&"]",
)
)
This formula displays either the winning message or the losing message based on the game result.
Reveal the Secret Code (B1)
=IF(IFNA(XMATCH(K1, G2:G13)), TOROW(Helper!A2:F2, 1), )
This formula reveals the secret code after the player successfully cracks it.
Step 3: Set Up Data Validation
To control user input in your Bulls and Cows Google Sheets game, apply the following data validation rule to the range B2:F13.
Go to:
Data → Data validation → Add rule
Under Criteria, select Custom formula is, then enter the formula below:
=ARRAYFORMULA(
AND(
ISNUMBER(B2),
ISBETWEEN(B2, 0, 9),
OR(
ROW(B2)=2,
COUNT(IF($J$1="Expert", OFFSET($B2, -1, 0, 1, $K$1), UNIQUE(OFFSET($B2,-1, 0, 1, $K$1), TRUE)))=$K$1
)
)
)
Under Advanced options, enable:
- Reject the input
This validation ensures that players can enter only digits from 0 to 9 and helps enforce the game rules based on the selected difficulty mode.
Step 4: Apply Conditional Formatting
To make your Bulls and Cows Google Sheets game visually interactive, apply the following conditional formatting rules.
Game Board Formatting (B2:F13)
Apply the following rules one by one to the range B2:F13.
Rule 1 – Highlight Entered Digits
=AND(LEN(B2), $A2>0, COLUMN(B$2)<=$K$1+1)
Fill color: #455a64
Rule 2 – Highlight Active Play Area
=AND($A2>0, COLUMN(B$2)<=$K$1+1)
Fill color: #37474f
Win or Lose Message Formatting (J2:N13)
Apply the following rules to the merged range J2:N13.
Win Message
=J2="🎉 AMAZING! YOU BROKE THE CODE! 🏆"
Fill color: #00d1ff
Lose Message
=SEARCH("Mission Failed", J2)
Fill color: #ff003c
Secret Code Reveal (B1:F1)
Apply the following rule to the range B1:F1.
=LEN(B1)
Fill color: #00d1ff
Step 5: Set Up the Helper Sheet
In the Helper sheet, delete all rows below row 2 and all columns after column G.
Next, add a small Google Apps Script to generate the secret code and reset your Bulls and Cows Google Sheets game automatically.
Step 6: Add the Reset Button with Google Apps Script
In your Google Sheet, go to:
Extensions → Apps Script
When the Apps Script editor opens, give your project a name such as Bulls and Cows Game.
Delete any existing code in the editor, then paste the following script:
function startNewGame() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const playSheet = ss.getSheetByName("Play");
const helperSheet = ss.getSheetByName("Helper");
const mode = playSheet.getRange("J1").getValue();
const numDigits = playSheet.getRange("K1").getValue();
let allowDuplicates = (mode === "Expert");
let secretCode = [];
// 1. ONLY CLEAR THE INPUT AREA (B2:F13)
// This leaves all your formulas in Row 1 and Columns G:H completely intact.
playSheet.getRange("B2:F13").clearContent();
// 2. GENERATE NEW CODE LOGIC
if (allowDuplicates) {
for (let i = 0; i < numDigits; i++) {
secretCode.push(Math.floor(Math.random() * 10));
}
} else {
let digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let i = digits.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[digits[i], digits[j]] = [digits[j], digits[i]];
}
secretCode = digits.slice(0, numDigits);
}
// 3. CLEAR HELPER DATA AND WRITE NEW SECRET
helperSheet.getRange("A2:E2").clearContent();
helperSheet.getRange(2, 1, 1, secretCode.length).setValues([secretCode]);
// 4. UI RESET
playSheet.getRange("B2").activate();
ss.toast("New Game Started!", "Bulls and Cows");
}
Click Save to save the project.
Go back to the Play sheet.
Then go to:
Insert → Drawing
Create a button or shape labeled Reset, then place it in the top-right area of your Bulls and Cows Google Sheets game.
Next:
- Right-click the drawing
- Click the three-dot menu
- Select Assign script
- Enter:
startNewGame
- Click OK
Now click the Reset button once to run the script and complete the authorization process.
After confirming everything works correctly, you can hide the Helper sheet to keep the game interface clean.
Additional Tip: Playing Bulls and Cows Without Apps Script
Can I play Bulls and Cows in Google Sheets without Apps Script?
Yes. If you prefer a formula-only setup, you can still play the game without using Google Apps Script by following these steps.
Enter the following formula in cell B1 of the Helper sheet:
=LET(
n, Play!K1,
IF(
Play!J1="Expert",
BYCOL(SEQUENCE(1, 4), LAMBDA(r, INDEX(SEQUENCE(10, 1, 0), RANDBETWEEN(1, 10)))),
TOROW(SORTN(SEQUENCE(10, 1, 0), n, 0, RANDARRAY(10, 1), TRUE))
)
)
Next:
- Change the background color of both rows in the Helper sheet to black so the generated values remain hidden
- Copy A1:F1 and paste it as values only into A2:F2
- Start playing the game
How to Start a New Game
- Clear the play area
- Select the desired difficulty mode
- Copy Helper!A1:F1 and paste it as values only into Helper!A2:F2
Conclusion
You’ve now learned how to build a fully dynamic Bulls and Cows Google Sheets game using formulas, conditional formatting, data validation, and optional Google Apps Script.
If you enjoyed this tutorial, explore more interactive spreadsheet projects in these Premium & Free Google Sheets Templates.