forked from Avdhesh-Varshney/WebMasterLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (106 loc) · 3.88 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
113
114
115
116
117
118
119
120
121
122
123
124
document.addEventListener('DOMContentLoaded', function () {
const gridSize = 9;
const solveButton = document.getElementById("solve-btn");
solveButton.addEventListener('click', solveSudoku);
const sudokuGrid = document.getElementById("sudoku-grid");
for (let row = 0; row < gridSize; row++) {
const newRow = document.createElement("tr");
for (let col = 0; col < gridSize; col++) {
const cell = document.createElement("td");
const input = document.createElement("input");
input.type = "number";
input.className = "cell";
input.id = `cell-${row}-${col}`;
cell.appendChild(input);
newRow.appendChild(cell);
}
sudokuGrid.appendChild(newRow);
}
});
async function solveSudoku() {
const gridSize = 9;
const sudokuArray = [];
// Fill the sudoku with grid values
for (let row = 0; row < gridSize; row++) {
sudokuArray[row] = [];
for (let col = 0; col < gridSize; col++) {
const cellId = `cell-${row}-${col}`;
const cellValue = document.getElementById(cellId).value;
if (cellValue !== "" && parseInt(cellValue) >= 10) {
alert("Enter valid numbers !");
location.reload();
}
sudokuArray[row][col] = cellValue !== "" ? parseInt(cellValue) : 0;
}
}
// Identify user-input cells and mark them
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const cellId = `cell-${row}-${col}`;
const cell = document.getElementById(cellId);
if (sudokuArray[row][col] !== 0) {
cell.classList.add("user-input");
}
}
}
// Solve the sudoku
if (solveSudokuHelper(sudokuArray)) {
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const cellId = `cell-${row}-${col}`;
const cell = document.getElementById(cellId);
if (!cell.classList.contains("user-input")) {
cell.value = sudokuArray[row][col];
cell.classList.add("solved");
await sleep(20);
}
}
}
} else {
alert("No solution exists for the given Sudoku puzzle.");
}
}
function solveSudokuHelper(board) {
const gridSize = 9;
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
if (board[row][col] === 0) {
for (let num = 1; num <= 9; num++) {
if (isValidMove(board, row, col, num)) {
board[row][col] = num;
// Recursively attempt to solve the Sudoku
if (solveSudokuHelper(board)) {
return true; // Puzzle solved
}
board[row][col] = 0; // Backtrack
}
}
return false; // No valid number found
}
}
}
return true; // All cells filled
}
function isValidMove(board, row, col, num) {
const gridSize = 9;
// Check row and column for conflicts
for (let i = 0; i < gridSize; i++) {
if (board[row][i] === num || board[i][col] === num) {
return false; // Conflict found
}
}
// Check the 3*3 subgrid for conflicts
const startRow = Math.floor(row / 3) * 3;
const startCol = Math.floor(col / 3) * 3;
for (let i = startRow; i < startRow + 3; i++) {
for (let j = startCol; j < startCol + 3; j++) {
if (board[i][j] === num) {
return false; // Conflict found
}
}
}
return true; // No conflicts found
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}