|
1 | 1 | document.addEventListener('DOMContentLoaded', () => {
|
2 | 2 | const grid = document.getElementById('sudokuGrid');
|
3 | 3 |
|
4 |
| - // Create 9x9 grid |
| 4 | + // Dynamically generate a 9x9 grid |
5 | 5 | for (let row = 0; row < 9; row++) {
|
6 | 6 | for (let col = 0; col < 9; col++) {
|
7 | 7 | const cell = document.createElement('input');
|
8 | 8 | cell.setAttribute('type', 'number');
|
9 | 9 | cell.setAttribute('min', '1');
|
10 | 10 | cell.setAttribute('max', '9');
|
11 | 11 | 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 |
| - |
19 | 12 | grid.appendChild(cell);
|
20 | 13 | }
|
21 | 14 | }
|
22 | 15 | });
|
23 | 16 |
|
24 |
| -// Update specific row in the grid |
| 17 | +// Update a specific row |
25 | 18 | function updateRow() {
|
26 | 19 | const rowIndex = parseInt(document.getElementById('rowSelect').value);
|
27 | 20 | const rowInput = document.getElementById('rowInput').value;
|
28 | 21 |
|
29 |
| - // Validate input |
30 | 22 | 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)."); |
32 | 24 | return;
|
33 | 25 | }
|
34 | 26 |
|
35 | 27 | const cells = document.querySelectorAll('.cell');
|
36 | 28 | 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"; |
39 | 73 | }
|
40 | 74 | }
|
41 | 75 |
|
| 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 |
42 | 88 | function getPuzzle() {
|
43 | 89 | const cells = document.querySelectorAll('.cell');
|
44 | 90 | let puzzle = [];
|
@@ -102,16 +148,3 @@ function sudokuIsValid(puzzle) {
|
102 | 148 | }
|
103 | 149 | return true;
|
104 | 150 | }
|
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