Hello Everyone,
I am working on a program to solve sudoku puzzle. I have written all the code and the logic seems correct. I had some people look at the other functions and I have verified that they are correct. The solve functions is running infinitely and I can't figure out why. The functions called in the inner for loops are all correct. Please help me figure this out.
(This is just for fun not any type of school assignment. I just wanted to test my programming ability and expand my C++ knowledge.)
Thanks for the help.
I am working on a program to solve sudoku puzzle. I have written all the code and the logic seems correct. I had some people look at the other functions and I have verified that they are correct. The solve functions is running infinitely and I can't figure out why. The functions called in the inner for loops are all correct. Please help me figure this out.
(This is just for fun not any type of school assignment. I just wanted to test my programming ability and expand my C++ knowledge.)
Code:
#include <iostream>
#include <cmath>
using namespace std;
void ReadData(int LocalPuzzle[][9],int Size);
void PrintData(int LocalPuzzle[][9],int Size);
bool CheckColumn(int number, int col, int LocalPuzzle[][9]);
bool CheckRow(int number, int row, int LocalPuzzle[][9]);
bool CheckSubSquare(int number, int row, int col, int LocalPuzzle[][9]);
int SolvePuzzle(int LocalPuzzle[][9]);
int main(int argc, char** argv)
{
const int ArraySize(9);
int Puzzle[ArraySize][ArraySize];
ReadData(Puzzle, ArraySize);
PrintData(Puzzle, ArraySize);
cout << "\n\n\n\n";
SolvePuzzle(Puzzle);
cout<< "\n\n";
PrintData(Puzzle, ArraySize);
cout << endl;
//Test functions to see if check methods are working
cout<< CheckColumn(2, 0, Puzzle)<<endl;
cout<< CheckRow(3, 0, Puzzle)<<endl;
if( CheckSubSquare(3, 2, 2, Puzzle)== true){
cout<<"true test"<< endl;
}
return 0;
}
void ReadData(int LocalPuzzle[][9], int Size)
{
for(int row = 0; row < Size; row++ )
{
for(int col = 0; col < Size; col++)
{
cin >> LocalPuzzle[row][col];
}
}
}
void PrintData(int LocalPuzzle[][9], int Size)
{
for(int row = 0; row < Size; row++)
{
for(int col = 0; col < Size ; col++)
{
cout << LocalPuzzle[row][col] << " ";
}
cout << endl;
}
}
bool CheckColumn(int number, int col, int LocalPuzzle[][9]){
//cout << "Entering loop for chco " << returnval << endl;
for(int i = 0; i < 9; i++){
if(LocalPuzzle[i][col]== number){
return 1;
}
return 0;
}
}
bool CheckRow(int number, int row, int LocalPuzzle[][9]){
//cout << "Entering loop for chrow " << returnval << endl;
for(int i = 0; i < 9; i++){
if(LocalPuzzle[row][i]== number){
return 1;
}
return 0;
}
}
bool CheckSubSquare(int number, int row, int col, int LocalPuzzle[][9]){
int rows = (row/3)*3;
int cols = (col/3)*3;
//cout << "Entering loop for chsq " << returnv << endl;
for(int i = rows; i < row+3; i++){
for(int j = cols; j < cols+3; j++){
if(LocalPuzzle[i][j] == number){
return 1;
// cout << " New V " << returnv << endl;
}return 0;
}
}
}
int SolvePuzzle(int LocalPuzzle[][9]){
int temponly = 0;
int answers = 0;
bool whilerun = 1;
while(whilerun)
{
whilerun = 0;
cout << "While : " << whilerun;
for(int row=0; row<9; row++)
{
for(int col=0; col<9; col++)
{
int position= LocalPuzzle[row][col];
//cout << "Post : " << position;
if(position == 0)
{
//int numOfAns= 0;
//cout << " In post if : " << endl;
for(int i= 1; i<=9; i++)
{
//cout << CheckSubSquare(i,row,col,LocalPuzzle);
if((CheckColumn(i, col, LocalPuzzle) == false) && (CheckRow(i, row, LocalPuzzle) == false) && (CheckSubSquare(i, row, col, LocalPuzzle) == false))
{
cout << " Num : " << whilerun;
temponly = LocalPuzzle[row][col];
answers++;
cout << "Answers : " << answers;
}
if(answers == 1)
{
//cout << "Puzzle Loc " <<LocalPuzzle[row][col];
LocalPuzzle[row][col] = answers;
whilerun = 1;
answers = 0;
}
}
}
}
}
}
Thanks for the help.