| 1 | /* |
| 2 | * ===================================================================================== |
| 3 | * |
| 4 | * Filename: ui.c |
| 5 | * |
| 6 | * Description: Routines to handle the user interface |
| 7 | * |
| 8 | * Version: 1.0 |
| 9 | * Created: 24/04/2017 16:41:15 |
| 10 | * Revision: none |
| 11 | * Compiler: gcc |
| 12 | * |
| 13 | * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org |
| 14 | * Organization: Piment Noir |
| 15 | * |
| 16 | * ===================================================================================== |
| 17 | */ |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "ui.h" |
| 22 | |
| 23 | /* in all print routine, y and x are the coordinates of the first character of the shape |
| 24 | * which can be a space ' ' */ |
| 25 | |
| 26 | /* FIXME: one can split this shape in building blocks and build it using them */ |
| 27 | void print_board(int y, int x) { |
| 28 | |
| 29 | mvprintw(y, x, " A | B | C | D | E | F | G | H"); |
| 30 | mvprintw(y+1, x, " -----+----+----+----+----+----+----+-----"); |
| 31 | mvprintw(y+2, x, "1| | | | | | | | |"); |
| 32 | mvprintw(y+3, x, " | | | | | | | | |"); |
| 33 | mvprintw(y+4, x, " +----+----+----+----+----+----+----+----+"); |
| 34 | mvprintw(y+5, x, "2| | | | | | | | |"); |
| 35 | mvprintw(y+6, x, " | | | | | | | | |"); |
| 36 | mvprintw(y+7, x, " +----+----+----+----+----+----+----+----+"); |
| 37 | mvprintw(y+8, x, "3| | | | | | | | |"); |
| 38 | mvprintw(y+9, x, " | | | | | | | | |"); |
| 39 | mvprintw(y+10, x, " +----+----+----+----+----+----+----+----+"); |
| 40 | mvprintw(y+11, x, "4| | | | | | | | |"); |
| 41 | mvprintw(y+12, x, " | | | | | | | | |"); |
| 42 | mvprintw(y+13, x, " +----+----+----+----+----+----+----+----+"); |
| 43 | mvprintw(y+14, x, "5| | | | | | | | |"); |
| 44 | mvprintw(y+15, x, " | | | | | | | | |"); |
| 45 | mvprintw(y+16, x, " +----+----+----+----+----+----+----+----+"); |
| 46 | mvprintw(y+17, x, "6| | | | | | | | |"); |
| 47 | mvprintw(y+18, x, " | | | | | | | | |"); |
| 48 | mvprintw(y+19, x, " +----+----+----+----+----+----+----+----+"); |
| 49 | mvprintw(y+20, x, "7| | | | | | | | |"); |
| 50 | mvprintw(y+21, x, " | | | | | | | | |"); |
| 51 | mvprintw(y+22, x, " +----+----+----+----+----+----+----+----+"); |
| 52 | mvprintw(y+23, x, "8| | | | | | | | |"); |
| 53 | mvprintw(y+24, x, " | | | | | | | | |"); |
| 54 | mvprintw(y+25, x, " -----+----+----+----+----+----+----+-----"); |
| 55 | } |
| 56 | |
| 57 | static void print_o(int y, int x, unsigned int type) { |
| 58 | |
| 59 | if (type == black) { |
| 60 | init_color(COLOR_CYAN, 0, 0, 0); /* redefine to a dark black color */ |
| 61 | init_pair(2, COLOR_WHITE, COLOR_CYAN); |
| 62 | attron(COLOR_PAIR(2)); |
| 63 | } else { |
| 64 | init_pair(3, COLOR_BLACK, COLOR_WHITE); |
| 65 | attron(COLOR_PAIR(3)); |
| 66 | } |
| 67 | mvprintw(y, x, "/\\"); |
| 68 | mvprintw(y+1, x, "\\/"); |
| 69 | /* reset to default */ |
| 70 | init_pair(1, COLOR_WHITE, COLOR_BLACK); |
| 71 | attron(COLOR_PAIR(1)); |
| 72 | } |
| 73 | |
| 74 | /* will be used for pawn placement hints */ |
| 75 | static void print_x(int y, int x, unsigned int type) { |
| 76 | |
| 77 | if (type == hint_allowed) { |
| 78 | init_pair(4, COLOR_GREEN, COLOR_BLACK); |
| 79 | attron(COLOR_PAIR(4)); |
| 80 | } else { |
| 81 | init_pair(5, COLOR_RED, COLOR_BLACK); |
| 82 | attron(COLOR_PAIR(5)); |
| 83 | } |
| 84 | mvprintw(y, x, "\\/"); |
| 85 | mvprintw(y+1, x,"/\\"); |
| 86 | /* reset to default */ |
| 87 | init_pair(1, COLOR_WHITE, COLOR_BLACK); |
| 88 | attron(COLOR_PAIR(1)); |
| 89 | } |
| 90 | |
| 91 | /* y = 1: y -> 0 x = 1: 1 -> 1 |
| 92 | * y > 1: y -> y + 3 x > 1: x -> x + 5 */ |
| 93 | static int remap_y(int y) { |
| 94 | |
| 95 | if (y == 1) { |
| 96 | return 2; |
| 97 | } else if (y > 1 && y < board_size + 1) { |
| 98 | return (remap_y(y - 1) + 3); |
| 99 | } |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | static int remap_x(int x) { |
| 104 | |
| 105 | if (x == 1) { |
| 106 | return 3; |
| 107 | } else if (x > 1 && x < board_size + 1) { |
| 108 | return (remap_x(x - 1) + 5); |
| 109 | } |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | void print_pawns(int base_y, int base_x, unsigned int pawn_array[board_size][board_size]) { |
| 114 | for (unsigned int i = 0; i < board_size; i++) { |
| 115 | for (unsigned int j = 0; j < board_size; j++) { |
| 116 | if (pawn_array[i][j] != empty) { |
| 117 | print_o(base_y + remap_y(i), base_x + remap_x(j), pawn_array[i][j]); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | int map_col_letter_to_int(char c) { |
| 124 | |
| 125 | if (c == 'a' || c == 'A') { |
| 126 | return 1; |
| 127 | } else if (c == 'b' || c == 'B') { |
| 128 | return 2; |
| 129 | } else if (c == 'c' || c == 'C') { |
| 130 | return 3; |
| 131 | } else if (c == 'd' || c == 'D') { |
| 132 | return 4; |
| 133 | } else if (c == 'e' || c == 'E') { |
| 134 | return 5; |
| 135 | } else if (c == 'f' || c == 'F') { |
| 136 | return 6; |
| 137 | } else if (c == 'g' || c == 'G') { |
| 138 | return 7; |
| 139 | } else if (c == 'h' || c == 'H') { |
| 140 | return 8; |
| 141 | } |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | int prompt_values(WINDOW* windows, int base_y, int base_x, const char* msg, int* y, char* x) { |
| 146 | mvwprintw(windows, base_y, base_x, "%s\n", msg); |
| 147 | int retVal = mvwscanw(windows, base_y + 1, base_x, "%d%c", y, x); |
| 148 | return (retVal == 1) ? 0 : 1; |
| 149 | } |