From 7ccf0878b06afb60eba074f0e2ba374adb731b8b Mon Sep 17 00:00:00 2001 From: Lucas Heil Date: Wed, 7 Feb 2024 17:35:36 +0100 Subject: [PATCH] refactoring: 11 --- src/main/c/sudoku.c | 96 +++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/main/c/sudoku.c b/src/main/c/sudoku.c index 975a080..f18284d 100644 --- a/src/main/c/sudoku.c +++ b/src/main/c/sudoku.c @@ -6,28 +6,30 @@ #define SIZE_OF_GAMEBORD_AXIS_X 9 #define SIZE_OF_GAMEBORD_AXIS_Y 9 +#define AVAILABLE_DIFFICULTIES 3 +#define AVAILABLE_LEVELS 3 #define EMPTY 0 #define LEVEL_NUMBER 3 -int difficulty; +int selected_difficulty; int selected_level; void Game_loop(); //is instead of main -void Level_Pool(int difficulty); -void Level_Selection(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -void initializeGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -void create_playing_field(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int difficulty, int level); -void Player_actions_for_playing(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -void printGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -void check_if_Sudoku_solved(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void Level_Pool(int selected_difficulty); +void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void initializeGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void create_playing_field(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int selected_difficulty, int level); +void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void printGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); +void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]); -int availableLevels[3][3][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = { +int availableLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = { { //easy {{0, 0, 3, 0, 2, 1, 8, 0, 0}, {6, 0, 0, 0, 7, 0, 1, 3, 2}, @@ -124,7 +126,7 @@ int availableLevels[3][3][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = { } }; -int solutionLevels[3][3][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = { +int solutionLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = { { //easy {{4, 7, 3, 6, 2, 1, 8, 5, 9}, @@ -224,7 +226,7 @@ int solutionLevels[3][3][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = { void Game_loop() { - int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]; + int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]; selected_level = 0; while (1) { @@ -239,12 +241,12 @@ void Game_loop() { break; } - difficulty = input[0] - '0'; // Convert the first character to an integer + selected_difficulty = input[0] - '0'; // Convert the first character to an integer - if ((difficulty >= 1 && difficulty <= 3) && input[1] == '\0') { - Level_Pool(difficulty); - Level_Selection(grid); - Player_actions_for_playing(grid); + if ((selected_difficulty >= 1 && selected_difficulty <= 3) && input[1] == '\0') { + Level_Pool(selected_difficulty); + Level_Selection(Sudoku_grid); + Player_actions_for_playing(Sudoku_grid); } else { printf("Invalid input. Please enter a number between 1 and 3.\n"); } @@ -252,8 +254,8 @@ void Game_loop() { } } -void Level_Pool(int difficulty) { - printf("\nAvailable Levels for Difficulty %d:\n", difficulty); +void Level_Pool(int selected_difficulty) { + printf("\nAvailable Levels for Difficulty %d:\n", selected_difficulty); for (int i = 0; i < LEVEL_NUMBER; i++) { printf("%d. Level %d\n", i + 1, i + 1); } @@ -261,7 +263,7 @@ void Level_Pool(int difficulty) { -void Level_Selection(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { int level = 0; printf("\nSelect a level:\n"); while (true){ @@ -276,7 +278,7 @@ void Level_Selection(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) level--; // Adjust to 0-based index - create_playing_field(grid, difficulty, selected_level); + create_playing_field(Sudoku_grid, selected_difficulty, selected_level); break; } else { printf("Invalid input. Please enter a number between 1 and 3.\n"); @@ -287,24 +289,24 @@ void Level_Selection(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) } -void initializeGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void initializeGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) { for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) { - grid[i][j] = EMPTY; + Sudoku_grid[i][j] = EMPTY; } } } -void create_playing_field(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int difficulty, int level) { - initializeGrid(grid); +void create_playing_field(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int selected_difficulty, int level) { + initializeGrid(Sudoku_grid); for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) { for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) { - grid[i][j] = availableLevels[difficulty - 1][level - 1][i][j]; + Sudoku_grid[i][j] = availableLevels[selected_difficulty - 1][level - 1][i][j]; } } for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) { for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) { - printf("%d", grid[i][j]); + printf("%d", Sudoku_grid[i][j]); } printf("\n"); } @@ -312,7 +314,7 @@ void create_playing_field(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXI } -void Player_actions_for_playing(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { while (1) { printf("\nTurn function - Choose an action:\n"); printf("1. Hints\n"); @@ -339,19 +341,19 @@ void Player_actions_for_playing(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBO switch (action) { case 1: - giving_hints_to_player(grid); + giving_hints_to_player(Sudoku_grid); break; case 2: - write_userinput_into_Sudoku(grid); + write_userinput_into_Sudoku(Sudoku_grid); break; case 3: - printGrid(grid); + printGrid(Sudoku_grid); break; case 4: - check_if_Sudoku_solved(grid); + check_if_Sudoku_solved(Sudoku_grid); break; case 5: - Level_Selection(grid); + Level_Selection(Sudoku_grid); break; case 6: printf("Exiting Sudoku program.\n"); @@ -364,7 +366,7 @@ void Player_actions_for_playing(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBO -void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { printf("\nTip function - Choose an option:\n"); printf("1. Set the user-specified cell to the right value\n"); printf("2. Set the user-specified 3x3 field to the right values\n"); @@ -407,7 +409,7 @@ void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_A printf("Invalid input. Please enter a number between 1 and 9.\n"); } } - grid[row - 1][col - 1] = solutionLevels[difficulty - 1][selected_level - 1][row - 1][col - 1]; + Sudoku_grid[row - 1][col - 1] = solutionLevels[selected_difficulty - 1][selected_level - 1][row - 1][col - 1]; printf("Value set successfully.\n"); break; @@ -432,8 +434,8 @@ void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_A } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - grid[startRow - 1 + i][startCol - 1 + j] = - solutionLevels[difficulty - 1][selected_level - 1][startRow - 1 + i][startCol - 1 + j]; + Sudoku_grid[startRow - 1 + i][startCol - 1 + j] = + solutionLevels[selected_difficulty - 1][selected_level - 1][startRow - 1 + i][startCol - 1 + j]; } } printf("3x3 field set successfully.\n"); @@ -442,7 +444,7 @@ void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_A case 3: for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) { for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) { - grid[i][j] = solutionLevels[difficulty - 1][selected_level - 1][i][j]; + Sudoku_grid[i][j] = solutionLevels[selected_difficulty - 1][selected_level - 1][i][j]; } } printf("Puzzle solved. \n"); @@ -452,7 +454,7 @@ void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_A } } -void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { printf("\nInput function - Choose an action:\n"); printf("1. Insert value in an empty field\n"); printf("2. Clear an already filled field\n"); @@ -504,7 +506,7 @@ void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEB num = nume_str[0] - '0'; // Convert the first character to an integer if ((num >= 1 && num <= 9) && nume_str[1] == '\0') { - grid[row_e - 1][col_e - 1] = num; + Sudoku_grid[row_e - 1][col_e - 1] = num; printf("Value inserted successfully.\n"); break; } else { @@ -527,7 +529,7 @@ void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEB col_d = cold_str[0] - '0'; // Convert the first character to an integer if (((row_d >= 1 && row_d <= 9) && rowd_str[1] == '\0') && ((col_d >= 1 && col_d <= 9) && cold_str[1] == '\0')) { - grid[row_d - 1][col_d - 1] = 0; + Sudoku_grid[row_d - 1][col_d - 1] = 0; printf("Cell cleared successfully.\n"); break; } else { @@ -538,26 +540,26 @@ void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEB default: printf("Invalid input. Please enter 1 or 2.\n"); } - printGrid(grid); + printGrid(Sudoku_grid); } -void printGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void printGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { printf("\nSudoku Grid:\n"); for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) { for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) { - printf("%d ", grid[i][j]); + printf("%d ", Sudoku_grid[i][j]); } printf("\n"); } } -void check_if_Sudoku_solved(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { +void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) { printf("\nDone function - Checking if the solution is correct...\n"); for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) { for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) { - if (grid[i][j] != solutionLevels[difficulty - 1][selected_level - 1][i][j]) { + if (Sudoku_grid[i][j] != solutionLevels[selected_difficulty - 1][selected_level - 1][i][j]) { printf("Incorrect solution. Keep trying!\n"); return; }