-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathscript.js
112 lines (101 loc) · 2.71 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
105
106
107
108
109
110
111
112
class SudokuSolver {
constructor() {
this.N = 9;
}
isSafe(row, col, val, grid) {
for (let i = 0; i < this.N; i++) {
if (
grid[row][i] === val ||
grid[i][col] === val ||
grid[Math.floor(row / 3) * 3 + Math.floor(i / 3)][
Math.floor(col / 3) * 3 + (i % 3)
] === val
) {
return false;
}
}
return true;
}
solve(grid) {
for (let i = 0; i < this.N; i++) {
for (let j = 0; j < this.N; j++) {
if (grid[i][j] === 0) {
for (let k = 1; k <= 9; k++) {
if (this.isSafe(i, j, k, grid)) {
grid[i][j] = k;
let solvePossible = this.solve(grid);
if (solvePossible) {
return true;
} else {
grid[i][j] = 0;
}
}
}
return false;
}
}
}
return true;
}
solveSudoku(grid) {
return this.solve(grid);
}
}
function initializeSudokuTable() {
const sudokuTable = document.getElementById("sudokuTable");
for (let i = 0; i < 9; i++) {
const row = sudokuTable.insertRow(i);
for (let j = 0; j < 9; j++) {
const cell = row.insertCell(j);
const input = document.createElement("input");
input.type = "text";
input.maxLength = 1;
input.className =
"w-full h-full text-center text-lg border-none focus:ring-2 focus:ring-blue-500";
input.addEventListener("input", validateInput);
cell.appendChild(input);
}
}
}
function validateInput(event) {
const inputValue = event.target.value;
event.target.value = inputValue.replace(/[^1-9]/g, "");
}
function getSudokuPuzzle() {
const puzzle = [];
const rows = document.getElementById("sudokuTable").rows;
for (let i = 0; i < 9; i++) {
const row = rows[i];
const rowData = [];
for (let j = 0; j < 9; j++) {
const input = row.cells[j].querySelector("input");
rowData.push(parseInt(input.value) || 0);
}
puzzle.push(rowData);
}
return puzzle;
}
function displaySolvedPuzzle(solvedPuzzle) {
const rows = document.getElementById("sudokuTable").rows;
for (let i = 0; i < 9; i++) {
const row = rows[i];
for (let j = 0; j < 9; j++) {
const input = row.cells[j].querySelector("input");
input.value = solvedPuzzle[i][j];
}
}
}
function solveSudoku() {
const sudokuSolver = new SudokuSolver();
const puzzle = getSudokuPuzzle();
if (sudokuSolver.solveSudoku(puzzle)) {
displaySolvedPuzzle(puzzle);
} else {
alert("No solution exists.");
}
}
function resetSudokuTable() {
const inputs = document.querySelectorAll("#sudokuTable input");
inputs.forEach((input) => (input.value = ""));
}
initializeSudokuTable();