White space cleanup
[Project_algorithmic_C.git] / lib / ui.c
index 2673dc96348204ad9931d8f8efac8f9e5750333e..4242ac079946b19404748af24f7540a24cd6c7e3 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
@@ -19,6 +19,8 @@
 #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 ' ' */
@@ -54,6 +56,12 @@ void print_board(int y, int x) {
     mvprintw(y+25, x, " -----+----+----+----+----+----+----+-----");
 }
 
+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) {
@@ -67,8 +75,7 @@ static void print_o(int y, int x, unsigned int type) {
     mvprintw(y, x, "/\\");
     mvprintw(y+1, x, "\\/");
     /* reset to default */
-    init_pair(1, COLOR_WHITE, COLOR_BLACK);
-    attron(COLOR_PAIR(1));
+    set_default_colors();
 }
 
 /* will be used for pawn placement hints */
@@ -84,44 +91,46 @@ static void print_x(int y, int x, unsigned int type) {
     mvprintw(y, x, "\\/");
     mvprintw(y+1, x,"/\\");
     /* reset to default */
-    init_pair(1, COLOR_WHITE, COLOR_BLACK);
-    attron(COLOR_PAIR(1));
+    set_default_colors();
 }
 
 /* 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 2;
-    } else if (y > 1 && y < board_size + 1) {
+    } else if (y > 1 && y <= board_size) {
         return (remap_y(y - 1) + 3);
+    } else {
+        return -1;
     }
-    return -1;
 }
 
 static int remap_x(int x) {
 
     if (x == 1) {
         return 3;
-    } else if (x > 1 && x < board_size + 1) {
+    } else if (x > 1 && x <= board_size) {
         return (remap_x(x - 1) + 5);
+    } else {
+        return -1;
     }
-    return -1;
 }
 
 void print_pawns(int base_y, int base_x, unsigned int pawn_array[board_size][board_size]) {
-    for (unsigned int i = 0; i < board_size; i++) {
-        for (unsigned int j = 0; j < board_size; j++) {
-            if (pawn_array[i][j] != empty) {
-                print_o(base_y + remap_y(i), base_x + remap_x(j), pawn_array[i][j]);
+
+    for (int i = 1; i <= board_size; i++) {
+        for (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) {
-    
+int map_col_letter_to_index(char c) {
+
     if (c == 'a' || c == 'A') {
         return 1;
     } else if (c == 'b' || c == 'B') {
@@ -138,12 +147,37 @@ int map_col_letter_to_int(char c) {
         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;
     }
-    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, "%d%c", y, x);
-    return (retVal == 1) ? 0 : 1;
+    int retval = mvwscanw(windows, base_y + 1, base_x + strlen(msg)/2, "%d%c", y, x);
+    return (retval == 1) ? 0 : 1;
 }