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