From: Laurent MAZET Date: Mon, 3 Jun 2024 09:26:43 +0000 (+0200) Subject: fix moves X-Git-Tag: v1.0~5 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=43f0a01ce3c75dcb3e6480c5eb73882d8c808e50;p=bs.git fix moves --- diff --git a/battleships.c b/battleships.c index 79477fc..babcf06 100644 --- a/battleships.c +++ b/battleships.c @@ -20,14 +20,14 @@ char *version = "0.9"; int boats[9] = {1, 1, 1, 1, 2, 2, 3, 4, 0}; char *help = - " Rotate shirp\n" - " Move up element\n" - " Move left element\n" - " Move down element\n" - " Move right element\n" - "

Put ship or bomb\n" + " Rotate ship\n" + " Move up\n" + " Move left\n" + " Move down\n" + " Move right\n" + "

Put ship/bomb\n" " Quit\n" - " Toggle case legend\n" + " 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 5483dc0..7a922f8 100644 --- 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;