Sudoku game in Javascript & HTML5

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:

Sudoku javascript game

  1. No number can repeat itself in the same 3×3 square
  2. No number can repeat itself on the same 9 cells column or row.
  3. 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.
  4. Each game should have one and only solution to be considered a valid Sudoku game.

Browser support:

  • IE9+ (without web-workers support)
  • FF4+
  • Chrome

Flow of things

  1. HTML table markup is generated for the board
  2. there are many pre-generated games for each difficulty level.
    A random one is picked from the default ‘easy’ grade, from the ‘board.games‘ object.
    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.
  3. 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:

    1. 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.
    2. 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
    3. 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.
  4. 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.
  5. 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.

Difficulties

While Generating a complete valid Sudoku game is relatively an easy task, taking out mirror cells and evaluating the game each time for a single solution took me some time to finally come up with a solution. It’s amazing how little information there is about this for Javascript implementation.
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.


The game

Full documentation of the project