How to build a tombola game in JavaScript
During the pandemic, the gaming industry boomed a lot. After my working hours, I played a lot of games on mobile devices. We played a tombola game over a Zoom call, which was quite interesting. So the game went like this — we had physical tombola tickets and over the Zoom call, we had one organiser who organised the game, and the rest all were the players.
We only faced occasional network issues and people talking too much over the microphone. This hindered my enjoyment of the game.
It was then that I got an idea of how to solve this problem. One way would be to create a tombola game on my own and make it available for users to play.
Getting started with the tombola ticket
Today, we will start with a very basic tombola ticket. Following are the few rules while generating a new ticket each time -
- Rule #1 — There should be 3 rows and 9 columns in a single ticket.
- Rule #2- Every column should have a range of numbers like first column 1–10, a second column 11–20.
- Rule #3- In a specific column, the number must be arranged in increasing order.
- Rule #4- The number should range from 1–99 including both 1 and 99.
- Rule #5- Each row cannot have more than 5 numbers.
Writing the tombola ticket code
The very first task is to create a ticket array. The array should be 3D with 3 rows and 9 columns. The total size of the array will be 27 and we will fill only 15 numbers in that array in ascending order in each column.
I applied basic maths and some permutations and combinations to fill 15 numbers and every row with only 5 numbers.
Smart Calculation
We have 9 columns and 15 numbers.
To fill every column with at least one and a maximum of 2 numbers, we just want 6 columns with 2 numbers each and the remaining 3 columns with one number each.
– 6 columns * 2 number each = 12 numbers
– 3columns * 1number each = 3 numbers
– Total = 15 numbers with at least one number in each column.
Filling a column with only one number
We will start by filling a column that has only a single number.
Again, the columns with a single number are random so we need 3 random columns out of 9 columns that will have only a single number.
To choose 3 columns, I create a random array of all columns from zero to 9 and shuffle them using the shuffle method of Lodash, and then the first 3 elements of the array will be my 3 random columns.
let columns = [0, 1, 2, 3, 4, 5, 6, 7, 8];
const newColumns = shuffle(columns);
3 columns with only single numbers are:
newColumns[0], newColumns[1] & newColumns[2]
Filling 3 columns with a single number in the ticket
My ticket object is a 2D array with 3 rows and 9 columns. To fill the single number in the column, I just need the column number, ticket object, and the exact position of that number.
To find a number within a single column, we again need to put all the numbers within that range in an array and shuffle the array to declare the first element of the array as our single number of that particular column.
import { shuffle } from 'lodash';
// assuming our column no is 3
so number will ranges from 30-40
so const nums = [ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40];
shuffle(nums);
Then nums[0] will be our single number from column 3To find the exact position of the number, I followed the basic concept of dividing the number in ranges to find the exact row in a single column.
For example, we have column number 3. Then the number range for column number 3 will be from 30 to 40. So, if we have a number 33, then it should put in the first row and 3rd column of the ticket object or if we have number 38, then it should put in the 3rd row and 3rd column of the ticket object.
Once we get the random number ranging from 30–40, we just need to choose the row and the concept behind choosing the row is again just basic maths.
The number will lie in:
- Row 1 when it ranges from (30–34](including 34).
- Row 2 when it ranges from (34–37](including 37).
- Row 3 when it ranges from (37–40](including 40).
Once we find the exact row in a single column for our number, we just need to push our number to that position in the ticket object.
I have written a function using the above logic to find the row of a number in a single column.
Filling columns with 2 numbers
Well, this part is also simple. We need 2 number ranges within a single column and then we need to decide the position of each number i.e. the row of each number in a single column. The last step is to repeat this process for 6 columns that have 2 numbers each. Hence, we will end up filling in 2 numbers each in 6 columns.
To decide the number within a range of a single column, we will use the same algorithm we have used while filling getting a single number for a column.
For example, our column number 3 will now have 2 number ranges from 30–40. We will shuffle the array with 30–40 numbers in it and the first 2 elements of the shuffled array will be our 2 numbers of that particular column.
import {shuffle} from 'lodash';
const nums = [31, 32, 33, 34, 35, 36, 37, 38, 39, 40];
shuffle(nums);
nums[0] & nums[1] will be our 2 number from column 3Getting the exact position of 2 numbers of a single column.
Once we get the 2 numbers, we just need the exact position of our 2 numbers. To find the row of those numbers, I followed the concept of comparison.
We will first get the 2 random rows of a single column of 3 columns and then using the last Rule no #3, we will find the exact rows of our 2 numbers. Followed by putting those 2 numbers in the exact position.
For example, for column number 3 we have 2 number ranges from 30–40. Let’s assume our 2 numbers here will be 33, 37. We then decide the rows to put these numbers in increasing order.
In order to get the 2 random rows from 3 rows, we will again shuffle the array of rows and the first 2 elements will be our 3 rows for those 2 numbers.
const rows = [0, 1, 2];
shuffle(rows);
const row1 = rows[0], const row2= rows[1]
row1 & row2 are 2 rows in a column number 3In the end, we have 2 numbers and 2 rows for a single column, we just need to put them in increasing order. The number which is bigger which always the take row which is deeper or bigger. A pretty simple method to arrange our numbers in increasing order in a single column.
Displaying the tombola ticket UI
Our ticket object is generated now. The last step is to display the ticket in the form of an actual ticket on the UI. I am using the Next.js repository and Tailwind CSS for styling. I have created a UI using the flex property to create the exact same UI as our ticket used to have. One last step is to add zero to empty spaces of an array otherwise you will not able to create a 9 X 3 size ticket.
I have simply created a function to fill those undefined positions of an array with zero so far. You can use anything else to compensate for undefined.
Final tombola ticket product
This is what my final ticket looks like.

Conclusion: extend the tombola game
You can extend more functionalities to the ticket. You can even add game prizes like corners, first lines, and the full house by simply allowing the user to click on every number and cancel them, and once they claim the price, cross-check those numbers from the numbers announced. This is not very hard, we just need to apply some pure mathematical logic under the hood.
I am adding the code repository for further reference, and in the future, I will be launching this game for audiences to play.
