Hello all!

New here at daniweb.

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:

/** 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;

}

Any ideas?

Recommended Answers

All 8 Replies

Hello all!

New here at daniweb.

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:

...

Any ideas?

Maybe finish the program, I suppose... :-/

Well of course...

This is just a function of the program.

My main question is how to check the 9x9 array to determine if it is a valid puzzle.

Here is the program:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
void readAPuzzle(int grid[] [9]);
bool search(int grid[] [9]);
void printGrid(int grid[] [9]);
bool isValid(int grid[] [9]);

int main()
{
	// Read a Sudoku puzzle
	int grid[9] [9];
	
	readAPuzzle(grid);
	
	printGrid(grid);
	
	if (isValid(grid))
    cout << "Is a sudoku puzzle. " << endl;
	
	else 
	cout << "Is not a sudoku puzzle. " << endl;
	
	return 0;
}

//Reads Puzzle into 9x9 array ( modify to read from file )
void readAPuzzle(int grid[] [9])
{
	ifstream in_f;
	
	in_f.open("ma1.dat");
	
	for (int i = 0; i < 9; i++)
	for (int j = 0; j < 9; j++)
	in_f >> grid[i] [j];
}

//Prints out the puzzle read from file (keep)
void printGrid(int grid[] [9])
{
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
			cout << grid[i] [j] << " ";
		cout << endl;
	}
}
/** 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;

}

Well of course...

This is just a function of the program.

My main question is how to check the 9x9 array to determine if it is a valid puzzle.

Oh, I get it. That was not a question in your previous post.

It looks to me like you are already checking the 9x9 array in isValid() so I don't understand exactly what you need help with. Please read the post Read Me: Read This Before Posting so you can ask exactly what you need to know in a way we can help you.

I 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."

I figured that the code I posted could be fixed some how. My apologies for not submitting a proper question.

The problem I have run into is that the program does not know the difference from a valid and invalid puzzle.

I have several solved puzzles that I am in inputting, and several that are just random numbers. The isValid() function is supposed to check for this very occurrence. However it is not.

I 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."

That's not a question. That is a statement. No one can tell if there is a problem or not.

I figured that the code I posted could be fixed some how. My apologies for not submitting a proper question.

It probably can. But you never mentioned what the problem is.

The problem I have run into is that the program does not know the difference from a valid and invalid puzzle.

That's obvious if you posted here. The problem is you aren't explaining what nor where the problem is. Is it ALL your tests? Just one? We didn't write it. We can't run it. You have to tell us what's wrong.

I have several solved puzzles that I am in inputting, and several that are just random numbers. The isValid() function is supposed to check for this very occurrence. However it is not.

Why not? What's going wrong? What area is it in? Did you output values at key places to figure out where the problem might be?

In other words, read the link, ask a proper question.

If I knew what was going wrong I wouldn't have asked the question or made the post.


My function is supposed to check whether or not a given input of a 9x9 matrix is a valid and solvable puzzle; that is, whether or not each row, column, and region in the matrix contains each value from 1 to 9 exactly once.

I have attempted to write the code that will do this, however for some reason my code is not working as it should. It is not checking whether or not a given input of a 9x9 matrix is a valid and solvable puzzle; that is, whether or not each row, column, and region in the matrix contains each value from 1 to 9 exactly once.

Fine. If you can't tell us anything, run this version of your program. Maybe then it can give you a clue how to figure out what is happening.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
void readAPuzzle(int grid[] [9]);
bool search(int grid[] [9]);
void printGrid(int grid[] [9]);
bool isValid(int grid[] [9]);

int main()
{
	// Read a Sudoku puzzle
	int grid[9] [9];
	
	readAPuzzle(grid);
	
	printGrid(grid);
	
	if (isValid(grid))
    cout << "Is a sudoku puzzle. " << endl;
	
	else 
	cout << "Is not a sudoku puzzle. " << endl;
	
	return 0;
}

//Reads Puzzle into 9x9 array ( modify to read from file )
void readAPuzzle(int grid[] [9])
{
	ifstream in_f;
	
	in_f.open("ma1.dat");
	
	for (int i = 0; i < 9; i++)
	for (int j = 0; j < 9; j++)
	in_f >> grid[i] [j];
}

//Prints out the puzzle read from file (keep)
void printGrid(int grid[] [9])
{
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
			cout << grid[i] [j] << " ";
		cout << endl;
	}
}
/** 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;
cout << "1st test: " << status << endl;

	for (int row = 0; row < 9; row++)
		if (row != i && grid[row] [j] == grid[i] [j])
			status = false;
cout << "2nd test: " << status << endl;
	
	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;
cout << "3rd test: " << status << endl;
	
	for (int i = 0; i < 9; i++)
		for (int j = 0; j < 9; j++)
			if (grid[i][j] != 0)
				status = false;
cout << "4th test: " << status << endl;
	
	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;
cout << "5th test: " << status << endl;

	
	return status;

}

Look at the output and you should be able to tell which loop is working improperly.

It appears that the line:

if (column != j && grid[i] [column] == grid[i] [j])

is incorrect.

How can I fix it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.