We are writing a program to check that a filled in Sudoku puzzle is solved correctly. We are required to use the Iterator and Iterable interfaces. The program uses a Cell class to represent each individual cell of a puzzle, and uses a 2D array to represent the entire puzzle. The next method in our class that implements Iterator must return a Cell object array that stores the current row, column and box that we are checking. I have figured out how to store the correct row and column in the array, but having trouble figuring out how to store the correct values for the box. We have to be able to do this for any size Sudoku puzzle, our test cases are 4x4, 9x9, 16x16, and 25x25. What I have right now:
- public class SudokuIterator implements Iterator<Cell []>
-
{
-
private Cell[][] puzzle;
-
private int cursor;
-
-
private SudokuIterator() {}
-
-
public SudokuIterator(Cell[][] puzzle)
-
{
-
this.puzzle = puzzle;
-
cursor = 0;
-
}
-
-
public boolean hasNext()
-
{
-
return cursor < puzzle.length;
-
}
-
-
public Cell[] next()
-
{
-
Cell[] value = new Cell[2 * puzzle.length];
-
-
for(int i = 0; i < value.length; i++)
-
{
-
if(i < puzzle.length) //sets the nth row
-
value[i] = puzzle[cursor][i];
-
else //sets the nth column
-
value[i] = puzzle[i - puzzle.length][cursor];
-
}
-
-
cursor++;
-
-
return value;
-
}
Not looking for exactly how to do it, just need some ideas to put me on the right track. Let me know if I need to be more clear on anything, and thanks for any help.