Nicolas VenturaAboutPublicationsGamesPhotosTools

Sudoku Boards

Lately, I've been obsessed with the well-known newspaper game, Sudoku. It's actually such a simple puzzle. Or rather, it has simple rules - the puzzle itself can be very difficult. My mind wanders when I play the game, and it led me to wanting to create my own puzzles. Without doing any prior research, I set out to building a program that could generate Sudoku boards. I also wanted to know how many possible boards there could be, but I haven't done the math for that yet.

Starting with a Seed

My idea to generate a puzzle was to start with a perfectly-solved board, I call the "seed", and basically shuffle the numbers around. Here's my seed:

$$ \begin{matrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ 4 & 5 & 6 & 7 & 8 & 9 & 1 & 2 & 3 \\ 7 & 8 & 9 & 1 & 2 & 3 & 4 & 5 & 6 \\ 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 1 \\ 5 & 6 & 7 & 8 & 9 & 1 & 2 & 3 & 4 \\ 8 & 9 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ 3 & 4 & 5 & 6 & 7 & 8 & 9 & 1 & 2 \\ 6 & 7 & 8 & 9 & 1 & 2 & 3 & 4 & 5 \\ 9 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \end{matrix} $$

The way I created it was going row-by-row and shifting the numbers around. The first row contains the numbers 1 through 9 in order, the second row starts with 4 (shifting left by 3), and the third row starts with 7 (shifting left by 6.)

The next group of 3 rows is the same as the first group of 3, except every number is shifted left by 1. The last group of 3 rows has all their numbers shifted left by 2. By default, this ensures that each row contains all 9 numbers. This algorithm also ensures that each column and 3x3 box contains all 9 numbers.

Shuffling Methods

The next step was to figure out a way to shuffle the board around while keeping its validity. (Meaning that each row, column, and 3x3 box still contain all 9 numbers.) I defined 4 methods that hold this true.

Swap 2 rows within a "box"

One way to shuffle these numbers around is to swap them. This "function" swaps 2 full rows of 9 numbers each. Obviously, each row will still have all 9 numbers since we are not changing anything within any row, so rows "pass the check." Each column also automatically passes the check as well, since in each column, we are just swapping the position of 2 numbers. For example, if we swap the first and last row, the first column $1;4;7;2;5;8;3;6;9$ changes to $9;4;7;2;5;8;3;6;1$, which is still valid. However, this will cause the check to fail for the grid boxes. In this case, the top-left grid box becomes this, which contains two 9's and no 3's.

$$ \begin{matrix} 9 & 1 & 2 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix} $$

The way to guarantee validity is to restrict which rows can be swapped. If you swap 2 rows within the same group of 3, each 3x3 box still contains the same 9 numbers. That means we can only swap rows (1 and 2), (1 and 3), and (2 and 3) for example. The same can be said for rows 4, 5, and 6, and for rows 7, 8, and 9.

Since this is a more restrictive rule from what I mentioned earlier, the rows and columns checks still pass. And now, the box check passes, too, because no numbers are "leaving" or being "introduced" to any box.

Swap 2 columns within a "box"

By following the same logic, we can also swap columns in a way that keeps the board valid. This rule lets you swap columns. To pass the validity checks, you can only swap columns that are within the same grid box.

So far, these two rules heavily restrict how much the board can be shuffled. Row 1 can only ever "move" as far as row 3, for example. So, I needed to come up with more ways to shuffle the board while maintaining validity.

Swap 2 "box-rows"

To be able to move numbers around some more, I discovered that you can swap entire boxes. However, you quickly run into problems if you just swap 2 boxes on their own. For example, starting from the seed, if I swap the top-left and middle-center boxes, the grid boxes still pass the check, but all rows and columns fail.

I got around this by forcing the rows stay valid. So instead of just swapping one box around, swap a row of 3 boxes with another row of 3 boxes. This way, each row and box are by definition still valid, and each column is still valid with just numbers shuffled around.

Swap 2 "box-columns"

The final function I defined for seed shuffling was the same as the previous rule except swapping a column of 3 boxes with another column of 3 boxes. Since we aren't changing the contents of any column or box, those automatically pass. Each row contains the same numbers just shuffled around, so rows stay valid too.

Putting it to the test

Equipped with my 4 shuffling functions, I coded up a Sudoku board shuffler where I could manually choose the functions to use. I theorized that with a sufficient combination of these four functions, I should be able to create any valid board, so I put it to the test.

swapRow(boxRow, rowA, rowB)
swapCol(boxCol, colA, colB)
swapBoxRow(boxRowA, boxRowB)
swapBoxCol(boxColA, boxColB)

I played a quick game online and was hit with this board:

$$ \begin{matrix} 1 & 6 & 2 & 5 & 9 & 8 & 4 & 3 & 7 \\ 4 & 7 & 9 & 1 & 3 & 2 & 5 & 8 & 6 \\ 5 & 3 & 8 & 7 & 6 & 4 & 9 & 1 & 2 \\ 3 & 4 & 5 & 8 & 7 & 1 & 6 & 2 & 9 \\ 8 & 9 & 1 & 2 & 5 & 6 & 3 & 7 & 4 \\ 7 & 2 & 6 & 3 & 4 & 9 & 1 & 5 & 8 \\ 9 & 1 & 3 & 4 & 2 & 7 & 8 & 6 & 5 \\ 6 & 8 & 7 & 9 & 1 & 5 & 2 & 4 & 3 \\ 2 & 5 & 4 & 6 & 8 & 3 & 7 & 9 & 1 \end{matrix} $$

I couldn't find any combination of those functions to get anywhere close to this. I tried to break it down, by just matching the first row, column, or box, but I couldn't do that either.

The problem is that my four rules are too restrictive. Look at the first row: I need to put a 6 next to a 1. There's no combination of moves that I can make to make that happen. Because I can only swap columns within a box, using my seed I can only put a 1 next to a 2, 3, 8, or 9 in the same grid box. I could easily put a 1 next to a 6 by running these 2 functions:

swapCol(1, 1, 3)
swapCol(2, 1, 3)

After that, the first row looks like: $3,2,1,6,5,4,7,8,9$

The 1 and the 6 are next to each other, but they're still in separate boxes, so I can't move them together to the first and second position in that row, like they are in the game I played.

My revised hypothesis

I think I am on the right track. Since I can't make all possible boards with those four functions, there must be another function (or multiple) that I'm missing which lets me shuffle the board in a new way. Combined with my four functions, that should allow for more board configurations than I can currently make.

I've been playing with the idea of board rotations and box rotations, but I'm not convinced that would work 100% of the time.

I could look it up, but what's the fun in that? I'll sleep on it. If it doesn't come to me in a dream, then I'll do some research.

Published on 04 December 2026. Go back to all posts.