From edcad38cdb593d8ab185b45b2aff5483c1fe9640 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Mon, 5 Feb 2024 20:20:42 +0100 Subject: [PATCH] refactoring: open_empty_space and number_of_bombs --- src/main/c/Minesweeper/minesweeper_start.c | 42 +++++++++++----------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 4a0c0e7..9122211 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -27,6 +27,7 @@ void draw_minesweeper(Minesweeper_Board *board); int open_tile(Minesweeper_Board *board, int tile); int number_of_bombs(Minesweeper_Board *board, int tile); void open_empty_space(Minesweeper_Board *board, int tile, int index); +int is_Valid_tile(Minesweeper_Board *board, int tile, int i); #pragma endregion #pragma region Global @@ -223,15 +224,8 @@ int open_tile(Minesweeper_Board *board, int tile){ int number_of_bombs(Minesweeper_Board *board, int tile){ int sum = 0; for(int i = 0; i < 8; i++){ - int check_tile = tile; - if(i < 3){if(tile % board->width == 0){continue;} - else{check_tile -= 1;}} - if(i > 4){if(tile % board->width + 1 == board->width){continue;} - else{check_tile += 1;}} - if(i % 3 == 0){if(i != 0 && tile / board->width == 0){continue;} - else{check_tile -= board->width;}} - if(i % 3 == 1){if(tile / board->width + 1 == height){continue;} - else{check_tile += board->width;}} + int check_tile = is_Valid_tile(board, tile, i); + if(check_tile == -1){continue;} if(array_contains_value(board->bombs, check_tile, board->num_bombs)){sum++;} } return sum; @@ -239,19 +233,27 @@ int number_of_bombs(Minesweeper_Board *board, int tile){ void open_empty_space(Minesweeper_Board *board, int tile, int index){ board->tiles[tile] = EMPTY; - board->marked[index++] = tile; + board->marked[index] = tile; for(int i = 0; i < 8; i++){ - int check_tile = tile; - if(i < 3){if(tile % board->width == 0){continue;} - else{check_tile -= 1;}} - if(i > 4){if(tile % board->width + 1 == board->width){continue;} - else{check_tile += 1;}} - if(i % 3 == 0){if(i != 0 && tile / board->width == 0){continue;} - else{check_tile -= board->width;}} - if(i % 3 == 1){if(tile / board->width + 1 == height){continue;} - else{check_tile += board->width;}} + int check_tile = is_Valid_tile(board, tile, i); + if(check_tile == -1){continue;} int sum = open_tile(board, check_tile); if(sum != 0){board->tiles[check_tile] = sum + 48;} - else if(!array_contains_value(board->marked, check_tile, index)){open_empty_space(board, check_tile, index);} + else if(!array_contains_value(board->marked, check_tile, index + 1)){open_empty_space(board, check_tile, index + 1);} } +} + +int is_Valid_tile(Minesweeper_Board *board, int tile, int i){ + int check_tile = tile; + + if(i < 3){if(tile % board->width == 0){return -1;} + else{check_tile -= 1;}} + if(i > 4){if(tile % board->width + 1 == board->width){return -1;} + else{check_tile += 1;}} + if(i % 3 == 0){if(i != 0 && tile / board->width == 0){return -1;} + else{check_tile -= board->width;}} + if(i % 3 == 1){if(tile / board->width + 1 == height){return -1;} + else{check_tile += board->width;}} + + return check_tile; } \ No newline at end of file