34 * A panel rendering the Sudoku field. |
34 * A panel rendering the Sudoku field. |
35 * <p> |
35 * <p> |
36 * Cells are identified by zero-based indices from top-left to bottom-right. |
36 * Cells are identified by zero-based indices from top-left to bottom-right. |
37 */ |
37 */ |
38 public final class Field extends JPanel { |
38 public final class Field extends JPanel { |
39 private SudokuTextField[][] cells; |
39 private final SudokuTextField[][] cells; |
40 |
40 |
41 /** |
41 /** |
42 * Constructs a new 9x9 Sudoku grid. |
42 * Constructs a new 9x9 Sudoku grid. |
43 */ |
43 */ |
44 public Field() { |
44 public Field() { |
122 * Sets the value of a specific cell. |
122 * Sets the value of a specific cell. |
123 * |
123 * |
124 * @param x horizontal position |
124 * @param x horizontal position |
125 * @param y vertical position |
125 * @param y vertical position |
126 * @param v the cells value |
126 * @param v the cells value |
|
127 * @throws IllegalArgumentException if v is not between 0 and 9 |
127 */ |
128 */ |
128 public void setCellValue(int x, int y, int v) { |
129 public void setCellValue(int x, int y, int v) { |
129 cells[x][y].setValue(v); |
130 cells[x][y].setValue(v); |
130 } |
131 } |
131 |
132 |
196 * Cells within the square are identified by the same coordinate system. |
197 * Cells within the square are identified by the same coordinate system. |
197 * |
198 * |
198 * @param x horizontal position from 0 to 2 |
199 * @param x horizontal position from 0 to 2 |
199 * @param y vertical position from 0 to 2 |
200 * @param y vertical position from 0 to 2 |
200 * @return a two-dimensional array containing the square cell values |
201 * @return a two-dimensional array containing the square cell values |
|
202 * @throws IllegalArgumentException if the coordinates are out of bounds |
201 */ |
203 */ |
202 public int[][] getSquare(int x, int y) { |
204 public int[][] getSquare(int x, int y) { |
203 if (x < 0 || x > 2 || y < 0 || y > 2) { |
205 if (x < 0 || x > 2 || y < 0 || y > 2) { |
204 throw new IllegalArgumentException("Invalid square coordinates"); |
206 throw new IllegalArgumentException("Invalid square coordinates"); |
205 } |
207 } |
217 /** |
219 /** |
218 * Returns an entire row. |
220 * Returns an entire row. |
219 * |
221 * |
220 * @param y the row position |
222 * @param y the row position |
221 * @return an array containing the row values |
223 * @return an array containing the row values |
|
224 * @throws IllegalArgumentException if y is not between 0 and 8 |
222 */ |
225 */ |
223 public int[] getRow(int y) { |
226 public int[] getRow(int y) { |
224 if (y < 0 || y > 8) { |
227 if (y < 0 || y > 8) { |
225 throw new IllegalArgumentException("Invalid row number"); |
228 throw new IllegalArgumentException("Invalid row number"); |
226 } |
229 } |
227 int row[] = new int[9]; |
230 int[] row = new int[9]; |
228 |
231 |
229 for (int x = 0; x < 9; x++) { |
232 for (int x = 0; x < 9; x++) { |
230 row[x] = getCellValue(x, y); |
233 row[x] = getCellValue(x, y); |
231 } |
234 } |
232 |
235 |
236 /** |
239 /** |
237 * Returns an entire column |
240 * Returns an entire column |
238 * |
241 * |
239 * @param x the column position |
242 * @param x the column position |
240 * @return an array containing the column values |
243 * @return an array containing the column values |
|
244 * @throws IllegalArgumentException if x is not between 0 and 8 |
241 */ |
245 */ |
242 public int[] getColumn(int x) { |
246 public int[] getColumn(int x) { |
243 if (x < 0 || x > 8) { |
247 if (x < 0 || x > 8) { |
244 throw new IllegalArgumentException("Invalid column number"); |
248 throw new IllegalArgumentException("Invalid column number"); |
245 } |
249 } |
246 int column[] = new int[9]; |
250 int[] column = new int[9]; |
247 |
251 |
248 for (int y = 0; y < 9; y++) { |
252 for (int y = 0; y < 9; y++) { |
249 column[y] = getCellValue(x, y); |
253 column[y] = getCellValue(x, y); |
250 } |
254 } |
251 |
255 |