fix more signed integer issues.
[Project_algorithmic_C.git] / lib / ui.c
index c786772620be4e78df055a277c8f5507e78f989f..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 ' ' */
@@ -43,25 +45,53 @@ void print_board(int y, int x) {
     mvprintw(y+14, x, "5|    |    |    |    |    |    |    |    |");
     mvprintw(y+15, x, " |    |    |    |    |    |    |    |    |");
     mvprintw(y+16, x, " +----+----+----+----+----+----+----+----+");
-    mvprintw(y+17, x, "7|    |    |    |    |    |    |    |    |");
+    mvprintw(y+17, x, "6|    |    |    |    |    |    |    |    |");
     mvprintw(y+18, x, " |    |    |    |    |    |    |    |    |");
     mvprintw(y+19, x, " +----+----+----+----+----+----+----+----+");
-    mvprintw(y+20, x, "8|    |    |    |    |    |    |    |    |");
+    mvprintw(y+20, x, "7|    |    |    |    |    |    |    |    |");
     mvprintw(y+21, x, " |    |    |    |    |    |    |    |    |");
-    mvprintw(y+22, x, " -----+----+----+----+----+----+----+-----");
+    mvprintw(y+22, x, " +----+----+----+----+----+----+----+----+");
+    mvprintw(y+23, x, "8|    |    |    |    |    |    |    |    |");
+    mvprintw(y+24, x, " |    |    |    |    |    |    |    |    |");
+    mvprintw(y+25, x, " -----+----+----+----+----+----+----+-----");
 }
 
-static void print_o(int y, int x, int type) {
+static void set_default_colors() {
 
+    init_pair(1, COLOR_WHITE, COLOR_BLACK);
+    attron(COLOR_PAIR(1));
+}
+
+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();
 }
 
-/* will be used for pawn placement hints */
-static void print_x(int y, int x, int type) {
+/* 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: y -> 0      x = 1: 1 -> 1
@@ -69,25 +99,93 @@ static void print_x(int y, int x, int type) {
 static int remap_y(int y) {
 
     if (y == 1) {
-        return 0;
-    } else if (y > 1) {
-        return (remap_y(y -1) + 3);
+        return 2;
+    } else if (y > 1 && y <= board_size) {
+        return (remap_y(y - 1) + 3);
+    } else {
+        return -1;
     }
 }
 
 static int remap_x(int x) {
 
     if (x == 1) {
-        return x;
-    } else 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 pawn_array[board_size][board_size]) {
-    for (int i = 0; i < board_size; i++) {
-        for (int j = 0; j < board_size; j++) {
-            //mvprintw(i,  j, "pawn[%d][%d] = %d\n", i, j, pawn_array[i][j]);
+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 (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 -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;
+}