From 2367550137e9c3c30e57e53bb00d4d36e6bba6b8 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 21:03:38 +0100 Subject: [PATCH] wip batch opening --- src/main/c/Minesweeper/minesweeper_start.c | 54 ++++++++++++++++------ src/main/c/Minesweeper/minesweeper_start.h | 5 +- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 48bf4d9..a16a24b 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -22,10 +22,12 @@ void game_minesweeper(); void options_minesweeper(); Minesweeper_Board initialize_minesweeper(); void place_bombs(Minesweeper_Board *board); -bool bomb_in_array(int array[], int bomb, int length); -void draw_minesweeper(Minesweeper_Board board); +bool bomb_in_array(int *array, int bomb, int length); +bool array_contains_value(int *array, int value, int length); +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); #pragma endregion #pragma region Global @@ -77,11 +79,12 @@ void game_minesweeper(){ bool running = true; int x, y, t; Minesweeper_Board board = initialize_minesweeper(); + place_bombs(&board); while (running){ system("clear"); - draw_minesweeper(board); + draw_minesweeper(&board); printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); @@ -91,7 +94,7 @@ void game_minesweeper(){ if(t == 0){ int bombs = open_tile(&board, x); if(bombs == -1){running = false;} - else if(bombs == 0){board.tiles[x] = EMPTY;} + else if(bombs == 0){open_empty_space(&board, x, 0);} else{board.tiles[x] = bombs + 48;} } if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} @@ -157,8 +160,10 @@ Minesweeper_Board initialize_minesweeper(){ Minesweeper_Board board; board.width = width; board.height = height; - board.tiles = (char*) malloc(width * height * sizeof(char)); + board.tiles = (int*) malloc(width * height * sizeof(int)); for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} + board.marked = (int*) malloc(width * height * sizeof(int)); + for(int i = 0; i < width * height; i++){board.tiles[i] = 0;} board.num_bombs = num_bombs; board.bombs = (int*) malloc(board.num_bombs * sizeof(int)); for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;} @@ -177,29 +182,33 @@ void place_bombs(Minesweeper_Board *board){ } } -bool bomb_in_array(int array[], int bomb, int length){ +bool bomb_in_array(int *array, int bomb, int length){ for(int i = 0; i < length; i++){ if(array[i] == bomb){return true;} } return false; } -void draw_minesweeper(Minesweeper_Board board){ +bool array_contains_value(int *array, int value, int length){ + +} + +void draw_minesweeper(Minesweeper_Board *board){ printf(" "); - for(int i = 0; i < board.width; i++){printf("%d", i / 10);} + for(int i = 0; i < board->width; i++){printf("%d", i / 10);} printf("\n "); - for(int i = 0; i < board.width; i++){printf("%d", i % 10);} + for(int i = 0; i < board->width; i++){printf("%d", i % 10);} printf("\n +"); - for(int i = 0; i < board.width; i++){printf("-");} + for(int i = 0; i < board->width; i++){printf("-");} printf("+\n"); - for(int i = 0; i < board.height; i++){ + for(int i = 0; i < board->height; i++){ printf("%d", i / 10); printf("%d|", i % 10); - for(int j = 0; j < board.width; j++){printf("%c", board.tiles[i * width + j]);} + for(int j = 0; j < board->width; j++){printf("%c", board->tiles[i * width + j]);} printf("|\n"); } printf(" +"); - for(int i = 0; i < board.width; i++){printf("-");} + for(int i = 0; i < board->width; i++){printf("-");} printf("+\n"); } @@ -224,4 +233,23 @@ int number_of_bombs(Minesweeper_Board *board, int tile){ if(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;} } return sum; +} + +void open_empty_space(Minesweeper_Board *board, int tile, int index){ + board->tiles[tile] = EMPTY; + 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 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);} + } } \ No newline at end of file diff --git a/src/main/c/Minesweeper/minesweeper_start.h b/src/main/c/Minesweeper/minesweeper_start.h index c62977b..b577233 100644 --- a/src/main/c/Minesweeper/minesweeper_start.h +++ b/src/main/c/Minesweeper/minesweeper_start.h @@ -4,7 +4,8 @@ typedef struct Minesweeper_Board{ unsigned int width; unsigned int height; - char *tiles; + int *tiles; + int *marked; int num_bombs; int *bombs; }Minesweeper_Board; @@ -12,7 +13,7 @@ typedef struct Minesweeper_Board{ void minesweeper_start(); Minesweeper_Board initialize_minesweeper(); -bool bomb_in_array(int array[], int bomb, int length); +bool bomb_in_array(int *array, int bomb, int length); int number_of_bombs(Minesweeper_Board *board, int tile);