Skip to content

Commit fb2a371

Browse files
Update script.js
1 parent 785ffff commit fb2a371

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

script.js

+59-26
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,90 @@
11
document.addEventListener('DOMContentLoaded', () => {
22
const grid = document.getElementById('sudokuGrid');
33

4-
// Create 9x9 grid
4+
// Dynamically generate a 9x9 grid
55
for (let row = 0; row < 9; row++) {
66
for (let col = 0; col < 9; col++) {
77
const cell = document.createElement('input');
88
cell.setAttribute('type', 'number');
99
cell.setAttribute('min', '1');
1010
cell.setAttribute('max', '9');
1111
cell.classList.add('cell', 'bg-white', 'border', 'border-gray-300', 'text-center');
12-
cell.style.width = '3rem';
13-
cell.style.height = '3rem';
14-
15-
// Add thicker borders for 3x3 sections
16-
if (row % 3 === 0 && row !== 0) cell.style.borderTopWidth = '2px';
17-
if (col % 3 === 0 && col !== 0) cell.style.borderLeftWidth = '2px';
18-
1912
grid.appendChild(cell);
2013
}
2114
}
2215
});
2316

24-
// Update specific row in the grid
17+
// Update a specific row
2518
function updateRow() {
2619
const rowIndex = parseInt(document.getElementById('rowSelect').value);
2720
const rowInput = document.getElementById('rowInput').value;
2821

29-
// Validate input
3022
if (rowInput.length !== 9 || !/^\d+$/.test(rowInput)) {
31-
alert('Please enter exactly 9 digits (1-9).');
23+
alert("Please enter exactly 9 numbers (1-9).");
3224
return;
3325
}
3426

3527
const cells = document.querySelectorAll('.cell');
3628
for (let i = 0; i < 9; i++) {
37-
const cellIndex = rowIndex * 9 + i;
38-
cells[cellIndex].value = rowInput[i];
29+
cells[rowIndex * 9 + i].value = rowInput[i];
30+
}
31+
}
32+
33+
// Update all rows
34+
function updateAllRows() {
35+
const allRowsInput = document.getElementById('allRowsInput').value;
36+
37+
if (allRowsInput.length !== 81 || !/^\d+$/.test(allRowsInput)) {
38+
alert("Please enter exactly 81 numbers (1-9).");
39+
return;
40+
}
41+
42+
const cells = document.querySelectorAll('.cell');
43+
for (let i = 0; i < 81; i++) {
44+
cells[i].value = allRowsInput[i];
45+
}
46+
}
47+
48+
// Validate Sudoku
49+
function validateSudoku() {
50+
const puzzle = getPuzzle();
51+
const resultElement = document.getElementById('result');
52+
const validateButton = document.getElementById('validateButton');
53+
54+
// Reset button to orange before validating
55+
validateButton.classList.remove('bg-red-500', 'bg-green-500');
56+
validateButton.classList.add('bg-orange-500', 'hover:bg-orange-600');
57+
validateButton.style.backgroundColor = "";
58+
resultElement.textContent = "";
59+
60+
if (sudokuIsValid(puzzle)) {
61+
resultElement.textContent = "The puzzle is valid!";
62+
resultElement.style.color = "green";
63+
validateButton.classList.remove('bg-orange-500', 'hover:bg-orange-600');
64+
validateButton.classList.add('bg-green-500');
65+
validateButton.style.backgroundColor = "green";
66+
showConfetti();
67+
} else {
68+
resultElement.textContent = "The puzzle is invalid.";
69+
resultElement.style.color = "red";
70+
validateButton.classList.remove('bg-orange-500', 'hover:bg-orange-600');
71+
validateButton.classList.add('bg-red-500');
72+
validateButton.style.backgroundColor = "red";
3973
}
4074
}
4175

76+
// Confetti effect
77+
function showConfetti() {
78+
const canvas = document.getElementById('confettiCanvas');
79+
const confetti = new ConfettiGenerator({ target: canvas });
80+
confetti.render();
81+
82+
setTimeout(() => {
83+
confetti.clear();
84+
}, 5000);
85+
}
86+
87+
// Utility functions
4288
function getPuzzle() {
4389
const cells = document.querySelectorAll('.cell');
4490
let puzzle = [];
@@ -102,16 +148,3 @@ function sudokuIsValid(puzzle) {
102148
}
103149
return true;
104150
}
105-
106-
function validateSudoku() {
107-
const puzzle = getPuzzle();
108-
const resultElement = document.getElementById('result');
109-
110-
if (sudokuIsValid(puzzle)) {
111-
resultElement.textContent = "The puzzle is valid!";
112-
resultElement.style.color = "green";
113-
} else {
114-
resultElement.textContent = "The puzzle is invalid.";
115-
resultElement.style.color = "red";
116-
}
117-
}

0 commit comments

Comments
 (0)