manage bombs for human
authorMazet Laurent <mazet@softndesign.org>
Thu, 30 May 2024 21:31:12 +0000 (23:31 +0200)
committerMazet Laurent <mazet@softndesign.org>
Thu, 30 May 2024 21:31:12 +0000 (23:31 +0200)
battleships.c
board.c
board.h
display.c
display.h

index ee3b990d73eafa00f5363b9a10d88cb8c6817ceb..17f18ad94c6e415913408303e0d943426d9b2c51 100644 (file)
@@ -6,6 +6,7 @@
 #include <curses.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <time.h>
 
 #include "board.h"
 #include "debug.h"
@@ -78,6 +79,7 @@ int main (int argc, char *argv[])
 
     curs_set (0);
     start_color ();
+    srand (time (NULL));
 
     int xsize = 20;
     int ysize = 10;
@@ -97,15 +99,17 @@ int main (int argc, char *argv[])
     int xlogs = xhuman;
     int ylogs = yhuman + ysize + 5;
 
-
-    board_t *boardhuman = initboard (xsize, ysize);
     board_t *boardcomputer = initboard (xsize, ysize);
+    int n = 0;
+    while (boats[n] > 0) {
+        drawship (boardcomputer, boats[n++]);
+    }
 
+    board_t *boardhuman = initboard (xsize, ysize);
     int x, y, orient;
-    int n = 0;
+    n = 0;
     int length = boats[n];
-    findlocation (boardhuman, length, &x, &y, &orient);
-    putlocation (boardhuman, length, x, y, orient, 'S');
+    findlocation (boardhuman, length, &x, &y, &orient, " ");
 
     int mode = 0;
     displayboard (boardhuman, xhuman, yhuman, mode, 1);
@@ -117,8 +121,12 @@ int main (int argc, char *argv[])
 
     int stop = 0;
     while (!stop) {
+        if (length > 0) {
+            displayelement (length, xhuman + x, yhuman + y, orient, 'S', mode);
+        } else {
+            displayelement (1, xcomputer + x, ycomputer + y, orient, 'B', mode);
+        }
         int c = getch ();
-        putlocation (boardhuman, length, x, y, orient, ' ');
         switch (c) {
         case KEY_ESC:
 #ifndef __PDCURSES__
@@ -129,40 +137,85 @@ int main (int argc, char *argv[])
         case ':':
             break;
         case 'd':
-            if (testlocation (boardhuman, length, x, y, orient ^ 1)) {
-                orient ^= 1;
+            if (length > 0) {
+                if (testlocation (boardhuman, length, x, y, orient ^ 1, " ")) {
+                    orient ^= 1;
+                }
             }
             break;
         case KEY_UP:
         case 'i':
-            if (testlocation (boardhuman, length, x, y - 1, orient)) {
-                y--;
+            if (length > 0) {
+                if (testlocation (boardhuman, length, x, y - 1, orient, " ")) {
+                    y--;
+                }
+            } else {
+                if (testlocation (boardcomputer, 1, x, y - 1, orient, " S")) {
+                    y--;
+                }
             }
             break;
         case KEY_LEFT:
         case 'j':
-            if (testlocation (boardhuman, length, x - 1, y, orient)) {
-                x--;
+            if (length > 0) {
+                if (testlocation (boardhuman, length, x - 1, y, orient, " ")) {
+                    x--;
+                }
+            } else {
+                if (testlocation (boardcomputer, 1, x - 1, y, orient, " S")) {
+                    x--;
+                }
             }
             break;
         case KEY_DOWN:
         case 'k':
-            if (testlocation (boardhuman, length, x, y + 1, orient)) {
-                y++;
+            if (length > 0) {
+                if (testlocation (boardhuman, length, x, y + 1, orient, " ")) {
+                    y++;
+                }
+            } else {
+                if (testlocation (boardcomputer, 1, x, y + 1, orient, " S")) {
+                    y++;
+                }
             }
             break;
         case KEY_RIGHT:
         case 'l':
-            if (testlocation (boardhuman, length, x + 1, y, orient)) {
-                x++;
+            if (length > 0) {
+                if (testlocation (boardhuman, length, x + 1, y, orient, " ")) {
+                    x++;
+                }
+            } else {
+                if (testlocation (boardcomputer, 1, x + 1, y, orient, " S")) {
+                    x++;
+                }
             }
             break;
         case 'p':
-            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;
+            if (length > 0) {
+                putlocation (boardhuman, length, x, y, orient, 'S');
+                length = boats[++n];
+                if (length == 0) {
+                    x = y = orient = 0;
+                    if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
+                        VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1));
+                        stop = 1;
+                    }
+                } else {
+                    if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) {
+                        VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length));
+                        stop = 1;
+                    }
+                }
+            } else {
+                char symb = (testlocation (boardcomputer, 1, x, y, orient, " ")) ? 'O' : 'X';
+                putlocation (boardcomputer, 1, x, y, orient, symb);
+                if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
+                    VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1));
+                    stop = 1;
+                }
+
+                /* computer turn */
             }
             break;
         case 'q':
