hi guys
i still dont get how to correct my coding i know its a minor problem.
could u please point out where i have gone wrong, and explain where how
i can solve it.
code-------------------------------------------------------
#include<iostream.h>
#include<iomanip>
#include<cstdlib>
#include<string> //defining necessary library
#include<cassert>
#include<time.h>
using namespace std;
const int miniGridSize=3;
const int GridSize=9; //initlialisation
int newGrid[GridSize][GridSize]={
{0,6,0,1,0,4,0,5,0},
{0,0,8,3,0,5,6,0,0},
{2,0,0,0,0,0,0,0,1}, //initialization of the values.
{8,0,0,4,0,7,0,0,6},
{0,0,6,0,0,0,3,0,0},
{7,0,0,9,0,1,0,0,4},
{5,0,0,0,0,0,0,0,2},
{0,0,7,2,0,6,9,0,0},
{0,4,0,5,0,8,0,7,0}};
void print(); //function print
//void sudoku_shuffle(int[9][9]);
void makeNewGrid(int[9][9]);
void fillsquare(int depth,int x,int y); //function declarations
bool isRowLegal(int);
bool isColLegal(int);
bool isMiniGridLegal(int,int);
bool checkForDuplicate(int[9]);
bool isLegal();
void generation(int[9][9]);
int main()
{
srand((unsigned)time(0)); //initialize random generator;
int num,x,y;
char response; //declaration of variables.
option:
cout<<"Menu for sudoku:"<<endl;
cout<<"----------------"<<endl; //defining menu option
cout<<"(0)print the original grid"<<endl;
cout<<"(1)print the possible generation of numbers(NOTE->WORK
PARTIALLY)"<<endl;
cout<<"(2)test validity of numbers(row/column/minigrid"<<endl;
cout<<"(3)exit the program"<<endl;
cout<<endl;
int choice;
cout<<"enter choice:"<<endl; //get user input
cin>>choice;
switch(choice) //switch case
{
case 0:
cout<<"original grid"<<endl<<endl;
print();
cout<<"sudoku not solved"<<endl;
goto option;
break;
case 1:
generation(newGrid); //calling generation function
print(); //calling print()
goto option; //goto option menu
break;
case 2:
cout<<"testing row/column/minigrid for validity of numbers"<<endl;
makeNewGrid(newGrid); //calling clear grid()
do{
cout<<"enter number:="<<endl; //geting user input number,x and
y cordinates
//for testing row/column/block
cin>>num;
cout<<"x cordinate:"<<endl;
cin>>x;
cout<<"y cordinate:"<<endl;
cin>>y;
fillsquare(num,x,y);
if(isLegal()) //validation of row.col.block
{
print(); //if valid print grid and display
cout<<"sudoku solved"<<endl;
}else
{
cout<<"not solved"<<endl;
newGrid[x][y]=0; //if invalid display and empty
specific sqr
}
cout<<"do you test numbers"<<endl<<endl;
cin>>response; //user input
}while(response=='Y'||'y');
goto option;
break;
case 3:
exit(0); //exit program
break;
default:
cout<<"wrong choice:"<<endl; //default option
goto option;
}
return 0;
}
/******************************************************************************************/
///function got the
problem////////////////////////////////////////////////////////////////
void generation(int newGrid[9][9]) //generation function with arg
{
int randV; //initialise variables
int r,c;
for(int count=0;count<81;count++) //run through loop 81 times
{
for( r=0;r<9;r++) //row
{
for(c=0;c<9;c++) //col
{
if(newGrid[r][c]==0) //if grid not full and empty sqrs exist
{
randV=rand()%9+1;
newGrid[r][c]=randV; //inialize a random num
if(!isLegal())//if not valid
{
newGrid[r][c]=0; //make it 0
}
}
}
}
}
}
/*****************************************************************************************/
bool checkForDuplicate(int arr[9]) //duplicate function bool
{
bool foundNoDuplicate=true; //make var true
bool tally[GridSize+1];
int x,i; //declaration of variable
for(i=0;i<=9;i++)
tally=false; //initialize the array variable to false
for(i=0;i<9;i++)
{
x=arr; //return arr numbers to variable called x for checking
if(x!=0) //ignore empty squares
{
if(tally[x]) //
foundNoDuplicate=false; //if found duplicate make it true
tally[x]=true; //make bool array true
}
}
return foundNoDuplicate;
}
bool isLegal()
{
bool rowLegal=true;
bool colLegal=true;
bool miniGridLegal=true;
int i;
for(i=0;i<GridSize;i++)
{
rowLegal=rowLegal && isRowLegal(i);
}
for(i=0;i<GridSize;i++)
{
colLegal=colLegal && isColLegal(i);
}
for(i=0;i<GridSize;i++)
{
miniGridLegal=miniGridLegal && isMiniGridLegal(3*(i/3),3*(i%3));
}
return rowLegal && colLegal && miniGridLegal; //if all vaues are true
then return true
}
bool isMiniGridLegal(int x,int y) //checking for bllock
validation
{
int miniGrid[9];
int offset;
for(offset=0;offset<GridSize;offset++)
{
miniGrid[offset]=newGrid[x+offset/miniGridSize][y+offset%miniGridSize];
//assigning valeues to minigrid arry(checking horizontally and
vertically in the minigrid(
}
bool legal=checkForDuplicate(miniGrid); //checking for duplicate with
in array
if(!legal){
cout<<"duplicate values in minigrid="<<x<<" , "<<y<<endl;
}
return legal;
}
bool isRowLegal(int y) //checking for row validation
{
int row[9]; //create a rray of 9
int x;
for(x=0;x<9;x++)
{
row[x]=newGrid[x][y]; //put each row value to array
}
bool legal=checkForDuplicate(row); //check for duplicate within array
and return result
if(!legal){ //if not valid display-
cout<<"there are duplicate values in col="<<y<<endl;
}
return legal;
}
bool isColLegal(int x) //checking for col validation
{
int col[9]; //create a array of 9
int y;
for(y=0;y<9;y++)
{
col[y]=newGrid[x][y]; //put each col values to array
}
bool legal=checkForDuplicate(col); //check for the duplicates within
col array and return the result to legal variable
if(!legal){ //if not legal display
cout<<"There are duplicate values in row="<<x<<endl;
}
return legal;
}
void fillsquare(int digit,int x,int y) //fill square function with arg
{
if((0<=x) && (x<9) && (0<=y) && (y<9)) //checking for valid grids and
insert the number
{
newGrid[x][y]=digit; //to specific grid
}
}
void makeNewGrid(int newGrid[GridSize][GridSize]) //clear grid
function
{
int i,j;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
newGrid[j]=0;
}
}
}
/*
void sudoku_shuffle(int newGrid[GridSize][GridSize]) //function for
shuffle sudoku
{
srand((unsigned)time(0));
//position=0;
int row,col;
int randValue=0;
for(row=0;row<9;row++)
{
for(col=0;col<9;col++)
{
randValue=rand()%9+1;
if(newGrid[row][col]==0)
{
// if((0<randValue) &&(randValue<=GridSize))
newGrid[row][col]=randValue;
}
}
}
}
*/
void print() //function for the print
{
for(int row=0;row<GridSize;row++)
{
for(int col=0;col<GridSize;col++)
{
if(newGrid[row][col]==0) //if empty grids display "_"
cout<<"["<<"_"<<"]";
else
cout<<"["<<newGrid[row][col]<<"]"; //else print numbers
}
cout<<endl;
}
}