3
3
import java .util .Iterator ;
4
4
import java .util .NoSuchElementException ;
5
5
6
+ /**
7
+ * Represents a Sudoku board with validation and iteration support.
8
+ * The board is always a square grid of size n x n,
9
+ * where n must be a perfect square (e.g., 4, 9, 16).
10
+ */
6
11
public class SudokuBoard implements Iterable <SudokuBoard .Cell > {
7
12
8
13
private final int size ;
9
14
private final int boxSize ;
10
15
private final int [][] board ;
11
16
17
+ /**
18
+ * Constructs a SudokuBoard of the given size.
19
+ *
20
+ * @param size the dimension of the Sudoku board (must be a perfect square)
21
+ * @throws IllegalArgumentException if size is not a positive perfect square
22
+ */
12
23
public SudokuBoard (int size ) {
13
24
if (size <= 0 || Math .sqrt (size ) % 1 != 0 ) {
14
25
throw new IllegalArgumentException ("Size must be a perfect square (e.g., 4, 9, 16)" );
@@ -18,19 +29,46 @@ public SudokuBoard(int size) {
18
29
this .board = new int [size ][size ];
19
30
}
20
31
32
+ /**
33
+ * Returns the size of the board.
34
+ *
35
+ * @return the board size
36
+ */
21
37
public int getSize () {
22
38
return size ;
23
39
}
24
40
41
+ /**
42
+ * Returns the box (subgrid) size.
43
+ *
44
+ * @return the size of a subgrid
45
+ */
25
46
public int getBoxSize () {
26
47
return boxSize ;
27
48
}
28
49
50
+ /**
51
+ * Gets the value at the given cell.
52
+ *
53
+ * @param row the row index
54
+ * @param col the column index
55
+ * @return the value at the specified cell
56
+ * @throws IndexOutOfBoundsException if indices are invalid
57
+ */
29
58
public int get (int row , int col ) {
30
59
validateCell (row , col );
31
60
return board [row ][col ];
32
61
}
33
62
63
+ /**
64
+ * Sets the value at the given cell.
65
+ *
66
+ * @param row the row index
67
+ * @param col the column index
68
+ * @param value the value to set (0 means empty)
69
+ * @throws IndexOutOfBoundsException if indices are invalid
70
+ * @throws IllegalArgumentException if value is out of range
71
+ */
34
72
public void set (int row , int col , int value ) {
35
73
validateCell (row , col );
36
74
if (value < 0 || value > size ) {
@@ -39,6 +77,15 @@ public void set(int row, int col, int value) {
39
77
board [row ][col ] = value ;
40
78
}
41
79
80
+ /**
81
+ * Checks whether placing a value at the given cell is valid
82
+ * according to Sudoku rules.
83
+ *
84
+ * @param row the row index
85
+ * @param col the column index
86
+ * @param value the value to check
87
+ * @return true if placement is valid, false otherwise
88
+ */
42
89
public boolean isValid (int row , int col , int value ) {
43
90
validateCell (row , col );
44
91
if (value <= 0 || value > size ) {
@@ -74,38 +121,77 @@ public boolean isValid(int row, int col, int value) {
74
121
return true ;
75
122
}
76
123
124
+ /**
125
+ * Ensures that the given cell indices are valid.
126
+ *
127
+ * @param row the row index
128
+ * @param col the column index
129
+ * @throws IndexOutOfBoundsException if indices are outside the board
130
+ */
77
131
private void validateCell (int row , int col ) {
78
132
if (row < 0 || row >= size || col < 0 || col >= size ) {
79
133
throw new IndexOutOfBoundsException ("Cell position out of bounds" );
80
134
}
81
135
}
82
136
137
+ /**
138
+ * Represents a single cell on the Sudoku board.
139
+ */
83
140
public class Cell {
84
141
private final int row ;
85
142
private final int col ;
86
143
144
+ /**
145
+ * Constructs a Cell with the given row and column.
146
+ *
147
+ * @param row the row index
148
+ * @param col the column index
149
+ */
87
150
public Cell (int row , int col ) {
88
151
this .row = row ;
89
152
this .col = col ;
90
153
}
91
154
155
+ /**
156
+ * Returns the row index of this cell.
157
+ *
158
+ * @return the row index
159
+ */
92
160
public int getRow () {
93
161
return row ;
94
162
}
95
163
164
+ /**
165
+ * Returns the column index of this cell.
166
+ *
167
+ * @return the column index
168
+ */
96
169
public int getCol () {
97
170
return col ;
98
171
}
99
172
173
+ /**
174
+ * Gets the current value stored in this cell.
175
+ *
176
+ * @return the cell value
177
+ */
100
178
public int getValue () {
101
179
return board [row ][col ];
102
180
}
103
181
182
+ /**
183
+ * Sets a value in this cell.
184
+ *
185
+ * @param value the value to set
186
+ */
104
187
public void setValue (int value ) {
105
188
SudokuBoard .this .set (row , col , value );
106
189
}
107
190
}
108
191
192
+ /**
193
+ * Iterator for traversing all cells in the board.
194
+ */
109
195
private class CellIterator implements Iterator <Cell > {
110
196
private int row = 0 ;
111
197
private int col = 0 ;
0 commit comments