| 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 KEY_DOWN: { |
| 69 | if (state->page_selection < 3) |
| 70 | state->page_selection++; |
| 71 | else |
| 72 | beep(); |
| 73 | break; |
| 74 | } |
| 75 | case KEY_UP: { |
| 76 | if (state->page_selection > 0) |
| 77 | state->page_selection--; |
| 78 | else |
| 79 | beep(); |
| 80 | break; |
| 81 | } |
| 82 | case KEY_RESIZE: |
| 83 | clear(); |
| 84 | break; |
| 85 | case ' ': |
| 86 | case 10: |
| 87 | case KEY_ENTER: { |
| 88 | if (state->page_selection == 3) { |
| 89 | clear(); |
| 90 | state->page = Title; |
| 91 | state->page_selection = 1; |
| 92 | return draw_title_screen(state, 0); |
| 93 | } |
| 94 | break; |
| 95 | } |
| 96 | case 0: |
| 97 | break; |
| 98 | default: |
| 99 | beep(); |
| 100 | } |
| 101 |
|
| 102 | int y = centery(); |
| 103 |
|
| 104 | attron(A_BOLD | COLOR_PAIR(1)); |
| 105 | mvaddstr(y - 3, centerx(options_screen_title), options_screen_title); |
| 106 | attroff(A_BOLD); |
| 107 |
|
| 108 | int x = (int)(COLS / 2); |
| 109 | mvaddstr(y - 1, x - 10, options_screen_width); |
| 110 | mvaddstr(y, x - 10, options_screen_height); |
| 111 | mvaddstr(y + 1, x - 10, options_screen_minecount); |
| 112 |
|
| 113 | if (state->page_selection == 0) attron(A_STANDOUT); |
| 114 | mvprintw(y - 1, x + 4, "%02d", state->board->width); |
| 115 | if (state->page_selection == 0) attroff(A_STANDOUT); |
| 116 |
|
| 117 | if (state->page_selection == 1) attron(A_STANDOUT); |
| 118 | mvprintw(y, x + 4, "%02d", state->board->height); |
| 119 | if (state->page_selection == 1) attroff(A_STANDOUT); |
| 120 |
|
| 121 | if (state->page_selection == 2) attron(A_STANDOUT); |
| 122 | mvprintw(y + 1, x + 4, "%02d", state->board->mine_count); |
| 123 | if (state->page_selection == 2) attroff(A_STANDOUT); |
| 124 |
|
| 125 | if (state->page_selection == 3) attron(A_STANDOUT); |
| 126 | mvaddstr(y + 3, centerx(options_screen_back), options_screen_back); |
| 127 | if (state->page_selection == 3) attroff(A_STANDOUT); |
| 128 |
|
| 129 | if (ch == ' ' || ch == 10 || ch == KEY_ENTER) { |
| 130 | int in = 0; |
| 131 | switch (state->page_selection) { |
| 132 | case 0: { |
| 133 | while ((in = digit_input(y - 1, x + 4))) { |
| 134 | if (state->board->mine_count < in * state->board->height - 8) |
| 135 | break; |
| 136 | else |
| 137 | beep(); |
| 138 | } |
| 139 | state->board->width = in; |
| 140 | attron(A_STANDOUT); |
| 141 | mvprintw(y - 1, x + 4, "%02d", state->board->width); |
| 142 | attroff(A_STANDOUT); |
| 143 | break; |
| 144 | } |
| 145 | case 1: { |
| 146 | while ((in = digit_input(y, x + 4))) { |
| 147 | if (state->board->mine_count < state->board->width * in - 8) |
| 148 | break; |
| 149 | else |
| 150 | beep(); |
| 151 | } |
| 152 | state->board->height = in; |
| 153 | attron(A_STANDOUT); |
| 154 | mvprintw(y, x + 4, "%02d", state->board->height); |
| 155 | attroff(A_STANDOUT); |
| 156 | break; |
| 157 | } |
| 158 | case 2: { |
| 159 | while ((in = digit_input(y + 1, x + 4))) { |
| 160 | if (in < state->board->width * state->board->height - 8) |
| 161 | break; |
| 162 | else |
| 163 | beep(); |
| 164 | } |
| 165 | state->board->mine_count = in; |
| 166 | state->board->mines_left = in; |
| 167 | attron(A_STANDOUT); |
| 168 | mvprintw(y + 1, x + 4, "%02d", state->board->mine_count); |
| 169 | attroff(A_STANDOUT); |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | state->board->current_cell = state->board->width / 2; |
| 174 | } |
| 175 |
|
| 176 | attroff(COLOR_PAIR(1)); |
| 177 |
|
| 178 | return 0; |
| 179 | } |
| 180 |
|