//function to recursively solve sudoku oldgrid by guessing and backtracking int guess(char oldgrid[9][9][10]) { char i,j,k; for(i=0;i<9;++i) for(j=0;j<9;++j) if(!oldgrid[i][j][0]) for(k=1;k<10;++k) if(oldgrid[i][j][k]) { char newgrid[9][9][10]; //guessing guessed(oldgrid,newgrid,i,j,k); //solving according to guess solve(newgrid); //checking if solved if(solved(newgrid)) { output(newgrid); return 1; } //guessing recursively if(guess(newgrid)) return 1; //eliminating guess oldgrid[i][j][k]=0; solve(oldgrid); } return 0; }
int main(void) {/************/ int number, guess; char play_again = 'y'; srand(time(NULL)); while (play_again == 'y') { number = 1 + rand() % 1000; /* This printf should be cancelled once bug is fixed */ printf("%d\n", number); printf("I have a number beetween 1 and 1000\n" "Can you guess my number?\n" "Please type your first guess: "); scanf("%d", &guess); while (guessed(number, guess) == 0) scanf("%d", &guess); printf("Would you like to play again? (y o n)"); /* This printf should be cancelled once bug is fixed */ printf("\n%c\n", play_again); /* This second scanf could fall in the classic problem of newline left * sitting in the buffer after (first) scanf(). Please take a * look at: */ /* + http://cboard.cprogramming.com/showthread.php?s=c85a7ea9a4048feb99ddadb1c380a734&t=95886 */ /* + http://www.phanderson.com/C/scanf.html */ scanf("\n%c", &play_again); /* Notice \n before %c */ } return 0; }