473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

sudoku validity checker

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
false.

Any ideas?
/** Check whether grid[i][j] is valid in the grid */
bool isValid(int grid[] [9])
{
int i, j;
bool status;
status = true;

for (int column = 0; column < 9; column++)
if (column != j && grid[i] [column] == grid[i] [j])
status = false;

for (int row = 0; row < 9; row++)
if (row != i && grid[row] [j] == grid[i] [j])
status = false;

for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row] [col] == grid[i] [j])
status = false;

for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] != 0)
status = false;

for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if ((grid[i][j] < 0) || (grid[i][j] 9))
status = false;
return status;

}
Nov 21 '07 #1
6 11851
blux wrote:
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.
In what sense? Do you expect c.l.c++ readers to be sudoku masters?
If your program doesn't work as expected, read the FAQ 5.8, and
follow its advice
>
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
false.

Any ideas?
Yes. Read the FAQ.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 21 '07 #2
However I have found that it does not check it correctly.

You didn't tell us how it misbehaves, and didn't give a compilable
example.

Well all those for loops (and without comments too) are certainly
confusing until you spend enough time to step through them, and then
again, from experience, subtle bugs are very easy to miss. If I was to
write something similar, the first thing that comes to mind is this:

An entry is valid, if the number is unique in the row and column, and
also in the inner square (don't know sudoku terminology, sorry). So,
inside a function, i would have three vectors (capacity 9 for 9 rows/
cols/squares in the sudoku table) of vectors (capacity 9 for 9 entries
per row/col/square) of ints, one for the row, one for the column, one
for the inner square, initialized to zero. For each entry you read
from the sudoku table, update the corresponding vector element by
incrementing (ie, you count in those vectors the occurrence of each
entry). If you attempt to increment something and find it non-zero, it
means that that number is not unique inside that particular row/col/
square, and this you return false.

I probably didn't explain what I mean too well, but it's pretty
simple, and the code should be easy to follow and debug.
Nov 21 '07 #3
"blux" <br******@gmail.comwrote in message
news:ae**********************************@d61g2000 hsa.googlegroups.com...
>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.
My wife spends some of her free time solving these puzzles.
I don't pretend to understand the rules of creating or solving
them, but they do keep her out of my hair when I'm busy. :-)
>
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
false.

Any ideas?
Yes. How would *you* verify the correctness, by hand?

Write out (in English) a detailed procedure.
Test it thoroughly.

Translate the English version to C++.
Test it thoroughly.

If you still get stuck, when you post your code again,
if you want us to help track down where things go wrong,
you must tell *us* exactly the rules the program is using
for validation.

-Mike
Nov 21 '07 #4
"blux" wrote:
>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
false.

Any ideas?
/** Check whether grid[i][j] is valid in the grid */
Shouldn't that be:
/* check whether element i, j is valid in the grid */
bool isValid(int grid[] [9])
{
int i, j;
Shouldn't i and j be parameters to the function?
bool status;
status = true;
Add a comment here telling what the next little blob of code is intended to
do.
for (int column = 0; column < 9; column++)
if (column != j && grid[i] [column] == grid[i] [j])
status = false;
Common practice is to use a break here to immediately return as soon as a
problem is detected.

Don't use the word "valid" when you simply mean "consistent".

<snip>
Nov 21 '07 #5
On Nov 20, 9:48 pm, blux <brluk...@gmail.comwrote:
[snippy]
Any ideas?

/** Check whether grid[i][j] is valid in the grid */
bool isValid(int grid[] [9])
{
int i, j;
bool status;
status = true;

for (int column = 0; column < 9; column++)
if (column != j && grid[i] [column] == grid[i] [j])
status = false;
[snip]

At this point the value of j is what?
Socks
Nov 21 '07 #6
On Nov 21, 3:48 am, blux <brluk...@gmail.comwrote:
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 false.
I wonder about the data representation. When I implemented my
Sudoku solver, I just used a one dimensional array of 81
entries. Plus three different mapping arrays, associating each
entry with a row, a column or a box. I then had an array of
9 bool for each row, column and box, indicating the values
already used.

It worked out very well in practice.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.