Browse Source

Test no bombs araound tile

remotes/origin/David
David Moeller 11 months ago
parent
commit
2a8cefa322
  1. 19
      src/main/c/Minesweeper/minesweeper_start.c
  2. 2
      src/main/c/Minesweeper/minesweeper_start.h
  3. 24
      test/Minesweeper/test_number_of_bombs.c

19
src/main/c/Minesweeper/minesweeper_start.c

@ -25,6 +25,7 @@ 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);
void draw_minesweeper(Minesweeper_Board board); void draw_minesweeper(Minesweeper_Board board);
int open_tile(Minesweeper_Board *board, int tile); int open_tile(Minesweeper_Board *board, int tile);
int number_of_bombs(Minesweeper_Board *board, int tile);
#pragma endregion #pragma endregion
#pragma region Global #pragma region Global
@ -199,5 +200,23 @@ void draw_minesweeper(Minesweeper_Board board){
int open_tile(Minesweeper_Board *board, int tile){ int open_tile(Minesweeper_Board *board, int tile){
if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;} if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;}
int num = number_of_bombs(board, 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 && 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(bomb_in_array(board->bombs, check_tile, board->num_bombs)){sum++;}
}
return sum;
} }

2
src/main/c/Minesweeper/minesweeper_start.h

@ -11,7 +11,9 @@ typedef struct Minesweeper_Board{
void minesweeper_start(); 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);
#endif // MINESWEEPER_START_H #endif // MINESWEEPER_START_H

24
test/Minesweeper/test_number_of_bombs.c

@ -0,0 +1,24 @@
#ifdef TEST
#include "unity.h"
#include <stdbool.h>
#include "../../src/main/c/Minesweeper/minesweeper_start.h"
void setUp(void){}
void tearDown(void){}
void test_no_bombs_placed_around_tile(void){
/* arrange */
int result;
Minesweeper_Board board = initialize_minesweeper();
int tile = 1 + board.width * 1;
/* act */
result = number_of_bombs(&board, tile);
/* assert */
TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed
}
#endif // TEST
Loading…
Cancel
Save