#include #include #include void gotoXY(int x, int y) { COORD coord = { x, y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); return; } void getCursorXY(int &x, int&y) { CONSOLE_SCREEN_BUFFER_INFO csbi; if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { x = csbi.dwCursorPosition.X; y = csbi.dwCursorPosition.Y; } } int readKey() { int key = 0; if (kbhit()) { key = getch(); /* a key was pressed; read it */ if (key == 0) /* if zero, this is a two-char Fn key */ key = -getch(); /* read 2nd char & return as negative */ } /* to signal that this is an F-key */ return key; } int isAvailable(int puzzle[][9], int row, int col, int num) { int rowStart = (row / 3) * 3; int colStart = (col / 3) * 3; int i, j; for (i = 0; i<9; ++i) { if (puzzle[row][i] == num) return 0; if (puzzle[i][col] == num) return 0; if (puzzle[rowStart + (i % 3)][colStart + (i / 3)] == num) return 0; } return 1; } int fillSudoku(int puzzle[][9], int row, int col) { int i; if (row<9 && col<9) { if (puzzle[row][col] != 0) { if ((col + 1)<9) return fillSudoku(puzzle, row, col + 1); else if ((row + 1)<9) return fillSudoku(puzzle, row + 1, 0); else return 1; } else { for (i = 0; i<9; ++i) { if (isAvailable(puzzle, row, col, i + 1)) { puzzle[row][col] = i + 1; if ((col + 1)<9) { if (fillSudoku(puzzle, row, col + 1)) return 1; else puzzle[row][col] = 0; } else if ((row + 1)<9) { if (fillSudoku(puzzle, row + 1, 0)) return 1; else puzzle[row][col] = 0; } else return 1; } } } return 0; } else return 1; } void PrintSudoku(int puzzle[9][9]) { int j, i; gotoXY(0, 0); for (i = 0; i<9; ++i) { printf("{"); for (j = 0; j<9; ++j) { printf("%1d", puzzle[i][j]); if (j<8) printf(","); } printf("}\n"); } printf("(r)eset (s)solve (q)uit\n"); } void ResetSudoku(int puzzle[9][9]) { int j, i; gotoXY(0, 0); for (i = 0; i<9; ++i) { printf("{"); for (j = 0; j<9; ++j) { puzzle[i][j] = 0; printf("%1d", puzzle[i][j]); if (j<8) printf(","); } printf("}\n"); } } int main() { int i = 0, j = 0; int puzzle[9][9]; /* ={{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}; */ char c; int xx; int yy; ResetSudoku(puzzle); system("cls"); PrintSudoku(puzzle); gotoXY(1, 0); do { c = readKey(); // if (c!=0) printf("%d",c); if (c == -32) { c = readKey(); // printf(",%d",c); switch (c) { case 72: if (j>0) j--; break; case 80: if (j<8) j++; break; case 75: if (i>0) i--; break; case 77: if (i<8) { i++; } break; } xx = i * 2 + 1; yy = j; // gotoXY(0,10); // printf(" %d ",c); } else { switch (c) { case 13: i = 0; if (j<8) j++; xx = i * 2 + 1; yy = j; break; case '0': puzzle[j][i] = c - '0'; printf("%1d", puzzle[j][i]); if (i<8) { i++; xx = i * 2 + 1; } yy = j; gotoXY(xx, yy); break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (isAvailable(puzzle, j, i, c - '0')) { puzzle[j][i] = c - '0'; printf("%1d", puzzle[j][i]); if (i<8) { i++; xx = i * 2 + 1; } yy = j; gotoXY(xx, yy); } break; case 'r': ResetSudoku(puzzle); break; case 's': fillSudoku(puzzle, 0, 0); PrintSudoku(puzzle); break; } } gotoXY(xx, yy); // if (c!=0) printf("%1d",puzzle[j][i]); } while (c != 'q'); gotoXY(0, 10); return(0); }