Skip to content

Commit 4a04e9d

Browse files
author
KaranMishra3610
committed
Refactor: Apply Iterator pattern to SudokuBoard
1 parent 98c9423 commit 4a04e9d

File tree

1 file changed

+69
-17
lines changed

1 file changed

+69
-17
lines changed
Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,92 @@
1-
package sudoku;
1+
package com.thealgorithms.puzzlesandgames;
22

33
import java.util.Iterator;
44
import java.util.NoSuchElementException;
55

6-
/**
7-
* Represents a Sudoku board with support for iteration using the Iterator pattern.
8-
*/
96
public class SudokuBoard implements Iterable<SudokuBoard.Cell> {
107

11-
private final int[][] board;
128
private final int size;
9+
private final int boxSize;
10+
private final int[][] board;
1311

1412
public SudokuBoard(int size) {
13+
if (size <= 0 || Math.sqrt(size) % 1 != 0) {
14+
throw new IllegalArgumentException("Size must be a perfect square (e.g., 4, 9, 16)");
15+
}
1516
this.size = size;
17+
this.boxSize = (int) Math.sqrt(size);
1618
this.board = new int[size][size];
1719
}
1820

1921
public int getSize() {
2022
return size;
2123
}
2224

23-
public int getValue(int row, int col) {
25+
public int getBoxSize() {
26+
return boxSize;
27+
}
28+
29+
public int get(int row, int col) {
30+
validateCell(row, col);
2431
return board[row][col];
2532
}
2633

27-
public void setValue(int row, int col, int value) {
34+
public void set(int row, int col, int value) {
35+
validateCell(row, col);
36+
if (value < 0 || value > size) {
37+
throw new IllegalArgumentException("Value must be between 0 and " + size);
38+
}
2839
board[row][col] = value;
2940
}
3041

31-
/** Represents a single cell in the Sudoku board */
32-
public static class Cell {
42+
public boolean isValid(int row, int col, int value) {
43+
validateCell(row, col);
44+
if (value <= 0 || value > size) {
45+
return false;
46+
}
47+
48+
// check row
49+
for (int c = 0; c < size; c++) {
50+
if (board[row][c] == value) {
51+
return false;
52+
}
53+
}
54+
55+
// check column
56+
for (int r = 0; r < size; r++) {
57+
if (board[r][col] == value) {
58+
return false;
59+
}
60+
}
61+
62+
// check box
63+
int boxRowStart = (row / boxSize) * boxSize;
64+
int boxColStart = (col / boxSize) * boxSize;
65+
66+
for (int r = 0; r < boxSize; r++) {
67+
for (int c = 0; c < boxSize; c++) {
68+
if (board[boxRowStart + r][boxColStart + c] == value) {
69+
return false;
70+
}
71+
}
72+
}
73+
74+
return true;
75+
}
76+
77+
private void validateCell(int row, int col) {
78+
if (row < 0 || row >= size || col < 0 || col >= size) {
79+
throw new IndexOutOfBoundsException("Cell position out of bounds");
80+
}
81+
}
82+
83+
public class Cell {
3384
private final int row;
3485
private final int col;
35-
private final int value;
3686

37-
public Cell(int row, int col, int value) {
87+
public Cell(int row, int col) {
3888
this.row = row;
3989
this.col = col;
40-
this.value = value;
4190
}
4291

4392
public int getRow() {
@@ -49,26 +98,29 @@ public int getCol() {
4998
}
5099

51100
public int getValue() {
52-
return value;
101+
return board[row][col];
102+
}
103+
104+
public void setValue(int value) {
105+
SudokuBoard.this.set(row, col, value);
53106
}
54107
}
55108

56-
/** Iterator implementation for Sudoku board cells */
57109
private class CellIterator implements Iterator<Cell> {
58110
private int row = 0;
59111
private int col = 0;
60112

61113
@Override
62114
public boolean hasNext() {
63-
return row < size && col < size;
115+
return row < size;
64116
}
65117

66118
@Override
67119
public Cell next() {
68120
if (!hasNext()) {
69121
throw new NoSuchElementException();
70122
}
71-
Cell cell = new Cell(row, col, board[row][col]);
123+
Cell cell = new Cell(row, col);
72124
col++;
73125
if (col == size) {
74126
col = 0;
@@ -82,4 +134,4 @@ public Cell next() {
82134
public Iterator<Cell> iterator() {
83135
return new CellIterator();
84136
}
85-
}
137+
}

0 commit comments

Comments
 (0)