I have found a function on internet here for solving my problem on Sudoku written in C language.I have almost everything done but I am stuck here, I can't check the 3x3 box if any value is duplicated:
/**
* Check if a value contains in its 3x3 box for a cell.
* @param row current row index.
* @param col current column index.
* @return true if this cell is incorrect or duplicated in its 3x3 box.
*/
private boolean containedIn3x3Box(int row, int col, int value) {
// Find the top left of its 3x3 box to start validating from
int startRow = row / 3 * 3;
int startCol = col / 3 * 3;
// Check within its 3x3 box except its cell
for (int i = startRow; i < startRow + 3; i++)
for (int j = startCol; j < startCol + 3; j++) {
if (!(i == row && j == col)) {
if (cells[i][j] == value){
return true;
}
}
}
return false;
}
Up here is the function and I put it in my program here:
int valid(int k, int ii, int jj)
{
int i,start,final,j;
start=ii/3*3;
final=jj/3*3;
for(i = 1; i <= 9; ++i) {
if (i != ii && v[i][jj] == k)
return 0;
if (i != jj && v[ii][i] == k)
return 0;
}
for(i=start;i<=start+3;i++)
for(j=final;j<=final+3;j++)
{
if(!(i==ii && j==jj))
{
if(v[i][j]==k)
return 0;
}
}
return 1;
}
I have read on the website where I took this function and tried to understand the program. The program compares values with value part. My loop is not working well.