Ok so i got part of my code written out but i dont understand how to go about checking all the numbers to see if it is a valid soduku code. i need a function to check the puzzle and make sure it is correct and if it is not then the errors and locations will be displayed. thank you for any help your willing to give me.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;

void readAPuzzle(int grid[] [9]);
void printGrid(int grid[] [9]);


int main()
{
	int grid[9] [9];

		
	readAPuzzle(grid);
	printGrid(grid);

	

	system("pause");
	return 0;
}


void readAPuzzle(int grid[] [9])
{
	ifstream in_f;

	
	in_f.open("soduku.txt");
	
	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])
{
cout<<"Your Puzzle Is Listed Below: "<<endl;
for (int i = 0; i < 9; i++)
{
if ((i)%3 == 0)
cout<<endl<<endl;
for (int j = 0; j < 9; j++)
{
cout << (grid[i] [j]) << " ";

if ((j+1)%3 == 0)
cout<<"   ";
}
cout<<endl;
}
cout<<endl;
}


// CHECK IF VALID PUZZLE
Member Avatar for iamthwee

If I understand it, then all you need to do is go through the horizontal lines make sure all the lines have numbers 1-9 no duplicates, same with vertical lines and 3 x3 squares?

The rules of Sudoku state that a Sudoku puzzle is solved when the 9x9 game board contains the numbers 1 through 9 exactly once in each row, column, and 3x3 box The numbers can appear in any order and diagonals are not considered.

The rules of Sudoku state that a Sudoku puzzle is solved when the 9x9 game board contains the numbers 1 through 9 exactly once in each row, column, and 3x3 box The numbers can appear in any order and diagonals are not considered.

I'd have some boolean functions to check for legality:

bool CheckIfLegal (int grid[][9])
{
     for (int row = 0; row < 9; row++)
     {
          if (!CheckIfLegalHorizontal(grid, row)
               return false;
     }

     for (int col = 0; col < 9; col++)
     {
          if (!CheckIfLegalVertical(grid, col)
               return false;
     }

     for (int row = 0; row < 9; row+=3)
     {
          for (int col = 0; col < 9; col+=3)
          {
               if (!CheckIfLegalRectangle(grid, row, col)
                    return false;
          }
     }

     return true;
}


bool CheckIfLegalHorizontal(int grid[][9], int rowNum)
{
    // returns true if row rowNum is legal, false otherwise
}

bool CheckIfLegalVerical(int grid[][9], int colNum)
{
    // returns true if column colNum is legal, false otherwise
}

bool CheckIfLegalRectangle(int grid[][9], int upLeftX, upLeftY);
{
    // returns true if 3 x 3 grid with upper left coordinate
    // (upperLeftX, upperLeftY) is legal, false otehrwise
}

There are 27 different sets of 9 integers that must pass in order to be legal. If any fail, the function returns false. If they all pass, the function returns true. Each of these functions goes through the relevant nine integers and checks for duplicates. If it finds any, return false. Otherwise return true.

commented: Nice approach. +18

Two years ago, I wrote a peace of junk which managed to solve the puzzle. I lost the code. But I can say what I did.
I had a 9X9 matrix. At the beginning any of the 1-9 numbers can possibly occupy any of the 81 cells. When the user finish entering the constraints. I just iterated a loop over & over again until the answer is found.
The loop checks the following cases for all numbers between 1-9 in all the cells. I will talk with the number 1 for simplicity

1) Is 1 the only possible number that can occupy the cell?
2) Is this the only place where 1 can appear in that column?
3) Is this the only place where 1 can appear in that row?
4) Is this the only place where 1 can appear in that 3x3 box?
and a few more logics.

If any one of the conditions are satisfied then 1 will be eliminated from the possibility list of the other cells in the same column, row & box.

It did work but as I said before it was a crappy code.

But Free source codes are available. Just Google for it and see how many answers you get.

If you want the code to be genuine then you may start with your own logical explanations for the puzzle.

hey, im currently stuck at your problem too, as in im having the same problem. do you have any idea to the question yet? care to share?

So why not take Vernon's advice? It sounds pretty good to me

And as far as testing is concerned.

Each row and column should have a the numbers 1 to 9 in them and they should not repeat. So i guess You can do something like this.

for (int j=0;j<9;j++)
{
int total=0;
for (int 1=0;1<9;i++)
{
total+=sudoku[j][i]
}
if(total!=45)
{
return false;
}
}
return true;

Sameway with the columns i guess.

Just interchange i and j ;

This works because the total of all the numbers will be 45.

U know wat a valid sudoku grid is right? Take a particular position in the grid say (i,j) check the ith row n jth column n check the 3x3 grid in which the particular element is present. if the element is not der either in the row r column or grid its valid. check likewise for every position n well u r done. U can store the errors n the positins maybe in an array n work dat bit out.

Ok so i got part of my code written out but i dont understand how to go about checking all the numbers to see if it is a valid soduku code. i need a function to check the puzzle and make sure it is correct and if it is not then the errors and locations will be displayed. thank you for any help your willing to give me.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;

void readAPuzzle(int grid[] [9]);
void printGrid(int grid[] [9]);


int main()
{
	int grid[9] [9];

		
	readAPuzzle(grid);
	printGrid(grid);

	

	system("pause");
	return 0;
}


void readAPuzzle(int grid[] [9])
{
	ifstream in_f;

	
	in_f.open("soduku.txt");
	
	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])
{
cout<<"Your Puzzle Is Listed Below: "<<endl;
for (int i = 0; i < 9; i++)
{
if ((i)%3 == 0)
cout<<endl<<endl;
for (int j = 0; j < 9; j++)
{
cout << (grid[i] [j]) << " ";

if ((j+1)%3 == 0)
cout<<"   ";
}
cout<<endl;
}
cout<<endl;
}


// CHECK IF VALID PUZZLE
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.