From 72f8a44cc1d2c5ab3cefe1fa2ab0f679abc44671 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 15:59:03 +0100 Subject: [PATCH] Test if tile has no bomb --- src/main/c/Minesweeper/minesweeper_start.c | 9 ++++++++- test/Minesweeper/test_bomb_in_array.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 85dc9d1..50bb461 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -24,6 +24,7 @@ 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); +int open_tile(Minesweeper_Board *board, int tile); #pragma endregion #pragma region Global @@ -84,8 +85,9 @@ void game_minesweeper(){ printf("Next turn (x, y, t(0 = open, 1 = flag)): "); scanf("%d %d %d", &x, &y, &t); getchar(); - if(x < board.width && y < board.height){ + if(x < board.width && x > -1 && y < board.height && y > -1){ x = x + y * board.width; + if(t == 0){running = open_tile(&board, x);} if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];} } if (t == 2){break;} @@ -193,4 +195,9 @@ void draw_minesweeper(Minesweeper_Board board){ printf(" +"); for(int i = 0; i < board.width; i++){printf("-");} printf("+\n"); +} + +int open_tile(Minesweeper_Board *board, int tile){ + if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;} + } \ No newline at end of file diff --git a/test/Minesweeper/test_bomb_in_array.c b/test/Minesweeper/test_bomb_in_array.c index 710791b..2e97c56 100644 --- a/test/Minesweeper/test_bomb_in_array.c +++ b/test/Minesweeper/test_bomb_in_array.c @@ -22,4 +22,19 @@ void test_bomb_in_array(void){ TEST_ASSERT_TRUE(result);//head collides with body } + +void test_bomb_not_in_array(void){ + /* arrange */ + bool result; + int array[] = {5, 9, 42, 6, 87, 95, 202, 13, 45 ,78}; + int bomb = 0; + int length = 10; + + /* act */ + result = bomb_in_array(array, bomb, length); + + /* assert */ + TEST_ASSERT_FALSE(result);//head collides with body +} + #endif // TEST \ No newline at end of file