-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudokusolver.js
More file actions
317 lines (274 loc) · 7.8 KB
/
Copy pathsudokusolver.js
File metadata and controls
317 lines (274 loc) · 7.8 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
function generateSudokuGrid() {
const grid = document.querySelector('.sudoku-grid');
for (let row = 1; row <= 9; row++) {
for (let col = 1; col <= 9; col++) {
const cell = document.createElement('div');
cell.className = 'cell';
let numbersHtml = '<div class="cell-numbers">';
for (let i = 1; i <= 9; i++) {
numbersHtml += `<span id="cell_${row}_${col}_${i}">${i}</span>`;
}
numbersHtml += '</div>';
cell.innerHTML = `
${numbersHtml}
<input type="number" min="1" max="9" id="cell_${row}_${col}">
`;
grid.appendChild(cell);
}
}
addInputListeners();
}
function addInputListeners() {
const inputs = document.querySelectorAll('.sudoku-grid input');
inputs.forEach((input, index) => {
input.addEventListener('input', function () {
var errorCell = false;
// Check if the input is a number between 1 and 9
if (this.value < 1 || this.value > 9) {
console.warn(`Invalid input: ${this.value}.`);
this.value = '';
errorCell = true;
}
// Get the cell position and value
const row = Math.floor(index / 9) + 1;
const col = index % 9 + 1;
const value = this.value;
// Check if the cell is valid
if (!isValidCell(row, col, value)) {
console.warn(`Invalid cell: ${this.id}.`);
this.value = '';
errorCell = true;
}
// Clear previous highlights
document.querySelectorAll('.cell-numbers span').forEach(span => span.classList.remove('highlighted'));
// Highlight current cell
if (!errorCell) {
highlightRelatedCells(row, col, value);
}
// Highlight other filled cells
const allCells = document.querySelectorAll('.sudoku-grid input');
allCells.forEach(cell => {
if (cell.value) {
const row = Math.floor(cell.id.split('_')[1]);
const col = Math.floor(cell.id.split('_')[2]);
const value = cell.value;
highlightRelatedCells(row, col, value);
}
});
});
});
}
function highlightRelatedCells(row, col, value) {
if (value) {
// Highlight the cell
for (let i = 1; i <= 9; i++) {
highlightCell(row, col, i);
}
// Highlight row
for (let i = 1; i <= 9; i++) {
highlightCell(row, i, value);
}
// Highlight column
for (let i = 1; i <= 9; i++) {
highlightCell(i, col, value);
}
// Highlight 3×3 block
const blockStartRow = Math.floor((row - 1) / 3) * 3 + 1;
const blockStartCol = Math.floor((col - 1) / 3) * 3 + 1;
for (let row = blockStartRow; row < blockStartRow + 3; row++) {
for (let col = blockStartCol; col < blockStartCol + 3; col++) {
highlightCell(row, col, value);
}
}
}
}
function highlightCell(row, col, value) {
const span = document.getElementById(`cell_${row}_${col}_${value}`);
if (span) {
span.classList.add('highlighted');
}
}
function resolveSudoku() {
// Implement Sudoku solving logic here
console.log('Resolve button clicked.');
// First step is to check if the grid is valid
if (isValidGrid()) {
console.log('Grid is valid.');
} else {
alert('\u{1F914} Grid is not valid.');
}
// Second step: fill the cells with obvious values
var cellsFilled = true;
while (cellsFilled) {
cellsFilled = false;
// Check if there are any cells that can be filled
for (let row = 1; row <= 9; row++) {
for (let col = 1; col <= 9; col++) {
if (!document.getElementById(`cell_${row}_${col}`).value) {
// Get the cell possible values
const possibleValues = getCellPossibleValues(row, col);
// If there is only one possible value, fill the cell
if (possibleValues.length === 1) {
document.getElementById(`cell_${row}_${col}`).value = possibleValues[0];
highlightRelatedCells(row, col, possibleValues[0]);
cellsFilled = true;
}
}
}
}
}
// Check if the grid is already solved
if (isGridSolved()) {
setTimeout(function () {
alert('\u{1F92F} Sudoku solved!\n\u{1F913} The grid was solved by filling the obvious values.');
}, 100);
} else {
// Third step: use the backtracking algorithm to solve the rest of the grid
// Get the grid from the document
var grid = [];
for (let row = 1; row <= 9; row++) {
grid[row - 1] = [];
for (let col = 1; col <= 9; col++) {
grid[row - 1][col - 1] = parseInt(document.getElementById(`cell_${row}_${col}`).value) || 0;
}
}
// Solve the grid using the backtracking algorithm
if (backtrackSolveSudoku(grid, 0, 0)) {
// Fill the grid with the solved grid
fillGrid(grid);
setTimeout(function () {
alert('\u{1F92F} Sudoku solved!\n\u{1F916} The backtracking algorithm has been used.');
}, 100);
} else {
// No solution found
alert('\u{1F62D} No solution found!');
}
}
}
function backtrackSolveSudoku(grid, row, col) {
// Backtracking algorithm to solve the grid
while (grid[row][col] !== 0) {
if (col < 8) {
col++;
} else if (col === 8 && row < 8) {
row++;
col = 0;
} else {
return true;
}
}
for (let i = 1; i <= 9; i++) {
if (isValidCell(row + 1, col + 1, i, grid)) {
grid[row][col] = i;
if (backtrackSolveSudoku(grid, row, col)) {
return true;
}
grid[row][col] = 0;
}
}
return false;
}
function isValidGrid() {
// Check if the grid is valid
for (let row = 1; row <= 9; row++) {
for (let col = 1; col <= 9; col++) {
if (!isValidCell(row, col, document.getElementById(`cell_${row}_${col}`).value)) {
return false;
}
}
}
// All cells are valid, the grid is valid
return true;
}
function isValidCell(row, col, value, grid) {
// Check if the cell is valid
// If the cell is empty, it is valid
if (value === '') {
return true;
}
// Check if the number is already in the row
for (let i = 1; i <= 9; i++) {
if (grid) {
if (i !== row && grid[i - 1][col - 1] === value) {
return false;
}
} else {
if (i !== row && document.getElementById(`cell_${i}_${col}`).value == value) {
return false;
}
}
}
// Check if the number is already in the column
for (let i = 1; i <= 9; i++) {
if (grid) {
if (i !== col && grid[row - 1][i - 1] === value) {
return false;
}
} else {
if (i !== col && document.getElementById(`cell_${row}_${i}`).value == value) {
return false;
}
}
}
// Check if the number is already in the 3×3 block
const blockStartRow = Math.floor((row - 1) / 3) * 3 + 1;
const blockStartCol = Math.floor((col - 1) / 3) * 3 + 1;
for (let i = blockStartRow; i < blockStartRow + 3; i++) {
for (let j = blockStartCol; j < blockStartCol + 3; j++) {
if (grid) {
if (i !== row && j !== col && grid[i - 1][j - 1] === value) {
return false;
}
} else {
if (i !== row && j !== col && document.getElementById(`cell_${i}_${j}`).value == value) {
return false;
}
}
}
}
// All checks passed, the cell is valid
return true;
}
function isGridSolved() {
// Check if the grid is solved
for (let row = 1; row <= 9; row++) {
for (let col = 1; col <= 9; col++) {
if (!document.getElementById(`cell_${row}_${col}`).value) {
return false;
}
}
}
// All cells are filled, the grid is solved
return true;
}
function getCellPossibleValues(row, col, grid) {
// Get the cell possible values
const possibleValues = [];
for (let i = 1; i <= 9; i++) {
if (isValidCell(row, col, i, grid)) {
possibleValues.push(i);
}
}
// Return the possible values
return possibleValues;
}
function clearSudoku() {
// Clear the grid
const inputs = document.querySelectorAll('.sudoku-grid input');
inputs.forEach(input => input.value = '');
document.querySelectorAll('.cell-numbers span').forEach(span => span.classList.remove('highlighted'));
}
function fillGrid(grid) {
// Clear the grid
clearSudoku();
// Fills the grid with the array
for (let row = 1; row <= 9; row++) {
for (let col = 1; col <= 9; col++) {
if (grid[row - 1][col - 1] !== 0) {
document.getElementById(`cell_${row}_${col}`).value = grid[row - 1][col - 1];
highlightRelatedCells(row, col, grid[row - 1][col - 1]);
}
}
}
}
generateSudokuGrid();