-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (56 loc) · 1.73 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
const solveButton = document.getElementById('solve');
const cells = document.querySelectorAll('td');
const checkbox = document.querySelector('input[type="checkbox"]');
function exportTable() {
const entries = [];
for (const cell of cells) {
const value = cell.textContent === '' ? 0 : parseInt(cell.textContent, 10);
const clue = cell.classList.contains('black-on-white');
entries.push({ value, clue });
}
const result = [];
for (let i = 0; i < 81; i++) {
const row = Math.floor(i / 9);
const col = i % 9;
if (!result[row]) {
result[row] = [];
}
result[row][col] = entries[i];
}
return result;
}
cells.forEach(cell => {
cell.addEventListener('input', () => {
const nextCell = cell.nextElementSibling;
if (/^[1-9]$/.test(cell.textContent)) {
if (nextCell !== null) {
nextCell.focus();
}
} else if (cell.textContent !== '') {
cell.textContent = '';
}
});
});
cells.forEach(cell => {
cell.addEventListener('click', () => {
if (checkbox.checked) {
cell.classList.toggle('black-on-white');
}
});
});
solveButton.addEventListener('click', () => {
init(exportTable());
if (solveStr8ts()) {
// import data
let i = 0;
for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board[row].length; col++) {
if (!board[row][col].clue)
cells[i].textContent = board[row][col].value;
i++;
}
}
} else {
alert("No solution found!");
}
});