By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Having trouble with a Sudoku project.

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. public class SudokuIterator implements Iterator<Cell []>
  2. {
  3.   private Cell[][] puzzle;
  4.   private int cursor;
  6.   private SudokuIterator() {}
  8.   public SudokuIterator(Cell[][] puzzle)
  9.   {
  10.     this.puzzle = puzzle;
  11.     cursor = 0;
  12.   }
  14.   public boolean hasNext()
  15.   {
  16.     return cursor < puzzle.length;
  17.   }
  19.   public Cell[] next()
  20.   {
  21.     Cell[] value = new Cell[2 * puzzle.length];
  23.     for(int i = 0; i < value.length; i++)
  24.     {
  25.       if(i < puzzle.length) //sets the nth row
  26.         value[i] = puzzle[cursor][i];
  27.       else //sets the nth column
  28.         value[i] = puzzle[i - puzzle.length][cursor];
  29.     }
  31.     cursor++;
  33.     return value;
  34.   }
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.
Oct 4 '09 #1
Share this Question
Share on Google+
1 Reply

P: 13,264
Browse the Java insights section on this site.
Oct 5 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.