Browse Source

refactoring: open_empty_space and number_of_bombs

remotes/origin/David
David Moeller 11 months ago
parent
commit
edcad38cdb
  1. 42
      src/main/c/Minesweeper/minesweeper_start.c

42
src/main/c/Minesweeper/minesweeper_start.c

@ -27,6 +27,7 @@ void draw_minesweeper(Minesweeper_Board *board);
int open_tile(Minesweeper_Board *board, int tile);
int number_of_bombs(Minesweeper_Board *board, int tile);
void open_empty_space(Minesweeper_Board *board, int tile, int index);
int is_Valid_tile(Minesweeper_Board *board, int tile, int i);
#pragma endregion
#pragma region Global
@ -223,15 +224,8 @@ int open_tile(Minesweeper_Board *board, int 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){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;}}
int check_tile = is_Valid_tile(board, tile, i);
if(check_tile == -1){continue;}
if(array_contains_value(board->bombs, check_tile, board->num_bombs)){sum++;}
}
return sum;
@ -239,19 +233,27 @@ int number_of_bombs(Minesweeper_Board *board, int tile){
void open_empty_space(Minesweeper_Board *board, int tile, int index){
board->tiles[tile] = EMPTY;
board->marked[index++] = tile;
board->marked[index] = tile;
for(int i = 0; i < 8; i++){
int check_tile = tile;
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;}}
int check_tile = is_Valid_tile(board, tile, i);
if(check_tile == -1){continue;}
int sum = open_tile(board, check_tile);
if(sum != 0){board->tiles[check_tile] = sum + 48;}
else if(!array_contains_value(board->marked, check_tile, index)){open_empty_space(board, check_tile, index);}
else if(!array_contains_value(board->marked, check_tile, index + 1)){open_empty_space(board, check_tile, index + 1);}
}
}
int is_Valid_tile(Minesweeper_Board *board, int tile, int i){
int check_tile = tile;
if(i < 3){if(tile % board->width == 0){return -1;}
else{check_tile -= 1;}}
if(i > 4){if(tile % board->width + 1 == board->width){return -1;}
else{check_tile += 1;}}
if(i % 3 == 0){if(i != 0 && tile / board->width == 0){return -1;}
else{check_tile -= board->width;}}
if(i % 3 == 1){if(tile / board->width + 1 == height){return -1;}
else{check_tile += board->width;}}
return check_tile;
}
Loading…
Cancel
Save