From 79c871c7ff3ead2265e8f9a390d8f829e5924bb1 Mon Sep 17 00:00:00 2001 From: David Moeller Date: Fri, 26 Jan 2024 18:13:20 +0100 Subject: [PATCH] Test 2 bombs around tile --- src/main/c/Minesweeper/minesweeper_start.c | 16 ++++++------- test/Minesweeper/test_number_of_bombs.c | 27 ++++++++++++++++++---- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main/c/Minesweeper/minesweeper_start.c b/src/main/c/Minesweeper/minesweeper_start.c index 43a1eed..98eb0eb 100644 --- a/src/main/c/Minesweeper/minesweeper_start.c +++ b/src/main/c/Minesweeper/minesweeper_start.c @@ -208,14 +208,14 @@ 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 && tile % board->width == 0){continue;} - else{check_tile - 1;} - if(i > 4 && tile % board->width + 1 == board->width){continue;} - else{check_tile + 1;} - if(i % 3 == 0 && i != 0 && tile / board->width == 0){continue;} - else{check_tile - board->width;} - if(i % 3 == 1 && tile / board->width + 1 == height){continue;} - else{check_tile + board->width;} + 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;}} if(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;} } return sum; diff --git a/test/Minesweeper/test_number_of_bombs.c b/test/Minesweeper/test_number_of_bombs.c index e449f33..047c475 100644 --- a/test/Minesweeper/test_number_of_bombs.c +++ b/test/Minesweeper/test_number_of_bombs.c @@ -12,7 +12,7 @@ void test_no_bombs_placed_around_tile(void){ /* arrange */ int result; Minesweeper_Board board = initialize_minesweeper(); - int tile = 1 + board.width * 1; + int tile = 0 + board.width * 1; /* act */ result = number_of_bombs(&board, tile); @@ -26,10 +26,10 @@ void test_searching_for_bombs_on_correct_tiles(void){ int result; Minesweeper_Board board = initialize_minesweeper(); int tile = 0 + board.width * 1; - board.bombs[0] = 0 + board.width * 2; - board.bombs[0] = 2 + board.width * 1; - board.bombs[0] = board.width - 1 + board.width * 1; - board.bombs[0] = 0 + board.width * (board.height - 1); + board.bombs[0] = 0 + board.width * 3; + board.bombs[1] = 2 + board.width * 1; + board.bombs[2] = board.width - 1 + board.width * 1; + board.bombs[3] = 0 + board.width * (board.height - 1); /* act */ result = number_of_bombs(&board, tile); @@ -38,4 +38,21 @@ void test_searching_for_bombs_on_correct_tiles(void){ TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed around tile } +void test_2_bombs_around_tile(void){ + /* arrange */ + int result; + Minesweeper_Board board = initialize_minesweeper(); + int tile = 0 + board.width * 1; + board.bombs[0] = 0 + board.width * 2; + board.bombs[1] = 1 + board.width * 1; + board.bombs[2] = board.width - 1 + board.width * 1; + board.bombs[3] = 0 + board.width * (board.height - 1); + + /* act */ + result = number_of_bombs(&board, tile); + + /* assert */ + TEST_ASSERT_EQUAL_INT(2, result);//2 bombs placed around tile +} + #endif // TEST \ No newline at end of file