Question

Currently I'm having trouble with getting my code to work in which it checks the sudoku board after its been assigned a new value to a not slot and then see if its still block valid or not.

Currently even if I put in a correct input this segment of code is still telling me that my block is not valid.

The tempsudoku board is basically a sudoku board with a 0s random slot representing blanks. Could someone see if I am doing something wrong with my code?

int truefalse=1;
printf("Enter a row (1-9), column (1-9) and number (1-9), separated by spaces: ");
scanf("%d %d %d", &row, &column, &number);

tempsudoku[row-1][column-1]=number;

for(i=0;i<=2;i++)
{
    for(j=0;j<=2;j++)
    {
        int check[9]={0};
        for(x=0;x<=(j*3)+2;x++)
        {
            for(y=0;y<=(i*3)+2;y++)
            {
                for(k=0;k<=8;k++)
                {
                    if(tempsudoku[x][y]==k+1)
                    {
                        check[k]++;
                    }
                }
            }
        }

        for(k=0;k<=8;k++)
        {
            if(check[k]>=2)
            {
                printf("Error: repeats in board\n");
                truefalse=0;//basically means  its false
            }
        }
    }
}
Était-ce utile?

La solution

for(x=0;x<=(j*3)+2;x++){
   for(y=0;y<=(i*3)+2;y++){

should probably be

for(x=3*j;x<=(j*3)+2;x++){
   for(y=i*3;y<=(i*3)+2;y++){

to loop over the nine cells of one block.


Note that you can simplify

for(k=0;k<=8;k++){
    if(tempsudoku[x][y]==k+1){
        check[k]++;
    }
}

to

k = tempsudoku[x][y];
if (k >= 1)
    check[k-1]++;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top