@@ -174,7 +227,6 @@ int main (int argc, char *argv[])
         case ERR:
             break;
         }
-        putlocation (boardhuman, length, x, y, orient, 'S');
         displayboard (boardhuman, xhuman, yhuman, mode, 1);
         displayboard (boardcomputer, xcomputer, ycomputer, mode, 0);
     }
diff --git a/board.c b/board.c
index 0c9c0d30412629279982ac5c88b97428a894e7bb..21e46f0f63ba95e1a54e6c6406c57c3fc4a2fcba 100644 (file)
--- a/board.c
+++ b/board.c
@@ -13,7 +13,20 @@ board_t *initboard (int xsize, int ysize)
     return board;
 }
 
-int testlocation (board_t *board, int length, int x, int y, int orient)
+int isoneof (char t, char *symbs)
+{
+    int ret = 0;
+    while (*symbs != '\0') {
+        if (t == *symbs) {
+            ret = 1;
+            break;
+        }
+        symbs++;
+    }
+    return ret;
+}
+
+int testlocation (board_t *board, int length, int x, int y, int orient, char *symbs)
 {
     if ((x < 0) || (x + orient * (length - 1) == board->xsize) ||
         (y < 0) || (y + (orient^1) * (length - 1) == board->ysize)) {
@@ -23,7 +36,7 @@ int testlocation (board_t *board, int length, int x, int y, int orient)
     int ret = 1;
     int i;
     for (i = 0; i < length; i++) {
-        if (board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] != ' ') {
+        if (!isoneof (board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize], symbs)) {
             ret = 0;
             break;
         }
@@ -31,10 +44,10 @@ int testlocation (board_t *board, int length, int x, int y, int orient)
     return ret;
 }
 
-int findlocation (board_t *board, int length, int *x, int *y, int *orient)
+int findlocation (board_t *board, int length, int *x, int *y, int *orient, char *symbs)
 {
     *x = *y = *orient = 0;
-    while (!testlocation (board, length, *x, *y, *orient)) {
+    while (!testlocation (board, length, *x, *y, *orient, symbs)) {
         (*x)++;
         if (*x == board->xsize) {
             *x = 0;
@@ -55,7 +68,22 @@ void putlocation (board_t *board, int length, int x, int y, int orient, char sym
 {
     int i;
     for (i = 0; i < length; i++) {
-        board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] = symb;
+        board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize] = symb;
     }
 }
 
+void drawship (board_t *board, int length)
+{
+    int x, y, orient;
+
+    do {
+        x = rand () % board->xsize;
+        y = rand () % board->ysize;
+        orient = rand () % 2;
+    } while (!testlocation (board, length, x, y, orient, " "));
+
+
+    putlocation (board, length, x, y, orient, 'S');
+}
+
+/* vim: set ts=4 sw=4 et: */
diff --git a/board.h b/board.h
index 40c29d6cc395c94e509b37ea74547abdd461a0d9..ad6610ff334b66982200fbb1a12f4983e7e4bf9b 100644 (file)
--- a/board.h
+++ b/board.h
@@ -9,10 +9,14 @@ typedef struct {
 
 board_t *initboard (int xsize, int ysize);
 
-int testlocation (board_t *board, int length, int x, int y, int orient);
+int testlocation (board_t *board, int length, int x, int y, int orient, char *symbs);
 
-int findlocation (board_t *board, int length, int *x, int *y, int *orient);
+int findlocation (board_t *board, int length, int *x, int *y, int *orient, char *symbs);
 
 void putlocation (board_t *board, int length, int x, int y, int orient, char symb);
 
+void drawship (board_t *board, int length);
+
 #endif /* __BOARD_H__ */
+
+/* vim: set ts=4 sw=4 et: */
index 1140dff709a574b3d5f1feab5571be2f4add125b..76081a71c62b7d81e9af51a739c8e883b0b5b824 100644 (file)
--- a/display.c
+++ b/display.c
@@ -57,6 +57,9 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show)
             } else {
                 c = board->tab[x + y * board->xsize];
                 switch (c) {
+                case 'B':
+                    setcolor (magenta);
+                    break;
                 case 'O':
                     setcolor (blue);
                     break;
@@ -82,6 +85,24 @@ 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)
+{
+    int i;
+    switch (symb) {
+    case 'S':
+        setcolor (yellow);
+        break;
+    case 'B':
+        setcolor (magenta);
+        break;
+    }
+    char c = (show) ? symb : ' ';
+    for (i = 0; i < length; i++) {
+        mvaddch (y + (orient ^ 1) * i, x + orient * i, c);
+    }
+    setcolor (white);
+}
+
 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";
index a6e4490983f7e8947f5e69b27061d5b3f6af1c87..36cc9934403cffd90ca8a68dc5fca7b259796718 100644 (file)
--- a/display.h
+++ b/display.h
@@ -7,6 +7,8 @@
 
 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 displaylogs (char *messages, int width, int height, int xoffset, int yoffset);