forked from Alan0602/SudokuSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
104 lines (94 loc) · 3.64 KB
/
script.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
document.addEventListener("DOMContentLoaded", function() {
const sudokuContainer = document.querySelector('.sudoku-container');
const solveBtn = document.querySelector('.solve-btn');
const resetBtn = document.querySelector('.reset-btn');
const loader = document.querySelector('.loader');
const numbersInput = document.getElementById('numbers');
function createSudokuBoard() {
sudokuContainer.innerHTML = '';
const size = 9;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
const cell = document.createElement('input');
cell.type = 'text';
cell.maxLength = 1;
cell.dataset.row = i;
cell.dataset.col = j;
sudokuContainer.appendChild(cell);
}
}
}
function isValidMove(grid, row, col, num) {
// Check if the number is not present in the current row, column, and box
for (let i = 0; i < 9; i++) {
if (grid[row][i] === num || grid[i][col] === num || grid[row - row % 3 + Math.floor(i / 3)][col - col % 3 + i % 3] === num) {
return false;
}
}
return true;
}
function solveSudoku(grid) {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (grid[row][col] === 0) {
for (let num = 1; num <= 9; num++) {
if (isValidMove(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) {
return true;
}
grid[row][col] = 0;
}
}
return false;
}
}
}
return true;
}
function resetSudoku() {
const cells = document.querySelectorAll('.sudoku-container input');
cells.forEach(cell => cell.value = '');
}
function handleSolveClick() {
loader.style.display = 'block';
const grid = getGridFromInput();
if (grid) {
if (solveSudoku(grid)) {
renderGrid(grid);
} else {
alert('No solution exists for the given Sudoku.');
}
} else {
alert('Invalid Sudoku input. Please check your numbers.');
}
loader.style.display = 'none';
}
function getGridFromInput() {
const cells = document.querySelectorAll('.sudoku-container input');
const grid = Array.from({ length: 9 }, () => Array(9).fill(0));
for (const cell of cells) {
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
const value = parseInt(cell.value);
if (!isNaN(value) && value >= 1 && value <= 9) {
grid[row][col] = value;
} else if (cell.value !== '') {
// Invalid input
return null;
}
}
return grid;
}
function renderGrid(grid) {
const cells = document.querySelectorAll('.sudoku-container input');
cells.forEach(cell => {
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
cell.value = grid[row][col];
});
}
numbersInput.addEventListener('change', createSudokuBoard);
solveBtn.addEventListener('click', handleSolveClick);
resetBtn.addEventListener('click', resetSudoku);
});