Index

ncurses-minesweeper / 14fa750

Terminal game of Minesweeper, implemented in C with ncurses.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
2016 Sep 2020 17:4014fa750Double board render widthJosh Stockin111G

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

text/plain2464 bytesdownload raw
1#include <ncurses.h>
2
3#include "../game/game.h"
4#include "../state.h"
5#include "pages.h"
6#include "text.h"
7
8int draw_game(game_state *state, int ch) {
9 if (ch == 'q' || ch == 'Q') {
10 state->board->status = Playing;
11 state->page = Title;
12 state->page_selection = 0;
13 return draw_title_screen(state, 0);
14 } else if (ch == 'r' || ch == 'R')
15 ch = 0;
16 game(state, ch);
17 game_board *board = state->board;
18 mvprintw(0, 20, "MINES LEFT: %d", board->mines_left);
19 switch (board->status) {
20 case Playing:
21 mvaddstr(0, centerx("o_o"), "o_o");
22 break;
23 case Done:
24 mvaddstr(0, centerx("^_^"), "^_^");
25 break;
26 case Kaboom:
27 mvaddstr(0, centerx("x_x"), "x_x");
28 break;
29 }
30 attron(A_BOLD);
31 for (int cell = 0; cell < board->width * board->height; cell++) {
32 if (board->current_cell == cell) attron(A_STANDOUT);
33 int x = (int)(COLS / 2) - board->width + cell % board->width * 2;
34 int y = 1 + cell / board->width;
35 if (board->current_cell == cell) attron(A_STANDOUT);
36 if (board->cells[cell].flagged) {
37 if (board->status == Playing)
38 mvaddch(y, x, 'F');
39 else {
40 if (!board->cells[cell].is_bomb) {
41 attron(A_STANDOUT | COLOR_PAIR(5));
42 mvaddch(y, x, 'F');
43 attroff(A_STANDOUT | COLOR_PAIR(5));
44 } else
45 mvaddch(y, x, 'F');
46 }
47 } else {
48 if (board->cells[cell].opened) {
49 if (board->cells[cell].is_bomb) {
50 attron(A_BOLD | COLOR_PAIR(5));
51 mvaddch(y, x, 'X');
52 attroff(A_BOLD | COLOR_PAIR(5));
53 } else if (board->cells[cell].surrounding_bomb_count) {
54 attron(COLOR_PAIR(board->cells[cell].surrounding_bomb_count));
55 mvaddch(y, x, '0' + board->cells[cell].surrounding_bomb_count);
56 attroff(COLOR_PAIR(board->cells[cell].surrounding_bomb_count));
57 } else {
58 mvaddch(y, x, ' ');
59 }
60 } else {
61 attroff(A_BOLD);
62 mvaddch(y, x, '~');
63 attron(A_BOLD);
64 }
65 }
66 if (board->current_cell == cell) attroff(A_STANDOUT);
67 }
68 attroff(A_BOLD);
69 return 0;
70}
71