Index

ncurses-minesweeper / 14fa750

Terminal game of Minesweeper, implemented in C with ncurses.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
1913 Sep 2020 14:2371d76abAdd majority of game logic, renderingJosh Stockin131G

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

text/plain3655 bytesdownload raw
1#include <ncurses.h>
2
3#include "../state.h"
4#include "pages.h"
5#include "text.h"
6
7const char *options_screen_title = "OPTIONS";
8
9const char *options_screen_width = " Board width:";
10const char *options_screen_height = "Board height:";
11const char *options_screen_minecount = " # of Mines:";
12
13const char *options_screen_back = "Back";
14
15int digit_input(int line, int col) {
16 // input of two digits for int
17 move(line, col);
18 clrtoeol();
19
20 char in1, in2;
21 uint8_t n = 0;
22
23 // digit one (tens)
24 attron(A_UNDERLINE);
25 mvaddch(line, col, ' ');
26 while ((in1 = getch())) {
27 if (in1 >= '0' && in1 <= '9') {
28 n = 10 * (in1 - '0');
29 break;
30 } else // invalid input, beep
31 beep();
32 }
33 attroff(A_UNDERLINE);
34 mvaddch(line, col, in1);
35
36 // digit two (ones)
37 attron(A_UNDERLINE);
38 mvaddch(line, col + 1, ' ');
39 while ((in2 = getch())) {
40 if (in2 >= '0' && in2 <= '9') {
41 n += in2 - '0';
42 break;
43 } else // invalid input, beep
44 beep();
45 }
46 attroff(A_UNDERLINE);
47
48 return n;
49}
50
51int draw_options_screen(game_state *state, int ch) {
52 if (ch == 'j' || ch == 'J') {
53 if (state->page_selection < 3)
54 state->page_selection++;
55 else
56 beep();
57 } else if (ch == 'k' || ch == 'K') {
58 if (state->page_selection > 0)
59 state->page_selection--;
60 else
61 beep();
62 } else if (ch == ' ' || ch == 10) {
63 if (state->page_selection == 3) {
64 state->page = Title;
65 state->page_selection = 1;
66 return draw_title_screen(state, 0);
67 }
68 }
69
70 int y = centery();
71
72 attron(A_BOLD | COLOR_PAIR(1));
73 mvaddstr(y - 3, centerx(options_screen_title), options_screen_title);
74 attroff(A_BOLD);
75
76 int x = (int)(COLS / 2);
77 mvaddstr(y - 1, x - 10, options_screen_width);
78 mvaddstr(y, x - 10, options_screen_height);
79 mvaddstr(y + 1, x - 10, options_screen_minecount);
80
81 if (state->page_selection == 0) attron(A_STANDOUT);
82 mvprintw(y - 1, x + 4, "%02d", state->board->width);
83 if (state->page_selection == 0) attroff(A_STANDOUT);
84
85 if (state->page_selection == 1) attron(A_STANDOUT);
86 mvprintw(y, x + 4, "%02d", state->board->height);
87 if (state->page_selection == 1) attroff(A_STANDOUT);
88
89 if (state->page_selection == 2) attron(A_STANDOUT);
90 mvprintw(y + 1, x + 4, "%02d", state->board->mine_count);
91 if (state->page_selection == 2) attroff(A_STANDOUT);
92
93 if (state->page_selection == 3) attron(A_STANDOUT);
94 mvaddstr(y + 3, centerx(options_screen_back), options_screen_back);
95 if (state->page_selection == 3) attroff(A_STANDOUT);
96
97 if (ch == ' ' || ch == 10) {
98 switch (state->page_selection) {
99 case 0: {
100 state->board->width = digit_input(y - 1, x + 4);
101 attron(A_STANDOUT);
102 mvprintw(y - 1, x + 4, "%02d", state->board->width);
103 attroff(A_STANDOUT);
104 break;
105 }
106 case 1: {
107 state->board->height = digit_input(y, x + 4);
108 attron(A_STANDOUT);
109 mvprintw(y, x + 4, "%02d", state->board->height);
110 attroff(A_STANDOUT);
111 break;
112 }
113 case 2: {
114 state->board->mine_count = digit_input(y + 1, x + 4);
115 attron(A_STANDOUT);
116 mvprintw(y + 1, x + 4, "%02d", state->board->mine_count);
117 attroff(A_STANDOUT);
118 break;
119 }
120 }
121 }
122
123 attroff(COLOR_PAIR(1));
124
125 return 0;
126}
127