fix moves
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 3 Jun 2024 09:26:43 +0000 (11:26 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 3 Jun 2024 09:26:43 +0000 (11:26 +0200)
battleships.c
board.c

index 79477fc0fe9c8a92f52d98f1f20b841c6ef762e1..babcf06af63d2a20ef25b7f962a8d5d09dadab61 100644 (file)
@@ -20,14 +20,14 @@ char *version = "0.9";
 int boats[9] = {1, 1, 1, 1, 2, 2, 3, 4, 0};
 
 char *help =
-    "<d> Rotate shirp\n"
-    "<i> Move up element\n"
-    "<j> Move left element\n"
-    "<k> Move down element\n"
-    "<l> Move right element\n"
-    "<p> Put ship or bomb\n"
+    "<d> Rotate ship\n"
+    "<i> Move up\n"
+    "<j> Move left\n"
+    "<k> Move down\n"
+    "<l> Move right\n"
+    "<p> Put ship/bomb\n"
     "<q> Quit\n"
-    "<t> Toggle case legend\n"
+    "<t> Toggle legend\n"
     ;
 
 /* help message */
@@ -166,48 +166,80 @@ int main (int argc, char *argv[])
         case KEY_UP:
         case 'i':
             if (length > 0) {
-                if (testlocation (boardhuman, length, x, y - 1, orient, " ")) {
-                    y--;
+                int t;
+                for (t = y - 1; t >= 0; t--) {
+                    if (testlocation (boardhuman, length, x, t, orient, " ")) {
+                        y = t;
+                        break;
+                    }
                 }
             } else {
-                if (testlocation (boardcomputer, 1, x, y - 1, orient, " S")) {
-                    y--;
+                int t;
+                for (t = y - 1; t >= 0; t--) {
+                    if (testlocation (boardcomputer, 1, x, t, orient, " S")) {
+                        y = t;
+                        break;
+                    }
                 }
             }
             break;
         case KEY_LEFT:
         case 'j':
             if (length > 0) {
-                if (testlocation (boardhuman, length, x - 1, y, orient, " ")) {
-                    x--;
+                int t;
+                for (t = x - 1; t >= 0; t--) {
+                    if (testlocation (boardhuman, length, t, y, orient, " ")) {
+                        x = t;
+                        break;
+                    }
                 }
             } else {
-                if (testlocation (boardcomputer, 1, x - 1, y, orient, " S")) {
-                    x--;
+                int t;
+                for (t = y - 1; t >= 0; t--) {
+                    if (testlocation (boardcomputer, 1, t, y, orient, " S")) {
+                        x = t;
+                        break;
+                    }
                 }
             }
             break;
         case KEY_DOWN:
         case 'k':
             if (length > 0) {
-                if (testlocation (boardhuman, length, x, y + 1, orient, " ")) {
-                    y++;
+                int t;
+                for (t = y + 1; t < ysize; t++) {
+                    if (testlocation (boardhuman, length, x, t, orient, " ")) {
+                        y = t;
+                        break;
+                    }
                 }
             } else {
-                if (testlocation (boardcomputer, 1, x, y + 1, orient, " S")) {
-                    y++;
+                int t;
+                for (t = y + 1; t < ysize; t++) {
+                    if (testlocation (boardcomputer, 1, x, t, orient, " S")) {
+                        y = t;
+                        break;
+                    }
                 }
             }
             break;
         case KEY_RIGHT:
         case 'l':
             if (length > 0) {
-                if (testlocation (boardhuman, length, x + 1, y, orient, " ")) {
-                    x++;
+                int t;
+                for (t = x + 1; t < xsize; t++) {
+                    if (testlocation (boardhuman, length, t, y, orient, " ")) {
+                        x = t;
+                        break;
+                    }
                 }
             } else {
-                if (testlocation (boardcomputer, 1, x + 1, y, orient, " S")) {
-                    x++;
+                int t;
+                for (t = x + 1; t < xsize; t++) {
+                    if (testlocation (boardcomputer, 1, t, y, orient, " S")) {
+                        x = t;
+                        break;
+                    }
                 }
             }
             break;
diff --git a/board.c b/board.c
index 5483dc0fb84a7bc0d94002c9a9947b8852e5dfde..7a922f8160b1f766b7bf51fe1df76c7d4f93cf89 100644 (file)
--- a/board.c
+++ b/board.c
@@ -29,9 +29,9 @@ int isoneof (char t, char *symbs)
 
 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)) {
-        return 0;
+    if ((x < 0) || (x + orient * (length - 1) >= board->xsize) ||
+        (y < 0) || (y + (orient ^ 1) * (length - 1) >= board->ysize)) {
+            return 0;
     }
 
     int ret = 1;