33import java .util .Iterator ;
44import java .util .NoSuchElementException ;
55
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+ */
611public class SudokuBoard implements Iterable <SudokuBoard .Cell > {
712
813 private final int size ;
914 private final int boxSize ;
1015 private final int [][] board ;
1116
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+ */
1223 public SudokuBoard (int size ) {
1324 if (size <= 0 || Math .sqrt (size ) % 1 != 0 ) {
1425 throw new IllegalArgumentException ("Size must be a perfect square (e.g., 4, 9, 16)" );
@@ -18,19 +29,46 @@ public SudokuBoard(int size) {
1829 this .board = new int [size ][size ];
1930 }
2031
32+ /**
33+ * Returns the size of the board.
34+ *
35+ * @return the board size
36+ */
2137 public int getSize () {
2238 return size ;
2339 }
2440
41+ /**
42+ * Returns the box (subgrid) size.
43+ *
44+ * @return the size of a subgrid
45+ */
2546 public int getBoxSize () {
2647 return boxSize ;
2748 }
2849
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+ */
2958 public int get (int row , int col ) {
3059 validateCell (row , col );
3160 return board [row ][col ];
3261 }
3362
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+ */
3472 public void set (int row , int col , int value ) {
3573 validateCell (row , col );
3674 if (value < 0 || value > size ) {
@@ -39,6 +77,15 @@ public void set(int row, int col, int value) {
3977 board [row ][col ] = value ;
4078 }
4179
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+ */
4289 public boolean isValid (int row , int col , int value ) {
4390 validateCell (row , col );
4491 if (value <= 0 || value > size ) {
@@ -74,38 +121,77 @@ public boolean isValid(int row, int col, int value) {
74121 return true ;
75122 }
76123
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+ */
77131 private void validateCell (int row , int col ) {
78132 if (row < 0 || row >= size || col < 0 || col >= size ) {
79133 throw new IndexOutOfBoundsException ("Cell position out of bounds" );
80134 }
81135 }
82136
137+ /**
138+ * Represents a single cell on the Sudoku board.
139+ */
83140 public class Cell {
84141 private final int row ;
85142 private final int col ;
86143
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+ */
87150 public Cell (int row , int col ) {
88151 this .row = row ;
89152 this .col = col ;
90153 }
91154
155+ /**
156+ * Returns the row index of this cell.
157+ *
158+ * @return the row index
159+ */
92160 public int getRow () {
93161 return row ;
94162 }
95163
164+ /**
165+ * Returns the column index of this cell.
166+ *
167+ * @return the column index
168+ */
96169 public int getCol () {
97170 return col ;
98171 }
99172
173+ /**
174+ * Gets the current value stored in this cell.
175+ *
176+ * @return the cell value
177+ */
100178 public int getValue () {
101179 return board [row ][col ];
102180 }
103181
182+ /**
183+ * Sets a value in this cell.
184+ *
185+ * @param value the value to set
186+ */
104187 public void setValue (int value ) {
105188 SudokuBoard .this .set (row , col , value );
106189 }
107190 }
108191
192+ /**
193+ * Iterator for traversing all cells in the board.
194+ */
109195 private class CellIterator implements Iterator <Cell > {
110196 private int row = 0 ;
111197 private int col = 0 ;
0 commit comments