473,237 Members | 1,398 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,237 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 11829
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,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.