Sudoku Solve Function C++

Status
Not open for further replies.

bpm55

In Runtime
Messages
248
Location
SC
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.)

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.
 
First, when posting code, use code tags so that your formatting is not lost.
Second, post all the code -- regardless of what anyone else has told you about its correctness.

As for why it's running indefinitely, one possible reason is that whilerun is never false at the time it is evaluated. However, for all I know, the CheckColumn, CheckRow, or CheckSubSquare functions could be part of the problem. As a general suggestion, I would recommend restructuring your program and removing the while loop. Also, you should probably be able to identify your problem easily by stepping through the code with a debugger.
 
Status
Not open for further replies.
Back
Top Bottom