-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve-sudoku.js
60 lines (52 loc) · 1.6 KB
/
solve-sudoku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Algorithm to solve Sudoku Game
// Check validity of board for each result
async function isValid(board, row, col, char) {
for (let i = 0; i < 9; i++) {
// row
if (board[row][i] == char) return false;
// column
if (board[i][col] == char) return false;
if (
board[3 * Math.floor(row / 3) + Math.floor(i / 3)][
3 * Math.floor(col / 3) + (i % 3)
] == char
)
return false;
}
return true;
}
// Algorithm to solve the board
async function solveSudoku(board, delay) {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
// Cell is blank
if (board[row][col] == '.') {
// Explore all possibilities
for (let char = '1'; char <= '9'; char++) {
// Check validity of the board for each possibility
if (await isValid(board, row, col, char)) {
// Sleep or delay
await new Promise((r) => setTimeout(r, delay));
// Put value on board
const cellId = row + '/' + col;
const cell = document.getElementById(cellId);
cell.innerText = char;
board[row][col] = char;
// Check validity further
if (await solveSudoku(board, delay)) return true;
else {
// Sleep or delay if visualization is true
await new Promise((r) => setTimeout(r, delay));
// Remove value from board
cell.innerText = '';
board[row][col] = '.';
}
}
}
return false;
}
}
}
return true;
}
export { solveSudoku };