Click on "Solve!" to solve the Sudoku game, and on "Reset" to clear the grid and start again. The grid is filled with a sample Sudoku game, just reset the game and introduce the initial number clues of the Sudoku game you want to solve. Click on any cell to edit its content. You can also type any initial clues you like as long as they satisfy the Sudoku rules, but be aware that, in that case, the solution may not me unique.
The rules to solve a Sudoku game are simple, fill in the empty cells with numbers from 1 to 9 such that: (i) each row and column in the grid contains all numbers from 1 to 9 and (ii) each square in the grid also contains all numbers from 1 to 9. A number of algorithms are able to solve most Sudoku games efficiently. Here, we implement a constraint-based approach in which we transform the Sudoku game into an Integer Linear Program (ILP). An ILP optimizes a linear objective function over a feasible domain, which is defined by a set of linear constraints, plus the addition that all variables must have integer values. Thus, solving the Sudoku game translates to finding a feasible solution to our ILP.
Each cell in the Sudoku grid, situated in row $i$ and column $j$ has 9 binary variables assigned: $x_{ijn} \;\,\forall n \in {1,...,9}$. These variables represent all possible values that a cell can have, for instance, if the cell in the first row and first column on the grid has a value of 1, then $x_{111} = 1$. After defining the binary variables, it isn't hard to define an ILP whose solution will be a Sudoku solution:
$\max\limits_{x} \; 0$
s.t.
$\sum\limits_{n=1}^{9} x_{ijn} = 1 \;\; \,\forall i,j \;\; (1)$
$\sum\limits_{j=1}^{9} x_{ijn} = 1 \;\; \,\forall i,n \;\; (2)$
$\sum\limits_{i=1}^{9} x_{ijn} = 1 \;\; \,\forall j,n \;\; (3)$
$\sum\limits_{i,j \in \; \text{S}} x_{ijn} = 1 \;\; \,\forall \; \text{S} \;,n \;\; (4)$
$x_{ijn} = 1 \;\; \,\forall i,j,n \in \text{I} \;\; (5)$
The constraints in this ILP define the feasible Sudoku solution space. Specifically, the constraints in (1) guarantee that each cell in the grid gets only one number $n$ assigned, the constraints in (2) and in (3) restrict rows and columns, respectively, to have all numbers from 1 to 9 and the constraints in (4) guarantee that all squares (S) in the grid contain all numbers from 1 to 9. Finally, the constraints in (5) enforce the initial clues that you typed in before solving. To this end, it constraints each variable in the set of initial clues $\text{I}$ to have a value of 1, for instance, in the sample Sudoku, the cell in the first row and the second column has a value of 2, hence $x_{112} = 1 \,$ is included in the set of constraints in (5).
As a side note, we do not care about the objective function of the ILP, here set to maximize a constant! This is because a proper Sudoku game, that is, one with the correct set of initial clues, has only one possible solution. Therefore, the feasible space defined by all constraints, including the proper initial clues, contains only one solution, and the optimization of any objective function will render it as a result. However, if an insufficient number of initial clues is given, the feasible space can contain more than one solution. In this case, the algorithm behind the ILP solver—Glpk.js in this case—will retrieve the first feasible solution it encounters.