Index

ncurses-minesweeper / 7eb2a2e

Terminal game of Minesweeper, implemented in C with ncurses.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
2417 Sep 2020 22:4163b24cbFix copyright disclaimerJosh Stockin111G

Blob @ ncurses-minesweeper / src / game / open.c

text/plain2142 bytesdownload raw
1/* ncurses-minesweeper Copyright (c) 2020 Joshua 'joshuas3' Stockin
2 * <https://joshstock.in>
3 * <https://github.com/JoshuaS3/ncurses-minesweeper>
4 *
5 * This software is licensed and distributed under the terms of the MIT License.
6 * See the MIT License in the LICENSE file of this project's root folder.
7 *
8 * This comment block and its contents, including this disclaimer, MUST be
9 * preserved in all copies or distributions of this software's source.
10 */
11
12#include <ncurses.h>
13#include <stdlib.h>
14
15#include "../state.h"
16#include "kaboom.h"
17#include "nearest_cells.h"
18#include "new.h"
19
20void open_cell(game_board *board) {
21 if (board->status == Waiting) { // game hasn't been started
22 begin_game(board); // start new game at this position (generates new board)
23 } else if (board->status == Playing) { // in play
24 game_board_cell *cell = &board->cells[board->current_cell];
25 if (!cell->flagged) { // not flagged
26 if (!cell->opened) {
27 cell->opened = 1;
28 if (cell->is_bomb) {
29 kaboom(board);
30 } else if (!cell->surrounding_bomb_count) // not a bomb
31 // no surrounding bombs -> open all surrounding cells
32 recursively_open_nearby_cells(board, board->current_cell);
33 } else { // cell is already opened. check if # of surrounding flags == cell #
34 // count surrounding flags
35 int16_t *close_cells = list_nearby_cells(board, board->current_cell);
36 int flag_count = 0;
37 for (int i = 0; i < 9; i++) {
38 int16_t close_cell = close_cells[i];
39 if (close_cell != board->current_cell && board->cells[close_cell].flagged) flag_count++;
40 }
41 free(close_cells);
42 // if # of surrounding flags == cell #, recursively open others
43 if (flag_count == cell->surrounding_bomb_count) {
44 recursively_open_nearby_cells(board, board->current_cell);
45 }
46 }
47 }
48 } else
49 beep();
50}
51