sudoku validity checker

B

blux

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[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 [column] == grid [j])
status = false;

for (int row = 0; row < 9; row++)
if (row != i && grid[row] [j] == grid [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 [j])
status = false;

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

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


return status;

}
 
V

Victor Bazarov

blux said:
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
 
I

Ioannis Gyftos

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

Mike Wahler

blux said:
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
 
O

osmium

blux said:
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[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 [column] == grid [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>
 
P

Puppet_Sock

Any ideas?

/** Check whether grid[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 [column] == grid [j])
status = false;

[snip]

At this point the value of j is what?
Socks
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top