Fix an off-by-one on the pawn 2D array indexes.
[Project_algorithmic_C.git] / lib / ui.c
index cee08f1ff1d165627ba1cbda0b29328ed39d2925..c75e3b01ca69f981a1544455fc6aaf51494968b3 100644 (file)
--- a/lib/ui.c
+++ b/lib/ui.c
  * =====================================================================================
  */
 
-#include <ncurses.h>
+#include <string.h>
 
 #include "ui.h"
+#include "othello.h"
+#include "debug.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 print_o(int y, int x, unsigned int type) {
 
-    mvprintw(y, x, "\\/");
-    mvprintw(y+1, x,"/\\");
+    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 */
+    init_pair(1, COLOR_WHITE, COLOR_BLACK);
+    attron(COLOR_PAIR(1));
 }
 
-static void print_o(int y, int x) {
+/* will be used for pawn placement hints */
+static void print_x(int y, int x, unsigned int type) {
 
-    mvprintw(y, x, "/\\");
-    mvprintw(y+1, x, "\\/");
+    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 */
+    init_pair(1, COLOR_WHITE, COLOR_BLACK);
+    attron(COLOR_PAIR(1));
 }
 
-/* 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));
+            }
+        }
+    }
+}
+
+int map_col_letter_to_int(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 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;
+}