Workshop › Sudoku › Step 1 of 28
Play Sudoku, then teach the computer to solve it and to invent new puzzles. A grid read from a string, the three rules in one loop, a backtracking solver that picks the most constrained cell, a generator that keeps each puzzle to exactly one solution, and a number pad for phones.
Sudoku is played on 9 × 9 cells. We draw them, centered, as white squares on a light background.
```js
const canvas = document.getElementById('game')
const ctx = canvas.getContext('2d')
const SIZE = 48
const LEFT = (canvas.width - 9 * SIZE) / 2
const TOP = 56
function draw() {
ctx.fillStyle = '#f8fafc'
ctx.fillRect(0, 0, canvas.width, canvas.height)
for (let r = 0; r < 9; r++) {
for (let c = 0; c < 9; c++) {
const x = LEFT + c * SIZE
const y = TOP + r * SIZE
ctx.fillStyle = '#ffffff'
ctx.fillRect(x, y, SIZE, SIZE)
}
}
}
draw()
```SIZE is a cell's width; LEFT is the free space on each side, so the 9-cell board is centered; TOP leaves room
for the time above.r) and, in each, the 9 columns (c): 81 cells.LEFT + c * SIZE, TOP + r * SIZE.Write the lines under the comments, then press Run.
Skills: Drawing on a canvas, Loops