add functions to put ships on board
authorLaurent Mazet <mazet@softndesign.org>
Sat, 25 May 2024 14:09:00 +0000 (16:09 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 25 May 2024 14:09:00 +0000 (16:09 +0200)
battleships.c
board.c
board.h

index 7e1f010df6e7179b1878b262b4b01cc467131dc2..6ac20dbb5a0437da3e9e58146fbd053b3ea0b243 100644 (file)
@@ -75,7 +75,7 @@ int main (int argc, char *argv[])
     keypad (stdscr, TRUE);   /* enable cursor keys */
     scrollok (stdscr, TRUE); /* enable scrolling in main window */
 
-    curs_set (2);
+    curs_set (0);
     start_color ();
 
     int xsize = 20;
@@ -88,16 +88,19 @@ int main (int argc, char *argv[])
     board->tab[4] = 'O';
     board->tab[5] = 'S';
 
+    int x, y, orient;
+    int boatlength = (random () % 4) + 1;
+    findlocation (board, boatlength, &x, &y, &orient);
+    putlocation (board, boatlength, x, y, orient, 'S');
+
     int mode = 0;
     displayboard (board, xoffset, yoffset, mode);
 
-    int x = (xsize + 1) / 2 - 1;
-    int y = (ysize + 1) / 2 - 1;
-
     int stop = 0;
     while (!stop) {
-        move (y + yoffset, x + xoffset);
-        switch (getch ()) {
+        int c = getch ();
+        putlocation (board, boatlength, x, y, orient, ' ');
+        switch (c) {
         case KEY_ESC:
 #ifndef __PDCURSES__
             if (getch () != KEY_ESC) {
@@ -106,40 +109,54 @@ int main (int argc, char *argv[])
 #endif /* __PDCURSES__ */
         case ':':
             break;
+        case 'd':
+            if (testlocation (board, boatlength, x, y, orient ^ 1)) {
+                orient ^= 1;
+            }
+            break;
         case KEY_UP:
         case 'i':
-            if (y > 0) {
+            if (testlocation (board, boatlength, x, y - 1, orient)) {
                 y--;
             }
             break;
         case KEY_LEFT:
         case 'j':
-            if (x > 0) {
+            if (testlocation (board, boatlength, x - 1, y, orient)) {
                 x--;
             }
             break;
         case KEY_DOWN:
         case 'k':
-            if (y < ysize - 1) {
+            if (testlocation (board, boatlength, x, y + 1, orient)) {
                 y++;
             }
             break;
         case KEY_RIGHT:
         case 'l':
-            if (x < xsize - 1) {
+            if (testlocation (board, boatlength, 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 (ERROR, fprintf (stderr, "can't position for boat %d\n", boatlength));
+                exit (1);
+            }
+            break;
         case 'q':
             stop = 1;
             break;
         case 't':
             mode ^= 1;
-            displayboard (board, xoffset, yoffset, mode);
             break;
         case ERR:
             break;
         }
+        putlocation (board, boatlength, x, y, orient, 'S');
+        displayboard (board, xoffset, yoffset, mode);
     }
 
     endwin ();
diff --git a/board.c b/board.c
index 6d91d0dcfcbb70616037ad479891cab9aaeb7af1..aef8af591b32f0f8c805129a8cb73ea82c3c0226 100644 (file)
--- a/board.c
+++ b/board.c
@@ -87,3 +87,50 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode)
         }
     }
 }
+
+int testlocation (board_t *board, int length, int x, int y, int orient)
+{
+    if ((x < 0) || (x + orient * (length - 1) == board->xsize) ||
+        (y < 0) || (y + (orient^1) * (length - 1) == board->ysize)) {
+        return 0;
+    }
+
+    int ret = 1;
+    int i;
+    for (i = 0; i < length; i++) {
+        if (board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] != ' ') {
+            ret = 0;
+            break;
+        }
+    }
+    return ret;
+}
+
+int findlocation (board_t *board, int length, int *x, int *y, int *orient)
+{
+    *x = *y = *orient = 0;
+    while (!testlocation (board, length, *x, *y, *orient)) {
+        (*x)++;
+        if (*x == board->xsize) {
+            *x = 0;
+            (*y)++;
+            if (*y == board->ysize) {
+                *x = *y = 0;
+                *orient ^= 1;
+                if (*orient == 0) {
+                    return 0;
+                }
+            }
+        }
+    }
+    return 1;
+}
+
+void putlocation (board_t *board, int length, int x, int y, int orient, char symb)
+{
+    int i;
+    for (i = 0; i < length; i++) {
+        board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] = symb;
+    }
+}
+
diff --git a/board.h b/board.h
index 7c837027e6adb66b5a6f7c792112c0244b0598be..5ba3ebc3917ca10ecccb1f2a214b1a739fafd099 100644 (file)
--- a/board.h
+++ b/board.h
@@ -11,4 +11,10 @@ board_t *initboard (int xsize, int ysize);
 
 void displayboard (board_t *board, int xoffset, int yoffset, int mode);
 
+int testlocation (board_t *board, int length, int x, int y, int orient);
+
+int findlocation (board_t *board, int length, int *x, int *y, int *orient);
+
+void putlocation (board_t *board, int length, int x, int y, int orient, char symb);
+
 #endif /* __BOARD_H__ */