Code:
#include <stdio.h>
#include <stdlib.h>
void draw_board(void);
void entered_board();
void enter_board();
int outputArray[9][9];
int sudoku [9][9];
char *str_blank_board [201] = {"A", "B","C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1", "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2", "A3", "B3", "C3"};
int input_value(int x, int y, int value){
int i,j;
for (i = 0; i < 9; i++) {
if (value == outputArray[i][y] || value == outputArray[x][i]) {
return 0;
}
}
// Scan its own square
if (x < 3) {
if (y < 3) {
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
} else if (y < 6) {
for (i = 0; i < 3; i++) {
for (j = 3; j < 6; j++) {
if (value == outputArray[i][j]) {
return 0; }
}
}
} else {
for (i = 0; i < 3; i++) {
for (j = 6; j < 9; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
}
} else if (x < 6) {
if (y < 3) {
for (i = 3; i < 6; i++) {
for (j = 0; j < 3; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
} else if (y < 6) {
for (i = 3; i < 6; i++) {
for (j = 3; j < 6; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
} else { for (i = 3; i < 6; i++) {
for (j = 6; j < 9; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
}
} else {
if (y < 3) {
for (i = 6; i < 9; i++) {
for (j = 0; j < 3; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
} else if (y < 6) {
for (i = 6; i < 9; i++) {
for (j = 3; j < 6; j++) {
if (value == outputArray[i][j]) {
return 0;
}
}
}
} else {
for (i = 6; i < 9; i++) {
for (j = 6; j < 9; j++) {
if (value == outputArray[i][j]) {
return 0;
} }
}
}
}
return value;
}
int backtrack(int x, int y) {
int temp,i,j;
if (outputArray[x][y] == 0) {
for (i = 1; i < 10; i++) {
temp = input_value(x,y,i);
if (temp > 0) {
outputArray[x][y] = temp;
if (x == 8 && y == 8) {
return 1;
} else if (x == 8) {
if (backtrack(0,y+1)) return 1;
} else {
if (backtrack(x+1,y)) return 1 ;
}
}
}
if (i == 10) {
if (outputArray[x][y] != sudoku[x][y]) outputArray[x][y] = 0;
return 0;
}
} else {
if (x == 8 && y == 8) {
return 1;
} else if (x == 8) { if (backtrack(0,y+1)) return 1;
} else {
if (backtrack(x+1,y)) return 1;
}
}
}
int main(void)
{
int i;
int j;
char correct = '\0';
int board_space = 0;
int board_space_A1C3 = 0;
int sudoku_space = 0;
int sudoku_space_R = 0;
int sudoku_protect[9][9];
char blank_board [81];
printf("this program will solve any sudoku puzzle you enter\n\n");
printf("here is the board...\n\n\n");
draw_board();
enter_board();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("here is the board you entered\n\n");
printf("sudoku [8][8] %d\n\n", sudoku[8][8]);
printf("sudoku [8][7] %d\n\n", sudoku[8][7]);
printf("sudoku [7][8] %d\n\n", sudoku[7][8]);
entered_board(sudoku);
do{//loop to see if board is right
printf("is this board correct?\n");
printf("y/n\n");
do
{
scanf("%c", & correct);
}while (correct == '\n');
switch (correct)
{
case 'n':
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");//clear screen
printf("well then, let's begin the entering process again\n\n\n");
draw_board();//put board on screen
enter_board();//get input values
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("here is the board you entered\n\n");
entered_board(sudoku);
break;
}
case 'N':
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");//clear screen
printf("well then, let's begin the entering process again\n\n\n");
draw_board();//put board on screen
enter_board();//get input values
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("here is the board you entered\n\n");
entered_board(sudoku);
break;
}
case 'y':
{
printf("\n\n");
printf("good, lets continue\n");
break;
}
case 'Y':
{
printf("\n\n");
printf("good, lets continue\n");
break;
}
default:
{
printf("please enter 'y' or 'n'\n\n");
break;
}
}//END SWITCH
}while ((correct == 'n') || (correct == 'N') || (correct =! 'y') || (correct =! 'Y'));//end do loop
printf("OK! its time to get serious!!!!!\n");
printf("");
for (i = 0; i < 9; i++) {//transfer values of sudoku to output array
for (j = 0; j < 9; j++) {
outputArray[i][j] = sudoku[i][j];
}
}
if (backtrack(0,0)) {
printf("Soln is :\n");//prints solution
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
printf("%d ", outputArray[i][j]);
}
printf("\n");
}
} else{
printf("I don't know what you did, but there is no solution, sorry...\n");
}
printf("%d", sudoku[2][3]);
return 0;
}
void draw_board (void)
{
printf("A B C D E F G H I\n");
printf("J K L M N O P Q R\n");
printf("S T U V W X Y Z A1\n\n");
printf("B1 C1 D1 E1 F1 G1 H1 I1 J1\n");
printf("K1 L1 M1 N1 O1 P1 Q1 R1 S1\n");
printf("T1 U1 V1 W1 X1 Y1 Z1 A2 B2\n\n");
printf("C2 D2 E2 F2 G2 H2 I2 J2 K2\n");
printf("L2 M2 N2 O2 P2 Q2 R2 S2 T2\n");
printf("U2 V2 W2 X2 Y2 Z2 A3 B3 C3\n\n");
printf("please enter the numbers on your board to their corresponding location\non the board on the screen. If no value enter a zero (0)\n\n");
return;
}
void entered_board()
{
int sudoku_endl = 0;
int sudoku_space_void = 0;
for(sudoku_space_void = 0; sudoku_space_void<=8; sudoku_space_void++)
{
printf("%d", sudoku[sudoku_space_void][0]);
printf(" %d", sudoku[sudoku_space_void][1]);
printf(" %d", sudoku[sudoku_space_void][2]);
printf(" %d", sudoku[sudoku_space_void][3]);
printf(" %d", sudoku[sudoku_space_void][4]);
printf(" %d", sudoku[sudoku_space_void][5]);
printf(" %d", sudoku[sudoku_space_void][6]);
printf(" %d", sudoku[sudoku_space_void][7]);
printf(" %d\n", sudoku[sudoku_space_void][8]);
sudoku_endl = sudoku_endl + 9;
if ((sudoku_endl == 27) || (sudoku_endl == 54) || (sudoku_endl == 81))
{
printf("\n");
}//end if
}//end for loop
return;
}
void enter_board()
{
int sudoku_space = 0;
int board_space = 0;
for (sudoku_space = 0; sudoku_space <=8; sudoku_space++)//get values for the first time
{
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][0]);
do{
if ((sudoku[sudoku_space][0]>9) ||(sudoku[sudoku_space][0]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][0]);//re-enter value
}
}while((sudoku[sudoku_space][0]>9) ||(sudoku[sudoku_space][0]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
///////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][1]);
do{
if ((sudoku[sudoku_space][1]>9) ||(sudoku[sudoku_space][1]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][1]);//re-enter value
}
}while((sudoku[sudoku_space][1]>9) ||(sudoku[sudoku_space][1]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][2]);
do{
if ((sudoku[sudoku_space][2]>9) ||(sudoku[sudoku_space][2]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][2]);//enter value
}
}while((sudoku[sudoku_space][2]>9) ||(sudoku[sudoku_space][2]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][3]);
do{
if ((sudoku[sudoku_space][3]>9) ||(sudoku[sudoku_space][3]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][3]);//re-enter value
}
}while((sudoku[sudoku_space][3]>9) ||(sudoku[sudoku_space][3]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][4]);
do{
if ((sudoku[sudoku_space][4]>9) ||(sudoku[sudoku_space][4]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][4]);//re-enter value
}
}while((sudoku[sudoku_space][4]>9) ||(sudoku[sudoku_space][4]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][5]);
do{
if ((sudoku[sudoku_space][5]>9) ||(sudoku[sudoku_space][5]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][5]);//re-enter value
}
}while((sudoku[sudoku_space][5]>9) ||(sudoku[sudoku_space][5]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][6]);
do{
if ((sudoku[sudoku_space][6]>9) ||(sudoku[sudoku_space][6]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][6]);//re-enter value
}
}while((sudoku[sudoku_space][6]>9) ||(sudoku[sudoku_space][6]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][7]);
do{
if ((sudoku[sudoku_space][7]>9) ||(sudoku[sudoku_space][7]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][7]);//re-enter value
}
}while((sudoku[sudoku_space][7]>9) ||(sudoku[sudoku_space][7]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("what is the value of %s?\n", str_blank_board [board_space]);
scanf("%d", & sudoku[sudoku_space][8]);
do{
if ((sudoku[sudoku_space][8]>9) ||(sudoku[sudoku_space][8]<0))//checks if value is invalid
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("you entered an invalid number\n");
printf("please try again\n\n");
draw_board();
printf("what is the value of %s?\n", str_blank_board[board_space]);//re-print space where vlaue will be inputted
scanf("%d", & sudoku[sudoku_space][8]);//enter value
}
}while((sudoku[sudoku_space][8]>9) ||(sudoku[sudoku_space][8]<0));
board_space++;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
draw_board();
//////////////////////////////////////////////////////////////////////////////////////////
}//end for loop
}