473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Sudoku B

11,448 Expert 8TB
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
May 12 '07 #1
0 4938

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

Similar topics

5
by: sub1ime_uk | last post by:
Thought I'd offer a method for solving all possible 9x9 sudoku puzzles in one go. It'll takes a bit of time to run however (and 9x9 seems to be about as big as is reasonably possible before...
5
by: Stewart Gordon | last post by:
I have a few Sudoku puzzles on my site. http://www.stewartsplace.org.uk/mindbenders/ But adding extra columns and rows to separate the 3x3 blocks seems a rather kludgy approach, and the result...
11
by: ago | last post by:
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite...
12
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square...
0
by: JosAH | last post by:
Greetings, a couple of years ago a large part of the world went totally mad. Not because of global climate changes, not because of terrible wars that were started in the Middle East, nor because...
6
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with...
21
by: ningxin | last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite...
3
by: deanchhsw | last post by:
Hello, I'm trying to build a program that solves sudokus and prints out the result on the screen. Here's the code for the class SudokuBoard. this will later be called in a class Sudoku. I'm a newbie,...
1
by: deanchhsw | last post by:
Part A (http://bytes.com/topic/java/insights/645821-sudoku) B (http://bytes.com/topic/java/insights/739704-sudoku-b) C (http://bytes.com/topic/java/insights/739703-sudoku-c) this question refers...
3
by: DannyB13 | last post by:
Hi, and thanks for possible help in advance. Here's my dilemma. I've been making a sudoku generator, and I'm now stuck on one part. I must be able to take a 'solution' and verify that it is correct,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.