cleaner help message
authorMazet Laurent <mazet@softndesign.org>
Mon, 3 Jun 2024 05:41:30 +0000 (07:41 +0200)
committerMazet Laurent <mazet@softndesign.org>
Mon, 3 Jun 2024 05:41:30 +0000 (07:41 +0200)
battleships.c
display.c
display.h

index 7fcca9e631e6baadb4bd20e4f5b3f39a279cdf2b..5662501ba8f9c4f96b251435ccbabe0e54fba6b3 100644 (file)
@@ -19,6 +19,17 @@ char *version = "0.9";
 
 int boats[9] = {1, 1, 1, 1, 2, 2, 3, 4, 0};
 
+char *help =
+    "<d> Rotate shirp\n"
+    "<i> Move up element\n"
+    "<j> Move left element\n"
+    "<k> Move down element\n"
+    "<l> Move right element\n"
+    "<p> Put ship or bomb\n"
+    "<q> Quit\n"
+    "<t> Toggle case legend\n"
+    ;
+
 /* help message */
 int usage (int ret)
 {
@@ -130,7 +141,7 @@ int main (int argc, char *argv[])
     displayboard (boardhuman, xhuman, yhuman, mode, 1);
     displayboard (boardcomputer, xcomputer, ycomputer, mode, 0);
 
-    displayhelp (whelp, xhelp, yhelp);
+    displayhelp (help, xhelp, yhelp, whelp);
 
     displaylogs ("Welcome to Battle Ships", wlogs, hlogs, xlogs, ylogs);
     displaylogs ("Put your ships on board", wlogs, hlogs, xlogs, ylogs);
index d3c7f7db54403d275446f1130afa2fd7a8484156..3f6c2e841f928c493c29aa040501ce4759d0b2c8 100644 (file)
--- a/display.c
+++ b/display.c
@@ -105,24 +105,27 @@ void displayelement (int length, int x, int y, int orient, char symb, int show)
     setcolor (white);
 }
 
-void displayhelp (int width, int xoffset, int yoffset)
+void displayhelp (char *msg, int xoffset, int yoffset, int length)
 {
-    char *message = "Battle Ships\n------------\n- arrow or i, j, k, l to move\n- p to put ship or bomb\n- d to rotate ship\n- q to quit";
-
-    int i = 0, j = 0, l = 0;
-    char c;
-    while ((c = message[l++]) != 0) {
-        if (c != '\n') {
-            if (i == width) {
-                i = 0;
-                j++;
-            }
-            mvaddch (yoffset + j, xoffset + i, c);
-            i++;
-        } else {
+    char *title = "Help message";
+    int i, j;
+
+    for (i = 0; (i < length) && (title[i] != '\0'); i++) {
+        mvaddch (yoffset , xoffset + i, title[i]);
+        mvaddch (yoffset + 1, xoffset + i, ACS_HLINE);
+    }
+    i = j = 0;
+    yoffset += 2;
+    while ((msg) && (*msg != '\0')) {
+        if ((*msg == '\n') || (i  == length)) {
             i = 0;
             j++;
         }
+        if (*msg != '\n') {
+            mvaddch (yoffset + j, xoffset + i, *msg);
+            i++;
+        }
+        msg++;
     }
 }
 
index 36cc9934403cffd90ca8a68dc5fca7b259796718..85c56967454744e515595ae475c8cdb19342d411 100644 (file)
--- a/display.h
+++ b/display.h
@@ -9,7 +9,7 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show)
 
 void displayelement (int length, int x, int y, int orient, char symb, int show);
 
-void displayhelp (int width, int xoffset, int yoffset);
+void displayhelp (char *msg, int xoffset, int yoffset, int length);
 
 void displaylogs (char *messages, int width, int height, int xoffset, int yoffset);