I am coding a Sudoku puzzle generator but all i see is a blank console terminal when i compile and run the program. I have been waiting for 1 hr but its still a blank console terminal. I am wondering if its because of any logical error OR because it is still processing. Just a heads up, my entire code is super long. How do i optimize it if its a performance issue
Thanks
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
bool checkrow(int row,int value,int array[][9]);
void producearray(int array[][9]);
bool checksquare(int row,int col,int value,int array[][9]);
bool checkcol(int col,int value,int array[][9]);
void populatearray(int array[][9]);
void printarray(int array[][9]);
int main()
{
int array[9][9];
populatearray( array);
producearray(array);
printarray(array);
system("PAUSE");
}
bool checkrow(int row,int value,int array[][9])// checks the entire row, returns false if any two numbers in the row are the same
{
for (int j=0;j<9;j++)
{
if (value==array[row][j])
{
return false;
}
}
return true;
}
bool checkcol(int col,int value,int array[][9]) // check if any two numbers in the same column are the same
{
for (int j=0;j<9;j++)
{
if (value==array[j][col])
{
return false;
}
}
return true;
}
bool checksquare(int row,int col,int value,int array[][9]) //checks if the number within the same square are the same
{
if ( ( row>=0 && row<=2) && (col>=0 && col<=2) )
{
for (int i=0;i<=2;i++)
{
for (int j=0;j<=2;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=0 && row<=2) && (col>=3 && col<=5) )
{
for (int i=0;i<=2;i++)
{
for (int j=3;j<=5;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=0 && row<=2) && (col>=6 && col<=8) )
{
for (int i=0;i<=2;i++)
{
for (int j=6;j<=8;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=3 && row<=5) && (col>=0 && col<=2) )
{
for (int i=3;i<=5;i++)
{
for (int j=0;j<=2;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=3 && row<=5) && (col>=3 && col<=5) )
{
for (int i=3;i<=5;i++)
{
for (int j=3;j<=5;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=3 && row<=5) && (col>=6 && col<=8) )
{
for (int i=3;i<=5;i++)
{
for (int j=6;j<=8;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=6 && row<=8) && (col>=0 && col<=2) )
{
for (int i=6;i<=8;i++)
{
for (int j=0;j<=2;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=6 && row<=8) && (col>=3 && col<=5) )
{
for (int i=6;i<=8;i++)
{
for (int j=3;j<=5;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
else if ( ( row>=6 && row<=8) && (col>=6 && col<=8) )
{
for (int i=6;i<=8;i++)
{
for (int j=6;j<=8;j++)
{
if (array[i][j]== value)
{
return false;
}
}
}
return true;
}
}
void producearray(int array[9][9]) //produces the array
{
bool isrow;
bool iscol;
bool issquare;
for (int i=0;i<9;i++)
{
for (int j=0;j<9;j++)
{
do
{
array[i][j]=rand()%9+1;
isrow=checkrow(i,array[i][j],array);
iscol=checkcol(j,array[i][j],array);
issquare=checksquare(i,j,array[i][j],array);
}
while(isrow==false || iscol==false || issquare==false);
}
}
}
void populatearray(int array[][9]) // populate the arary
{
for (int i=0;i<9;i++)
{
for (int j=0;j<9;j++)
{
array[i][j]=0;
}
}
}
void printarray(int array[][9]) //prints the array
{
for (int i=0;i<9;i++)
{
for (int j=0;j<9;j++)
{
cout<<array[i][j]
<<"\t";
}
cout<<endl;
}
}
You are doing it wrong. Your code tries to generate a valid sudoku by randomizing every single field, one by one, checking if there is a collision. This most likely leads to the problem that when a part of the sudoku is already filled, there is no valid solution anymore. Such a deadlock happens all the time. The exising numbers do not conflict, but there is no way to fill the rest. Just take any sudoku from a newspaper and randomly fill a few fields so that they do not immediately conflict. Most likely, you won’t be able to complete the sudoku any more.
This problem is what makes a sudoku a puzzle, and your program ignores it – so it must fail (unless the random generator is really, really, really lucky today).
A much better way to generate a sudoku is to start with a known valid sudoku, than swap rows and columns (always within a 3 line/column block), and whole blocks of three lines / columns, and swap numbers (e.g. replace every 3 with 7 and concurrently every 7 with 3) so that every swap keeps the whole sudoku valid.