1 | #include <stdlib.h> |
2 |
|
3 | #include "../state.h" |
4 |
|
5 | int16_t *list_nearby_cells(game_board *board, uint16_t cell) { |
6 | int16_t *cells = (int16_t *)calloc(9, sizeof(int16_t)); |
7 | if (cells == NULL) abort(); |
8 | uint8_t board_width = board->width; |
9 | uint8_t board_height = board->height; |
10 | cells[0] = cell - board_width - 1; |
11 | cells[1] = cell - board_width; |
12 | cells[2] = cell - board_width + 1; |
13 | cells[3] = cell - 1; |
14 | cells[4] = cell; |
15 | cells[5] = cell + 1; |
16 | cells[6] = cell + board_width - 1; |
17 | cells[7] = cell + board_width; |
18 | cells[8] = cell + board_width + 1; |
19 | if (cell % board_width == 0) { // left edge |
20 | cells[0] = cell; |
21 | cells[3] = cell; |
22 | cells[6] = cell; |
23 | } |
24 | if ((cell + 1) % board_width == 0) { // right edge |
25 | cells[2] = cell; |
26 | cells[5] = cell; |
27 | cells[8] = cell; |
28 | } |
29 | if (cell < board_width) { // top edge |
30 | cells[0] = cell; |
31 | cells[1] = cell; |
32 | cells[2] = cell; |
33 | } |
34 | if (cell >= board_width * (board_height - 1)) { // bottom edge |
35 | cells[6] = cell; |
36 | cells[7] = cell; |
37 | cells[8] = cell; |
38 | } |
39 | return cells; |
40 | } |
41 |
|
42 | void recursively_open_nearby_cells(game_board *board, uint16_t cell) { |
43 | int16_t *nearby = list_nearby_cells(board, cell); |
44 | for (int i = 0; i < 9; i++) { |
45 | uint16_t c = nearby[i]; |
46 | game_board_cell *x = &board->cells[c]; |
47 | if (c != cell && !x->opened) { |
48 | x->opened = 1; |
49 | if (x->surrounding_bomb_count == 0 && !x->is_bomb && !x->flagged) recursively_open_nearby_cells(board, c); |
50 | } |
51 | } |
52 | free(nearby); |
53 | } |
54 |
|