Browse Source

Test if tile has no bomb

remotes/origin/David
David Moeller 11 months ago
parent
commit
72f8a44cc1
  1. 9
      src/main/c/Minesweeper/minesweeper_start.c
  2. 15
      test/Minesweeper/test_bomb_in_array.c

9
src/main/c/Minesweeper/minesweeper_start.c

@ -24,6 +24,7 @@ Minesweeper_Board initialize_minesweeper();
void place_bombs(Minesweeper_Board *board); 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);
#pragma endregion #pragma endregion
#pragma region Global #pragma region Global
@ -84,8 +85,9 @@ void game_minesweeper(){
printf("Next turn (x, y, t(0 = open, 1 = flag)): "); printf("Next turn (x, y, t(0 = open, 1 = flag)): ");
scanf("%d %d %d", &x, &y, &t); scanf("%d %d %d", &x, &y, &t);
getchar(); getchar();
if(x < board.width && y < board.height){
if(x < board.width && x > -1 && y < board.height && y > -1){
x = x + y * board.width; 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 == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];}
} }
if (t == 2){break;} if (t == 2){break;}
@ -193,4 +195,9 @@ void draw_minesweeper(Minesweeper_Board board){
printf(" +"); printf(" +");
for(int i = 0; i < board.width; i++){printf("-");} for(int i = 0; i < board.width; i++){printf("-");}
printf("+\n"); printf("+\n");
}
int open_tile(Minesweeper_Board *board, int tile){
if(bomb_in_array(board->bombs, tile, board->num_bombs)){return -1;}
} }

15
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 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 #endif // TEST
Loading…
Cancel
Save