fix more signed integer issues.
[Project_algorithmic_C.git] / lib / ui.c
index cee08f1ff1d165627ba1cbda0b29328ed39d2925..da79cb3f317f6f0776b81d58ac77df43a31f1ae6 100644 (file)
--- a/lib/ui.c
+++ b/lib/ui.c
@@ -3,7 +3,7 @@
  *
  *       Filename:  ui.c
  *
- *    Description:  Routines to handle the user interface 
+ *    Description:  Routines to handle the user interface
  *
  *        Version:  1.0
  *        Created:  24/04/2017 16:41:15
  * =====================================================================================
  */
 
-#include <ncurses.h>
+#include <string.h>
 
 #include "ui.h"
+#include "debug.h"
+#include "list.h"
 
 /* in all print routine, y and x are the coordinates of the first character of the shape
  * which can be a space ' ' */
 /* FIXME: one can split this shape in building blocks and build it using them */
 void print_board(int y, int x) {
 
-    mvprintw(y, x, "    |    |");
-    mvprintw(y+1, x, "    |    |");
-    mvprintw(y+2, x, "----+----+----");
-    mvprintw(y+3, x, "    |    |");
-    mvprintw(y+4, x, "    |    |");
-    mvprintw(y+5, x, "----+----+----");
-    mvprintw(y+6, x, "    |    |");
-    mvprintw(y+7, x, "    |    |");
+    mvprintw(y, x, "   A  | B  | C  | D  | E  | F  | G  |  H");
+    mvprintw(y+1, x, " -----+----+----+----+----+----+----+-----");
+    mvprintw(y+2, x, "1|    |    |    |    |    |    |    |    |");
+    mvprintw(y+3, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+4, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+5, x, "2|    |    |    |    |    |    |    |    |");
+    mvprintw(y+6, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+7, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+8, x, "3|    |    |    |    |    |    |    |    |");
+    mvprintw(y+9, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+10, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+11, x, "4|    |    |    |    |    |    |    |    |");
+    mvprintw(y+12, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+13, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+14, x, "5|    |    |    |    |    |    |    |    |");
+    mvprintw(y+15, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+16, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+17, x, "6|    |    |    |    |    |    |    |    |");
+    mvprintw(y+18, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+19, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+20, x, "7|    |    |    |    |    |    |    |    |");
+    mvprintw(y+21, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+22, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+23, x, "8|    |    |    |    |    |    |    |    |");
+    mvprintw(y+24, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+25, x, " -----+----+----+----+----+----+----+-----");
 }
 
-static void print_x(int y, int x) {
+static void set_default_colors() {
 
-    mvprintw(y, x, "\\/");
-    mvprintw(y+1, x,"/\\");
+    init_pair(1, COLOR_WHITE, COLOR_BLACK);
+    attron(COLOR_PAIR(1));
 }
 
-static void print_o(int y, int x) {
+static void print_o(int y, int x, unsigned int type) {
 
+    if (type == black) {
+        init_color(COLOR_CYAN, 0, 0, 0); /* redefine to a dark black color */
+        init_pair(2, COLOR_WHITE, COLOR_CYAN);
+        attron(COLOR_PAIR(2));
+    } else {
+        init_pair(3, COLOR_BLACK, COLOR_WHITE);
+        attron(COLOR_PAIR(3));
+    }
     mvprintw(y, x, "/\\");
     mvprintw(y+1, x, "\\/");
+    /* reset to default */
+    set_default_colors();
+}
+
+/* used for pawn placement hints */
+static void print_x(int y, int x, unsigned int type) {
+
+    if (type == hint_allowed) {
+        init_pair(4, COLOR_GREEN, COLOR_BLACK);
+        attron(COLOR_PAIR(4));
+    } else {
+        init_pair(5, COLOR_RED, COLOR_BLACK);
+        attron(COLOR_PAIR(5));
+    }
+    mvprintw(y, x, "\\/");
+    mvprintw(y+1, x,"/\\");
+    /* reset to default */
+    set_default_colors();
 }
 
-/* y: 1 -> +0  x: 1 -> +1
- *    2 -> +3     2 -> +6
- *    3 -> +6     3 -> +11 */
+/* y = 1: y -> 0      x = 1: 1 -> 1
+ * y > 1: y -> y + 3  x > 1: x -> x + 5 */
 static int remap_y(int y) {
 
     if (y == 1) {
-        return 0;
-    } else if (y == 2) {
-        return 3;
+        return 2;
+    } else if (y > 1 && y <= board_size) {
+        return (remap_y(y - 1) + 3);
     } else {
-        return 6;
+        return -1;
     }
 }
 
 static int remap_x(int x) {
 
     if (x == 1) {
+        return 3;
+    } else if (x > 1 && x <= board_size) {
+        return (remap_x(x - 1) + 5);
+    } else {
+        return -1;
+    }
+}
+
+void print_pawns(int base_y, int base_x, unsigned int pawn_array[board_size][board_size]) {
+
+    for (unsigned int i = 1; i <= board_size; i++) {
+        for (unsigned int j = 1; j <= board_size; j++) {
+            if (!is_box_type(i, j, pawn_array, empty)) {
+                print_o(base_y + remap_y(i), base_x + remap_x(j), get_box_value(i, j, pawn_array));
+            }
+        }
+    }
+}
+
+void print_shots_list(int base_y, int base_x, struct shots_list_s* shots_list) {
+    struct shots_list_s* list_counter;
+
+    list_for_each_entry(list_counter, &shots_list->list, list) {
+        print_x(base_y + remap_y(list_counter->y), base_x + remap_x(list_counter->x), list_counter->type);
+    }
+}
+
+int map_col_letter_to_index(char c) {
+
+    if (c == 'a' || c == 'A') {
         return 1;
-    } else if (x == 2) {
+    } else if (c == 'b' || c == 'B') {
+        return 2;
+    } else if (c == 'c' || c == 'C') {
+        return 3;
+    } else if (c == 'd' || c == 'D') {
+        return 4;
+    } else if (c == 'e' || c == 'E') {
+        return 5;
+    } else if (c == 'f' || c == 'F') {
         return 6;
+    } else if (c == 'g' || c == 'G') {
+        return 7;
+    } else if (c == 'h' || c == 'H') {
+        return 8;
+    } else {
+        return -1;
+    }
+}
+
+/* return capital letters */
+char map_col_index_to_letter(int index) {
+
+    if (index == 1) {
+        return 'A';
+    } else if (index == 2) {
+        return 'B';
+    } else if (index == 3) {
+        return 'C';
+    } else if (index == 4) {
+        return 'D';
+    } else if (index == 5) {
+        return 'E';
+    } else if (index == 6) {
+        return 'F';
+    } else if (index == 7) {
+        return 'G';
+    } else if (index == 9) {
+        return 'H';
     } else {
-        return 11;
+        return -1;
     }
 }
+
+int prompt_values(WINDOW* windows, int base_y, int base_x, const char* msg, int* y, char* x) {
+    mvwprintw(windows, base_y, base_x, "%s\n", msg);
+    int retval = mvwscanw(windows, base_y + 1, base_x + strlen(msg)/2, "%d%c", y, x);
+    return (retval == 1) ? 0 : 1;
+}