From c49cdc2e37aa5551297c61fae1ba8919ed51692c Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 14:26:04 +0100 Subject: [PATCH] refactoring: placing bombs calculating marking tile --- src/main/c/Minesweeper/minesweeper_start.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index fd58cfe..85dc9d1 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -75,6 +75,8 @@ 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); @@ -82,9 +84,10 @@ void game_minesweeper(){ printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); getchar(); - if(t == 1){board.tiles[x + y * board.width] = board.tiles[x + y * board.width] == EMPTY ? EMPTY : board.tiles[x + y * board.width] == FLAG ? BLOCK : FLAG;} - - + if(x < board.width && y < board.height){ + x = x + y * board.width; + if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} + } if (t == 2){break;} } } @@ -149,12 +152,12 @@ Minesweeper_Board initialize_minesweeper(){ board.tiles = (char*) malloc(width * height * sizeof(char)); for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;} board.num_bombs = num_bombs; - place_bombs(&board); + board.bombs = (int*) malloc(board.num_bombs * sizeof(int)); + for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;} return board; } void place_bombs(Minesweeper_Board *board){ - board->bombs = (int*) malloc(board->num_bombs * sizeof(int)); srand(time(NULL)); int r; for(int i = 0; i < board->num_bombs; i++){