cleaning
authorLaurent Mazet <mazet@softndesign.org>
Sat, 1 Jun 2024 20:56:37 +0000 (22:56 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 1 Jun 2024 20:56:37 +0000 (22:56 +0200)
battleships.c
board.c
board.h

index 799b9a26397d10fa47239cd7258d7616fa696215..2c570fee58ae9ea6a175151dbf76c9978fe842a3 100644 (file)
@@ -31,6 +31,20 @@ int usage (int ret)
     return ret;
 }
 
+/* stop loop */
+int stoploop (void)
+{
+    int stop = 0;
+    while (!stop) {
+        switch (getch ()) {
+        case KEY_ESC:
+        case 'q':
+           stop = 1;
+        }
+    }
+    return stop;
+}
+
 /* main function */
 int main (int argc, char *argv[])
 {
@@ -207,9 +221,7 @@ 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') {
+                if (drawbomb (boardcomputer, x, y)) {
                     displaylogs ("Computer ship hited", wlogs, hlogs, xlogs, ylogs);
                 }
                 if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
@@ -220,31 +232,19 @@ int main (int argc, char *argv[])
                     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;
-                        }
-                    }
+                    stop = stoploop ();
                 }
 
                 /* computer turn */
 
-                if (drawbomb (boardhuman)) {
+                if (drawbomb (boardhuman, -1, -1)) {
                     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;
-                        }
-                    }
+                    stop = stoploop ();
                 }
             }
             break;
diff --git a/board.c b/board.c
index 108db536cf6cb92ad3c950f99f60d79d14203c69..3552d28d7c8e3d9d3bf4301a4b3d92046fbc9e9e 100644 (file)
--- a/board.c
+++ b/board.c
@@ -86,14 +86,14 @@ void drawship (board_t *board, int length)
     putlocation (board, length, x, y, orient, 'S');
 }
 
-int drawbomb (board_t *board)
+int drawbomb (board_t *board, int x, int y)
 {
-    int x, y;
-
-    do {
-        x = rand () % board->xsize;
-        y = rand () % board->ysize;
-    } while (!testlocation (board, 1, x, y, 0, " S"));
+    if (x == -1) {
+        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) {
diff --git a/board.h b/board.h
index 0dc3e9d8fe3f05fb6dd72cb8906e872ae68efe93..56923bb3201a3ccfbf5f0211e6f074a50abeecfb 100644 (file)
--- a/board.h
+++ b/board.h
@@ -17,7 +17,7 @@ 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 drawbomb (board_t *board, int x, int y);
 
 int testsunk (board_t *board);