Connecting Tech Pros Worldwide Forums | Help | Site Map

Sudoku B

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#1   May 12 '07
Greetings,

The second part of the article defines two groups of methods. One method that
is able to read an entire Sudoku board and a couple of methods that can write
such a board, using a nice format.

We want a method that can read from a Reader stream and initialize the
board accordingly. It would be very convenient if we could read a text file
with content as shown in the example board as shown in th first part of this
article.

Basically we want to read 81 digits or dots; a dot and a '0' both describe an
empty cell, while the digits 1 ... 9 describe a filled cell. Here goes:
Expand|Select|Wrap|Line Numbers
  1. public boolean read(Reader r) {
  2.  
  3.     int i= 0, j= 0; // the first position of the board
  4.  
  5.     try {
  6.         // keep on reading characters:
  7.         for (int x; (x= r.read()) != -1; ) {
  8.             // skip it if not a digit nor a dot
  9.             if (!(Character.isDigit(x) || x == '.')) continue;
  10.  
  11.             // is it a digit 1 ... 9?
  12.             if (!(x == '0' || x == '.'))
  13.                     setValue(i, j, x-'0');
  14.  
  15.             // position i,j at next position, return when done
  16.             if ((j= (j+1)%9) == 0)
  17.                 if (++i == 9) return true;
  18.         }
  19.     }
  20.     catch (IOException ioe) { }
  21.  
  22.     // something went wrong
  23.     return false;
  24. }
The next group of methods can print a board given a Writer. It prints
the board using the same format as shown in the example board shown in the
first part of the article:
Expand|Select|Wrap|Line Numbers
  1.  
  2. // print a horizontal separator line
  3. private void printHorizontal(PrintWriter p) {
  4.     p.println("+-------+-------+-------+");
  5. }
  6.  
  7. // print a little vertical line
  8. private void printVertical(PrintWriter p) {
  9.     p.print("| ");
  10. }
  11.  
  12. // print the Sudoku board
  13. public void print(Writer w) {
  14.  
  15.     PrintWriter p= new PrintWriter(w);
  16.  
  17.     for (int i= 0; i < rows.length; i++) {
  18.         if (i%3 == 0) printHorizontal(p);
  19.         for (int j= 0; j < columns.length; j++) {
  20.             if (j%3 == 0) printVertical(p);
  21.             p.print(board[i][j]+" ");
  22.         }
  23.         printVertical(p);
  24.         p.println();
  25.     }
  26.     printHorizontal(p);
  27.     p.flush();
  28. }
Note that both the read method and the write method do not close the character
streams; the Reader and Writer were passed in as a parameter so the caller of
these methods is responsible for closing the streams. The read method does
catch IOExceptions and simply returns false; all that the caller knows is that
a board could not be read for some reason, i.e. an IOException was thrown or
the content of the Reader didn't make up a valid board configuration.

Now we have all the primitive methods to manipulate a Sudoku board: we can
test and set a value anywhere on the board, we can reset a cell again, we
can initialize and entire board given a Reader and finally we can print the
Sudoku board given a Writer.

The third and last part of the article shows the actual Sudoku solver. All
methods shown above are part of a 'Sudoku' class. The solver method will also
be a member method. See you in part three of this article.

kind regards,

Jos



Reply