470,850 Members | 1,022 Online

# 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 10903
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
>
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?
[..]
V
--
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
>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

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 discussion thread is closed

Replies have been disabled for this discussion.