add help message, logs window and computer board
authorLaurent Mazet <mazet@softndesign.org>
Sun, 26 May 2024 22:16:58 +0000 (00:16 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 26 May 2024 22:16:58 +0000 (00:16 +0200)
battleships.c
display.c
display.h

index 0f4b2cf2aeebc3c393ebe3d796ab6959a0986837..ee3b990d73eafa00f5363b9a10d88cb8c6817ceb 100644 (file)
@@ -15,6 +15,8 @@
 char *progname = NULL;
 char *version = "0.1";
 
+int boats[9] = {1, 1, 1, 1, 2, 2, 3, 4, 0};
+
 /* help message */
 int usage (int ret)
 {
@@ -79,26 +81,44 @@ int main (int argc, char *argv[])
 
     int xsize = 20;
     int ysize = 10;
-    int xoffset = 3;
-    int yoffset = 3;
+    int whelp = 20;
+    int wlogs = 70;
+    int hlogs = 4;
+
+    int xhuman = 3;
+    int yhuman = 3;
+
+    int xcomputer = xhuman + xsize + 5;
+    int ycomputer = yhuman;
+
+    int xhelp = xcomputer + xsize + 4;
+    int yhelp = ycomputer - 1;
 
-    board_t *board = initboard (xsize, ysize);
-    board->tab[3] = 'X';
-    board->tab[4] = 'O';
-    board->tab[5] = 'S';
+    int xlogs = xhuman;
+    int ylogs = yhuman + ysize + 5;
+
+
+    board_t *boardhuman = initboard (xsize, ysize);
+    board_t *boardcomputer = initboard (xsize, ysize);
 
     int x, y, orient;
-    int boatlength = (random () % 4) + 1;
-    findlocation (board, boatlength, &x, &y, &orient);
-    putlocation (board, boatlength, x, y, orient, 'S');
+    int n = 0;
+    int length = boats[n];
+    findlocation (boardhuman, length, &x, &y, &orient);
+    putlocation (boardhuman, length, x, y, orient, 'S');
 
     int mode = 0;
-    displayboard (board, xoffset, yoffset, mode);
+    displayboard (boardhuman, xhuman, yhuman, mode, 1);
+    displayboard (boardcomputer, xcomputer, ycomputer, mode, 0);
+
+    displayhelp (whelp, xhelp, yhelp);
+
+    displaylogs ("Welcome to Battle Ships\0Put your ships on board\0", wlogs, hlogs, xlogs, ylogs);
 
     int stop = 0;
     while (!stop) {
         int c = getch ();
-        putlocation (board, boatlength, x, y, orient, ' ');
+        putlocation (boardhuman, length, x, y, orient, ' ');
         switch (c) {
         case KEY_ESC:
 #ifndef __PDCURSES__
@@ -109,39 +129,39 @@ int main (int argc, char *argv[])
         case ':':
             break;
         case 'd':
-            if (testlocation (board, boatlength, x, y, orient ^ 1)) {
+            if (testlocation (boardhuman, length, x, y, orient ^ 1)) {
                 orient ^= 1;
             }
             break;
         case KEY_UP:
         case 'i':
-            if (testlocation (board, boatlength, x, y - 1, orient)) {
+            if (testlocation (boardhuman, length, x, y - 1, orient)) {
                 y--;
             }
             break;
         case KEY_LEFT:
         case 'j':
-            if (testlocation (board, boatlength, x - 1, y, orient)) {
+            if (testlocation (boardhuman, length, x - 1, y, orient)) {
                 x--;
             }
             break;
         case KEY_DOWN:
         case 'k':
-            if (testlocation (board, boatlength, x, y + 1, orient)) {
+            if (testlocation (boardhuman, length, x, y + 1, orient)) {
                 y++;
             }
             break;
         case KEY_RIGHT:
         case 'l':
-            if (testlocation (board, boatlength, x + 1, y, orient)) {
+            if (testlocation (boardhuman, length, x + 1, y, orient)) {
                 x++;
             }
             break;
         case 'p':
-            putlocation (board, boatlength, x, y, orient, 'S');
-            boatlength = (random () % 4) + 1;
-            if (!findlocation (board, boatlength, &x, &y, &orient)) {
-                VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", boatlength));
+            putlocation (boardhuman, length, x, y, orient, 'S');
+            length = boats[++n];
+            if (!findlocation (boardhuman, length, &x, &y, &orient)) {
+                VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length));
                 stop = 1;
             }
             break;
@@ -154,8 +174,9 @@ int main (int argc, char *argv[])
         case ERR:
             break;
         }
-        putlocation (board, boatlength, x, y, orient, 'S');
-        displayboard (board, xoffset, yoffset, mode);
+        putlocation (boardhuman, length, x, y, orient, 'S');
+        displayboard (boardhuman, xhuman, yhuman, mode, 1);
+        displayboard (boardcomputer, xcomputer, ycomputer, mode, 0);
     }
 
     endwin ();
index b4f48f568ca6554cedfaa5970a8f1ad57e0b98a3..1140dff709a574b3d5f1feab5571be2f4add125b 100644 (file)
--- a/display.c
+++ b/display.c
@@ -1,4 +1,5 @@
 #include <curses.h>
+#include <string.h>
 #include "board.h"
 
 #include "display.h"
@@ -28,7 +29,7 @@ void setcolor (color_t color)
     attrset (COLOR_PAIR (color));
 }
 
-void displayboard (board_t *board, int xoffset, int yoffset, int mode)
+void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show)
 {
     int x, y;
 
@@ -63,7 +64,11 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode)
                     setcolor (red);
                     break;
                 case 'S':
-                    setcolor (yellow);
+                    if (!show) {
+                        c = ' ';
+                    } else {
+                        setcolor (yellow);
+                    }
                     break;
                 }
                 if (!mode) {
@@ -76,3 +81,49 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode)
         }
     }
 }
+
+void displayhelp (int width, int xoffset, int yoffset)
+{
+    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 {
+            i = 0;
+            j++;
+        }
+    }
+}
+
+void displaylogs (char *messages, int width, int height, int xoffset, int yoffset)
+{
+    int i;
+
+    mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER);
+    mvaddch (yoffset + height, xoffset - 1, ACS_LLCORNER);
+    mvaddch (yoffset + height, xoffset + width, ACS_LRCORNER);
+    mvaddch (yoffset - 1, xoffset + width, ACS_URCORNER);
+
+    for (i = 0; i < width; i++) {
+        mvaddch (yoffset - 1, xoffset + i, ACS_HLINE);
+        mvaddch (yoffset + height, xoffset + i, ACS_HLINE);
+    }
+
+    for (i = 0; i < height; i++) {
+        mvaddch (yoffset + i, xoffset - 1, ACS_VLINE);
+        mvaddch (yoffset + i, xoffset + width, ACS_VLINE);
+        if (*messages != 0) {
+            mvaddstr (yoffset + i, xoffset, messages);
+            messages += strlen (messages) + 1;
+        }
+    }
+}
+
index 9c95a43d845665748930a14c0eef9e02ba35ee80..a6e4490983f7e8947f5e69b27061d5b3f6af1c87 100644 (file)
--- a/display.h
+++ b/display.h
@@ -5,6 +5,10 @@
 
 #define KEY_ESC 0x1b
 
-void displayboard (board_t *board, int xoffset, int yoffset, int mode);
+void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show);
+
+void displayhelp (int width, int xoffset, int yoffset);
+
+void displaylogs (char *messages, int width, int height, int xoffset, int yoffset);
 
 #endif /* __DISPLAY_H__ */