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 | // TODO: rewrite this POS |
13 |
|
14 | #include <ncurses.h> |
15 |
|
16 | #include "../state.h" |
17 | #include "pages.h" |
18 | #include "text.h" |
19 |
|
20 | const char *options_screen_title = "OPTIONS"; |
21 |
|
22 | const char *options_screen_width = " Board width:"; |
23 | const char *options_screen_height = "Board height:"; |
24 | const char *options_screen_minecount = " # of Mines:"; |
25 |
|
26 | const char *options_screen_back = "Back"; |
27 |
|
28 | int digit_input(int line, int col) { |
29 | // input of two digits for int |
30 | move(line, col); |
31 | clrtoeol(); |
32 |
|
33 | char in1, in2; |
34 | uint8_t n = 0; |
35 |
|
36 | // digit one (tens) |
37 | attron(A_UNDERLINE); |
38 | mvaddch(line, col, ' '); |
39 | while ((in1 = getch())) { |
40 | if (in1 >= '0' && in1 <= '9') { |
41 | n = 10 * (in1 - '0'); |
42 | break; |
43 | } else if (in1 > 0) // invalid input, beep |
44 | beep(); |
45 | } |
46 | attroff(A_UNDERLINE); |
47 | mvaddch(line, col, in1); |
48 |
|
49 | // digit two (ones) |
50 | attron(A_UNDERLINE); |
51 | mvaddch(line, col + 1, ' '); |
52 | while ((in2 = getch())) { |
53 | if (in2 >= '0' && in2 <= '9') { |
54 | n += in2 - '0'; |
55 | break; |
56 | } else if (in2 > 0) // invalid input, beep |
57 | beep(); |
58 | } |
59 | attroff(A_UNDERLINE); |
60 |
|
61 | return n; |
62 | } |
63 |
|
64 | int draw_options_screen(game_state *state, int ch) { |
65 | switch (ch) { |
66 | case -1: |
67 | return 0; |
68 | case 'j': |
69 | case 'J': { |
70 | if (state->page_selection < 3) |
71 | state->page_selection++; |
72 | else |
73 | beep(); |
74 | break; |
75 | } |
76 | case 'k': |
77 | case 'K': { |
78 | if (state->page_selection > 0) |
79 | state->page_selection--; |
80 | else |
81 | beep(); |
82 | break; |
83 | } |
84 | case KEY_RESIZE: |
85 | clear(); |
86 | break; |
87 | case ' ': |
88 | case 10: |
89 | case KEY_ENTER: { |
90 | if (state->page_selection == 3) { |
91 | clear(); |
92 | state->page = Title; |
93 | state->page_selection = 1; |
94 | return draw_title_screen(state, 0); |
95 | } |
96 | break; |
97 | } |
98 | case 0: |
99 | break; |
100 | default: |
101 | beep(); |
102 | } |
103 |
|
104 | int y = centery(); |
105 |
|
106 | attron(A_BOLD | COLOR_PAIR(1)); |
107 | mvaddstr(y - 3, centerx(options_screen_title), options_screen_title); |
108 | attroff(A_BOLD); |
109 |
|
110 | int x = (int)(COLS / 2); |
111 | mvaddstr(y - 1, x - 10, options_screen_width); |
112 | mvaddstr(y, x - 10, options_screen_height); |
113 | mvaddstr(y + 1, x - 10, options_screen_minecount); |
114 |
|
115 | if (state->page_selection == 0) attron(A_STANDOUT); |
116 | mvprintw(y - 1, x + 4, "%02d", state->board->width); |
117 | if (state->page_selection == 0) attroff(A_STANDOUT); |
118 |
|
119 | if (state->page_selection == 1) attron(A_STANDOUT); |
120 | mvprintw(y, x + 4, "%02d", state->board->height); |
121 | if (state->page_selection == 1) attroff(A_STANDOUT); |
122 |
|
123 | if (state->page_selection == 2) attron(A_STANDOUT); |
124 | mvprintw(y + 1, x + 4, "%02d", state->board->mine_count); |
125 | if (state->page_selection == 2) attroff(A_STANDOUT); |
126 |
|
127 | if (state->page_selection == 3) attron(A_STANDOUT); |
128 | mvaddstr(y + 3, centerx(options_screen_back), options_screen_back); |
129 | if (state->page_selection == 3) attroff(A_STANDOUT); |
130 |
|
131 | if (ch == ' ' || ch == 10 || ch == KEY_ENTER) { |
132 | int in = 0; |
133 | switch (state->page_selection) { |
134 | case 0: { |
135 | while ((in = digit_input(y - 1, x + 4))) { |
136 | if (state->board->mine_count < in * state->board->height - 8) |
137 | break; |
138 | else |
139 | beep(); |
140 | } |
141 | state->board->width = in; |
142 | attron(A_STANDOUT); |
143 | mvprintw(y - 1, x + 4, "%02d", state->board->width); |
144 | attroff(A_STANDOUT); |
145 | break; |
146 | } |
147 | case 1: { |
148 | while ((in = digit_input(y, x + 4))) { |
149 | if (state->board->mine_count < state->board->width * in - 8) |
150 | break; |
151 | else |
152 | beep(); |
153 | } |
154 | state->board->height = in; |
155 | attron(A_STANDOUT); |
156 | mvprintw(y, x + 4, "%02d", state->board->height); |
157 | attroff(A_STANDOUT); |
158 | break; |
159 | } |
160 | case 2: { |
161 | while ((in = digit_input(y + 1, x + 4))) { |
162 | if (in < state->board->width * state->board->height - 8) |
163 | break; |
164 | else |
165 | beep(); |
166 | } |
167 | state->board->mine_count = in; |
168 | state->board->mines_left = in; |
169 | attron(A_STANDOUT); |
170 | mvprintw(y + 1, x + 4, "%02d", state->board->mine_count); |
171 | attroff(A_STANDOUT); |
172 | break; |
173 | } |
174 | } |
175 | state->board->current_cell = state->board->width / 2; |
176 | } |
177 |
|
178 | attroff(COLOR_PAIR(1)); |
179 |
|
180 | return 0; |
181 | } |
182 |
|