1
1
document . addEventListener ( 'DOMContentLoaded' , ( ) => {
2
2
const grid = document . getElementById ( 'sudokuGrid' ) ;
3
3
4
- // Generate 9 sections (3x3 blocks)
5
- for ( let section = 0 ; section < 9 ; section ++ ) {
6
- const sectionDiv = document . createElement ( 'div' ) ;
7
- sectionDiv . classList . add ( 'section' ) ;
8
-
9
- // Create 9 cells per section
10
- for ( let i = 0 ; i < 9 ; i ++ ) {
4
+ // Create 9x9 grid
5
+ for ( let row = 0 ; row < 9 ; row ++ ) {
6
+ for ( let col = 0 ; col < 9 ; col ++ ) {
11
7
const cell = document . createElement ( 'input' ) ;
12
8
cell . setAttribute ( 'type' , 'number' ) ;
13
9
cell . setAttribute ( 'min' , '1' ) ;
14
10
cell . setAttribute ( 'max' , '9' ) ;
15
- cell . classList . add ( 'cell' ) ;
16
- sectionDiv . appendChild ( cell ) ;
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
+ grid . appendChild ( cell ) ;
17
20
}
18
- grid . appendChild ( sectionDiv ) ;
19
21
}
20
22
} ) ;
21
23
24
+ // Update specific row in the grid
25
+ function updateRow ( ) {
26
+ const rowIndex = parseInt ( document . getElementById ( 'rowSelect' ) . value ) ;
27
+ const rowInput = document . getElementById ( 'rowInput' ) . value ;
28
+
29
+ // Validate input
30
+ if ( rowInput . length !== 9 || ! / ^ \d + $ / . test ( rowInput ) ) {
31
+ alert ( 'Please enter exactly 9 digits (1-9).' ) ;
32
+ return ;
33
+ }
34
+
35
+ const cells = document . querySelectorAll ( '.cell' ) ;
36
+ for ( let i = 0 ; i < 9 ; i ++ ) {
37
+ const cellIndex = rowIndex * 9 + i ;
38
+ cells [ cellIndex ] . value = rowInput [ i ] ;
39
+ }
40
+ }
41
+
22
42
function getPuzzle ( ) {
23
43
const cells = document . querySelectorAll ( '.cell' ) ;
24
44
let puzzle = [ ] ;
@@ -28,7 +48,6 @@ function getPuzzle() {
28
48
const value = parseInt ( cell . value ) || 0 ;
29
49
row . push ( value ) ;
30
50
31
- // Add row to puzzle every 9 cells
32
51
if ( ( index + 1 ) % 9 === 0 ) {
33
52
puzzle . push ( row ) ;
34
53
row = [ ] ;
@@ -38,6 +57,17 @@ function getPuzzle() {
38
57
return puzzle ;
39
58
}
40
59
60
+ function includes1To9 ( arr ) {
61
+ if ( arr . length !== 9 ) return false ;
62
+ let sortedArr = arr . slice ( ) . sort ( ( a , b ) => a - b ) ;
63
+ for ( let i = 0 ; i < 9 ; i ++ ) {
64
+ if ( sortedArr [ i ] !== i + 1 ) {
65
+ return false ;
66
+ }
67
+ }
68
+ return true ;
69
+ }
70
+
41
71
function getRow ( puzzle , row ) {
42
72
return puzzle [ row ] ;
43
73
}
@@ -58,17 +88,6 @@ function getSection(puzzle, x, y) {
58
88
return section ;
59
89
}
60
90
61
- function includes1To9 ( arr ) {
62
- if ( arr . length !== 9 ) return false ;
63
- let sortedArr = arr . slice ( ) . sort ( ( a , b ) => a - b ) ;
64
- for ( let i = 0 ; i < 9 ; i ++ ) {
65
- if ( sortedArr [ i ] !== i + 1 ) {
66
- return false ;
67
- }
68
- }
69
- return true ;
70
- }
71
-
72
91
function sudokuIsValid ( puzzle ) {
73
92
for ( let row = 0 ; row < 9 ; row ++ ) {
74
93
if ( ! includes1To9 ( getRow ( puzzle , row ) ) ) return false ;
@@ -87,11 +106,12 @@ function sudokuIsValid(puzzle) {
87
106
function validateSudoku ( ) {
88
107
const puzzle = getPuzzle ( ) ;
89
108
const resultElement = document . getElementById ( 'result' ) ;
109
+
90
110
if ( sudokuIsValid ( puzzle ) ) {
91
111
resultElement . textContent = "The puzzle is valid!" ;
92
- resultElement . className = "result valid " ;
112
+ resultElement . style . color = "green " ;
93
113
} else {
94
114
resultElement . textContent = "The puzzle is invalid." ;
95
- resultElement . className = "result invalid " ;
115
+ resultElement . style . color = "red " ;
96
116
}
97
117
}
0 commit comments