#include #include #include #include #define SIZE 9 #define EMPTY 0 #define LEVEL_NUMBER 3 int difficulty; int selected_level; void startSudoku(); //is instead of main void availableLevelsFunction(int difficulty); void selectLevel(int grid[SIZE][SIZE]); void initializeGrid(int grid[SIZE][SIZE]); void generateSudoku(int grid[SIZE][SIZE], int difficulty, int level); void turnFunction(int grid[SIZE][SIZE]); int availableLevels[3][3][SIZE][SIZE] = { { //easy {{0, 0, 3, 0, 2, 1, 8, 0, 0}, {6, 0, 0, 0, 7, 0, 1, 3, 2}, {9, 2, 1, 5, 0, 0, 7, 6, 4}, {7, 6, 0, 3, 0, 0, 4, 0, 0}, {8, 5, 4, 1, 9, 7, 0, 0, 0}, {1, 0, 0, 4, 0, 6, 0, 0, 0}, {2, 9, 0, 8, 1, 5, 0, 0, 6}, {0, 4, 8, 2, 0, 9, 5, 0, 0}, {5, 0, 0, 7, 0, 3, 2, 0, 0}}, {{0, 0, 3, 0, 2, 1, 8, 0, 0}, {6, 0, 0, 0, 7, 0, 1, 3, 2}, {9, 2, 1, 5, 0, 0, 7, 6, 4}, {7, 6, 0, 3, 0, 0, 4, 0, 0}, {8, 5, 4, 1, 9, 7, 0, 0, 0}, {1, 0, 0, 4, 0, 6, 0, 0, 0}, {2, 9, 0, 8, 1, 5, 0, 0, 6}, {0, 4, 8, 2, 0, 9, 5, 0, 0}, {5, 0, 0, 7, 0, 3, 2, 0, 0}}, {{0, 0, 3, 0, 2, 1, 8, 0, 0}, {6, 0, 0, 0, 7, 0, 1, 3, 2}, {9, 2, 1, 5, 0, 0, 7, 6, 4}, {7, 6, 0, 3, 0, 0, 4, 0, 0}, {8, 5, 4, 1, 9, 7, 0, 0, 0}, {1, 0, 0, 4, 0, 6, 0, 0, 0}, {2, 9, 0, 8, 1, 5, 0, 0, 6}, {0, 4, 8, 2, 0, 9, 5, 0, 0}, {5, 0, 0, 7, 0, 3, 2, 0, 0}}}, {//medium {{0, 0, 3, 0, 2, 1, 8, 0, 0}, {6, 0, 0, 0, 7, 0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, 3, 2}, {9, 2, 1, 5, 0, 0, 7, 6, 4}, {7, 6, 0, 3, 0, 0, 4, 0, 0}, {8, 5, 4, 1, 9, 7, 0, 0, 0}, {1, 0, 0, 4, 0, 6, 0, 0, 0}, {2, 9, 0, 8, 1, 5, 0, 0, 6}, {0, 4, 8, 2, 0, 9, 5, 0, 0}, {5, 0, 0, 7, 0, 3, 2, 0, 0}}, {{0, 0, 3, 0, 2, 1, 8, 0, 0}, {6, 0, 0, 0, 7, 0, 1, 3, 2}, {9, 2, 1, 5, 0, 0, 7, 6, 4}, {7, 6, 0, 3, 0, 0, 4, 0, 0}, {8, 5, 4, 1, 9, 7, 0, 0, 0}, {1, 0, 0, 4, 0, 6, 0, 0, 0}, {2, 9, 0, 8, 1, 5, 0, 0, 6}, {0, 4, 8, 2, 0, 9, 5, 0, 0}, {5, 0, 0, 7, 0, 3, 2, 0, 0}} } }; void startSudoku() { int grid[SIZE][SIZE]; selected_level = 0; while (1) { printf("\nDifficulty Function - Choose difficulty:\n"); printf("1. Easy\n2. Medium\n3. Hard\n"); printf("Enter the corresponding number or type 'quit' to exit: "); char input[10]; scanf("%s", input); if (strcmp(input, "quit") == 0) { break; } difficulty = input[0] - '0'; // Convert the first character to an integer if ((difficulty >= 1 && difficulty <= 3) && input[1] == '\0') { availableLevelsFunction(difficulty); selectLevel(grid); turnFunction(grid); } else { printf("Invalid input. Please enter a number between 1 and 3.\n"); } } } void availableLevelsFunction(int difficulty) { printf("\nAvailable Levels for Difficulty %d:\n", difficulty); for (int i = 0; i < LEVEL_NUMBER; i++) { printf("%d. Level %d\n", i + 1, i + 1); } } void selectLevel(int grid[SIZE][SIZE]) { int level = 0; printf("\nSelect a level:\n"); while (true){ char level_select[10]; scanf("%s", level_select); level = level_select[0] - '0'; // Convert the first character to an integer if ((level >= 1 && level <= 3) && level_select[1] == '\0') { selected_level = level; level--; // Adjust to 0-based index generateSudoku(grid, difficulty, selected_level); break; } else { printf("Invalid input. Please enter a number between 1 and 3.\n"); } } } void initializeGrid(int grid[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { grid[i][j] = EMPTY; } } } void generateSudoku(int grid[SIZE][SIZE], int difficulty, int level) { initializeGrid(grid); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { grid[i][j] = availableLevels[difficulty - 1][level - 1][i][j]; } } for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("%d", grid[i][j]); } printf("\n"); } } void turnFunction(int grid[SIZE][SIZE]) { printf("\nTurn function - Choose an action:\n"); int action; while (true){ char action_str[10]; scanf("%s", action_str); action = action_str[0] - '0'; // Convert the first character to an integer if ((action >= 1 && action <= 6) && action_str[1] == '\0') { break; } else { printf("Invalid input. Please enter a number between 1 and 6.\n"); } } } int main(){ startSudoku(); return 0; }