Вы находитесь на странице: 1из 5

#include <stdio.h>#include <stdlib.h> #include <time.

h> #define size //function 1 int obeysSudokuRules(int a[][size]) { int count[10]; int i; int r; int c; //r: row, c: column for ( r=0; r<size; r++) //start from 1st row checking colums { for( c=0; c<size; c++) { count[a[r][c]]++; if ( (count[a[r][c]]) > 1) return 0; } for (i=0; i<size; i++) { count[i]=0; } // to clear }//end row loop for (i=0; i<size; i++) { count[i]=0; } // to clear //start from 1st col checking rows for ( c=0; c<size; c++) { for( r=0; r<size; r++) { count[a[r][c]]++; if ( (count[a[r][c]]) > 1) return 0; } for (i=0; i<size; i++) { count[i]=0; } // to clear }//end col loop for (i=0; i<size; i++) { count[i]=0; } // to clear //z is No. of grids r=0; c=0; for ( z=0; z<size; z++) { int k=0; //No. of rows in a grid int j=0; // No. of columns in a grid while (k<3) { while(j<3) { count[a[r][c]]++; if ( (count[a[r][c]]) > 1) return 0; c++; j++; } //end of j loop r++; k++; } // end of k loop if(z<2)

{ r=0; } if(z==2) { c=0; r=3; } if((z>2)&&(z<5)) { r=3; } if(z==5) { c=0; r=6; } if(z>5) { r=6; } for (i=0; i<size; i++) { count[i]=0; } // to clear & end z loop return 1; } // end grid loop //function 2 void printGrid(int a[][size]) { int r, c; for(r=0; r<size; r++) { for(c=0; c<size; c++) { if ((a[r][c])== 0) { printf(" "); } else { printf("%d",a[r][c]); } printf("\n"); } } } //function 5 &4 void getRowNum(void) { int row; printf(" please enter the row number that you want to enter the value\n"); scanf("%d",row); while ((row <0) || (row>8)) { printf(" please enter the row number that you want to enter the value"); scanf("%d",&row); } } int getCloumnNum(void) { int column; printf(" please enter the column number that you want to enter the value"); scanf("%d",&column);

while ((column<0) || (column>8)) { printf(" please enter the row number that you want to enter the value"); scanf("%d",column); } return column; } void fillZeros (int a[][size]) { int counter=0; int r,rand; for(r=0; r<size; r++) { while (counter<=4) { rand=rand()%8; a[r][rand]=0; counter++; } } } int getValue(void) { int value; printf("\nPlease Enter the value: "); scanf("%d",&value); return value; }

int compSolve(int a[][size]) { int r = 0; int c = 0; int checkZero; while(r<9) { while(c<9) { if((a[r][c])==0) checkZero = 0; else checkZero = 1; c++; } r++; } return checkZero; }

void genPartialGrid(int a[][size]) { int obeysRules; do { for(r=0;r<size;r++) { for(c=0;c<size;c++) { a[r][c]={0}; }//Fill array a with all zeros } obeysRules = genGrid(a, 0, 0); } while (obyesRules == 0); fillZeros(a); } int genGrid(int a[][size], int row, int column) { int i, r; if (row == size) return 1; int currentRow[size]={1,2,3,4,5,6,7,8,9}; for(i=0;i<size-1;i++) { r=i+rand()%8; swap=currentRow[i]; currentRow[i]=currentRow[r]; currentRow[r]=swap; } for(i=0;i<size;i++) { a[row][column] = currentRow[i]; if (obeysSudokuRules==1) { if (column == size -1) obyesRules = genGrid(a, row + 1, 0); else obyesRules = genGrid(a, row, column + 1) if(obyesRules) return 1 ; } // close inner if statement } // close for loop a[row][column] = 0; return 0 ; } int main(void) { int a[9][9]; int r,c, value, obey, solve;

printf("This a sudoku Game.All numbers in rows and columns must not be repeated" ); srand(time(NULL)); //to randomize random number generation genPartialGrid(a); //call function genPartialGrid to generate a partially fi lled grid that does not violate Sudoku rules printGrid(a); do { r = getRowNum(); ber c = getColumnNum(); mn number value = getValue(); a[r][c] = value; n positions entered printGrid(a); obey = obeysSudokuRules; solve = compSolve; //call function printGrid

//call function getRowNum to determine the row num //call function getColumnNum to determine the colu //call function to get the value (between 1 and 9) //enter the value in the grid at the row and colum //call function printGrid

} while ((obey ==1) && (solve == 0)); if ((solve == 1) && (obey == 1)) { printf("congratulation,you have solve the puzzle"); printGrid(a); } else /* it means Sudoku rules are not all obey ed */ { printf(" Sorry, the Solution is incorrect"); } print(" thank you, good bye"); return 0; }

Вам также может понравиться