473,608 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sudoku B

11,448 Recognized Expert MVP
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 4948

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

Similar topics

5
2634
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 combinatorial explosion completely scuppers this type of program)... Basic idea:- Start with a grid initialised with: 123456789
5
9975
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 isn't aesthetically the best either. There ought to be a way of making the grids look nicer. I've played about a bit with rowgroups and colgroups before discovering that rowgroup is called tbody.
11
4129
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 faster than the ASPN program. If anyone is interested I attached the code in http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html
12
6331
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. but thats not i want to do. I want to recurse back until until it find a correct number. i will post the function which i need the help; ---coding----------------------------------------------------------
0
6726
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 of global famine, but because of a puzzle: Sudoku. This is what Sudoku is all about: +-------+-------+-------+
6
11872
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 so far: However I have found that it does not check it correctly. I just need to check the 9x9 array, which I am passing to this function against the classic sudoku rules and then return true for
21
11361
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 some time and still not able to get the solution out. so i hope you guys can help me out. of course i am not expecting a full solution, but i would greatly appreciate it if anyone can suggest to me what should i do. i am very new to this subject so...
3
8592
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, so making this took me hours and hours of time... // class SudokuBoard, will be called by class Sudoku import java.util.Scanner; import java.io.*; public class SudokuBoard { private int board = new int;
1
1859
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 to the Sudoku howto posted on this website. The links are shown above. My question centers around the boolean variables declared in Part A, such as the following. boolean rows= new boolean; ...
3
5962
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, by checking a few things. 1. There are enough numbers, no 'periods' which signify '0' or 'no input'. 2. That there are no duplicate numbers in any row, column, or 3x3 box. I have been thinking of ways to do this and I've come up with 2 things....
0
8057
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6010
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3959
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4022
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2472
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.