add computer play
authorLaurent Mazet <mazet@softndesign.org>
Sat, 1 Jun 2024 19:53:24 +0000 (21:53 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 1 Jun 2024 19:53:24 +0000 (21:53 +0200)
battleships.c
board.c
board.h
display.c

index 18a1513fe811e397f72f733f92d3bb7544c8be14..799b9a26397d10fa47239cd7258d7616fa696215 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "board.h"
 #include "debug.h"
@@ -117,7 +118,8 @@ int main (int argc, char *argv[])
 
     displayhelp (whelp, xhelp, yhelp);
 
-    displaylogs ("Welcome to Battle Ships\0Put your ships on board\0", wlogs, hlogs, xlogs, ylogs);
+    displaylogs ("Welcome to Battle Ships", wlogs, hlogs, xlogs, ylogs);
+    displaylogs ("Put your ships on board", wlogs, hlogs, xlogs, ylogs);
 
     int stop = 0;
     while (!stop) {
@@ -197,6 +199,7 @@ int main (int argc, char *argv[])
                         VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1));
                         stop = 1;
                     }
+                    displaylogs ("Send bomb on computer board", wlogs, hlogs, xlogs, ylogs);
                 } else {
                     if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) {
                         VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length));
@@ -206,12 +209,43 @@ int main (int argc, char *argv[])
             } else {
                 char symb = (testlocation (boardcomputer, 1, x, y, orient, " ")) ? 'O' : 'X';
                 putlocation (boardcomputer, 1, x, y, orient, symb);
+                if (symb == 'X') {
+                    displaylogs ("Computer ship hited", wlogs, hlogs, xlogs, ylogs);
+                }
                 if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
                     VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1));
                     stop = 1;
                 }
+                if (testsunk (boardcomputer)) {
+                    displayboard (boardhuman, xhuman, yhuman, mode, 1);
+                    displayboard (boardcomputer, xcomputer, ycomputer, mode, 1);
+                    displaylogs ("All computer ships sunk", wlogs, hlogs, xlogs, ylogs);
+                    while (!stop) {
+                        switch (getch ()) {
+                        case KEY_ESC:
+                        case 'q':
+                           stop = 1;
+                        }
+                    }
+                }
 
                 /* computer turn */
+
+                if (drawbomb (boardhuman)) {
+                    displaylogs ("Humain ship hited", wlogs, hlogs, xlogs, ylogs);
+                }
+                if (testsunk (boardhuman)) {
+                    displayboard (boardhuman, xhuman, yhuman, mode, 1);
+                    displayboard (boardcomputer, xcomputer, ycomputer, mode, 1);
+                    displaylogs ("All human ships sunk", wlogs, hlogs, xlogs, ylogs);
+                    while (!stop) {
+                        switch (getch ()) {
+                        case KEY_ESC:
+                        case 'q':
+                           stop = 1;
+                        }
+                    }
+                }
             }
             break;
         case KEY_ESC:
diff --git a/board.c b/board.c
index 21e46f0f63ba95e1a54e6c6406c57c3fc4a2fcba..108db536cf6cb92ad3c950f99f60d79d14203c69 100644 (file)
--- a/board.c
+++ b/board.c
@@ -86,4 +86,35 @@ void drawship (board_t *board, int length)
     putlocation (board, length, x, y, orient, 'S');
 }
 
+int drawbomb (board_t *board)
+{
+    int x, y;
+
+    do {
+        x = rand () % board->xsize;
+        y = rand () % board->ysize;
+    } while (!testlocation (board, 1, x, y, 0, " S"));
+
+    int ret = testlocation (board, 1, x, y, 0, " ");
+    if (ret) {
+        putlocation (board, 1, x, y, 0, 'O');
+    } else {
+        putlocation (board, 1, x, y, 0, 'X');
+    }
+    return !ret;
+}
+
+int testsunk (board_t *board)
+{
+    int ret = 1;
+    int i;
+    for (i = 0; i < board->xsize * board->ysize; i++) {
+        if (board->tab[i] == 'S') {
+            ret = 0;
+            break;
+        }
+    }
+    return ret;
+}
+
 /* vim: set ts=4 sw=4 et: */
diff --git a/board.h b/board.h
index ad6610ff334b66982200fbb1a12f4983e7e4bf9b..0dc3e9d8fe3f05fb6dd72cb8906e872ae68efe93 100644 (file)
--- a/board.h
+++ b/board.h
@@ -17,6 +17,10 @@ void putlocation (board_t *board, int length, int x, int y, int orient, char sym
 
 void drawship (board_t *board, int length);
 
+int drawbomb (board_t *board);
+
+int testsunk (board_t *board);
+
 #endif /* __BOARD_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index 76081a71c62b7d81e9af51a739c8e883b0b5b824..e91111d82a2106eab2784f6cd1395202c635a4cb 100644 (file)
--- a/display.c
+++ b/display.c
@@ -1,4 +1,5 @@
 #include <curses.h>
+#include <stdlib.h>
 #include <string.h>
 #include "board.h"
 
@@ -124,9 +125,29 @@ void displayhelp (int width, int xoffset, int yoffset)
     }
 }
 
-void displaylogs (char *messages, int width, int height, int xoffset, int yoffset)
+void displaylogs (char *msg, int width, int height, int xoffset, int yoffset)
 {
-    int i;
+    int i, j;
+
+    static char **buffer = NULL;
+    if (buffer == NULL) {
+        buffer = (char **) malloc (height);
+        for (i = 0; i < height; i++) {
+            buffer[i] = (char *) calloc (1, width + 1);
+        }
+    }
+    for (i = 0; i < height; i++) {
+        if (*buffer[i] == '\0') {
+            strncpy (buffer[i], msg, width);
+            break;
+        }
+    }
+    if (i == height) {
+        for (i = 1; i < height; i++) {
+            strcpy (buffer[i - 1], buffer[i]);
+        }
+        strncpy (buffer[height - 1], msg, width);
+    }
 
     mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER);
     mvaddch (yoffset + height, xoffset - 1, ACS_LLCORNER);
@@ -136,14 +157,16 @@ void displaylogs (char *messages, int width, int height, int xoffset, int yoffse
     for (i = 0; i < width; i++) {
         mvaddch (yoffset - 1, xoffset + i, ACS_HLINE);
         mvaddch (yoffset + height, xoffset + i, ACS_HLINE);
+        for (j = 0; j < height; j++) {
+            mvaddch (yoffset + j, xoffset + i, ' ');
+        }
     }
 
     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;
+        if (*buffer[i] != 0) {
+            mvaddstr (yoffset + i, xoffset, buffer[i]);
         }
     }
 }