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:
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.
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.
There are a couple of globals functions/objects:
First board.init()
is called, which creates an empty table using .generateMainTable()
. Then checks if there is a game that
should be loaded from the window.location.search, and if not, load a random game from the pre-rendered games board.games
object,
and spawn in the background new 6 games, using board.spawnBoards()
, which is a complicated process by itself, which involves
the board.populate()
function, which creates a complete Sudoku game by filling all the cells with random numbers and checking
after each insertion if the game is valid or not using the board.checkNumber()
function. then comes the cells removal part,
which is explained more thoroughly in the "How a Sudoku game is made" section of this file.
after a game was created, analyzed, and given a grade, the program binds the user events, like mouse click, and keyboard support.
this is done by calling the board.bindUserEvents()
in board.init()
. (keyboard is supported when user hovers a cell with the mouse
and then click a number in the keypad, or hit the delete key).
After that, the board.selectBox()
.init() it called to initialize the box which pops up when user clicks a cell that can be changed.
Then, the options.bind_buttons() is called to initialize all buttons like "new", "restart", "save" etc..
The most important object, which is the actual array that represents the game is "board.arr
". after it is totally ready, it is
manifested inside the table element itself using the board.load()
function, and a copy of the game is saved in board.arr_original
.
After everything was initialized, the program injects the table element (board.mainBoard
) to the DOM.
Utilize the analyzer object and determine after every move made how much points to give the game, up to the final calculated grade. After each analyzer pass, the solver determine how to continue. if singles were found (numbers that can only occur in a specific cell) then pass all the solving methods once more from start.
Currently support 5 solving methods, from easy to more advanced: