This Sudoku game was started as a personal project to bring me back on track with web development after spending months traveling and not using a computer all this time. I thought I forgot most, but it seems getting back to business took less time than I thought, and was also fun process.
I wanted to build a Sudoku game in pure JS, after playing lots of Sudoku on my iPhone. At the beginning the game was simple, I managed to create a board with random numbers which followed the rules of the game:
- No number can repeat itself in the same 3×3 square
- No number can repeat itself on the same 9 cells column or row.
- Every empty cell should have a “mirror” empty cell (for example cells [0,7] & [8,1] OR [3,3] & [5,5]) ps. when Cells and 3×3 Cox index starts from ‘0’ ,left to right, top to bottom.
- Each game should have one and only solution to be considered a valid Sudoku game.
- IE9+ (without web-workers support)
Flow of things
- HTML table markup is generated for the board
there are many pre-generated games for each difficulty level.
A random one is picked from the default ‘easy’ grade, from the ‘
This is done because it takes time (~400ms) to generate a valid Sudoku game, so for
sake of user experience the program first loads a pre-made game.
Meanwhile, the program creates (spawns) 6 new games on 6 different CPU threads via web-workers
and runs all necessary calculations to make sure they are valid games. there are a couple
of good reasons to use web-workers in this situation:
The workers run on different threads, which makes sure the browser doesn’t “hang up”
when intense JS calculations are done, and the UX is kept smooth.
In some rare cases the where calculations becomes too intense for a specific game, it can crush
the browser, so by utilizing the web-workers, this situation will no longer happen
Performance. creating many boards at the same time and sort them by difficultly while the
person is actually interacting with the program in the background is pretty neat.
- The workers run on different threads, which makes sure the browser doesn’t “hang up”
Every time a user choose to play a new game, the program picks one of the games that was generated
when the program first loaded, so the user will have a unique board never played before. every
board the program generates is unique for the user and probably universally unique by the laws of probability.
- The program spawns up to 15 new unique games into the ‘
board.games‘ object for each level of difficulty, so the
user will never have to wait for a game to be calculated and generated, because this already happened in the background.
Tt was clear at the time that using Algorithm-X implementation was the best and maybe only solution. Next was the task of finding the most performance-opt version of it online, which was not an easy task as well. I came up with two versions then picked the one I saw fit.
Now that I had an actual board game with as much missing pieces I could remove, there was something missing, the most important part, of all as I see it, that without it, I see no point of releasing the game to public. The issue of scoring and rating the games I created.
That was the main difficultly, writing algorithms which simulate the human approach of solving sudoku games, from easy methods to more advance, and create a system which interprets that data and translate it to points. It took some weeks and some fine-tuning, but I believe the system is very accurate, when it comes to grading Sudoku games, even though it obviously lake many test cases which are very advanced and probably 99% of the people don’t even know of their existence, and frankly it’s too much of a development effort, as the system is quite good already, even though it’s very fun to develop.
Full documentation of the project