From e46f9096c28a91e07ceb5db940b5d552ad3acfba Mon Sep 17 00:00:00 2001 From: Mazet Laurent Date: Thu, 30 May 2024 23:31:12 +0200 Subject: [PATCH] manage bombs for human --- battleships.c | 96 +++++++++++++++++++++++++++++++++++++++------------ board.c | 38 +++++++++++++++++--- board.h | 8 +++-- display.c | 21 +++++++++++ display.h | 2 ++ 5 files changed, 136 insertions(+), 29 deletions(-) diff --git a/battleships.c b/battleships.c index ee3b990..17f18ad 100644 --- a/battleships.c +++ b/battleships.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "board.h" #include "debug.h" @@ -78,6 +79,7 @@ int main (int argc, char *argv[]) curs_set (0); start_color (); + srand (time (NULL)); int xsize = 20; int ysize = 10; @@ -97,15 +99,17 @@ int main (int argc, char *argv[]) int xlogs = xhuman; int ylogs = yhuman + ysize + 5; - - board_t *boardhuman = initboard (xsize, ysize); board_t *boardcomputer = initboard (xsize, ysize); + int n = 0; + while (boats[n] > 0) { + drawship (boardcomputer, boats[n++]); + } + board_t *boardhuman = initboard (xsize, ysize); int x, y, orient; - int n = 0; + n = 0; int length = boats[n]; - findlocation (boardhuman, length, &x, &y, &orient); - putlocation (boardhuman, length, x, y, orient, 'S'); + findlocation (boardhuman, length, &x, &y, &orient, " "); int mode = 0; displayboard (boardhuman, xhuman, yhuman, mode, 1); @@ -117,8 +121,12 @@ int main (int argc, char *argv[]) int stop = 0; while (!stop) { + if (length > 0) { + displayelement (length, xhuman + x, yhuman + y, orient, 'S', mode); + } else { + displayelement (1, xcomputer + x, ycomputer + y, orient, 'B', mode); + } int c = getch (); - putlocation (boardhuman, length, x, y, orient, ' '); switch (c) { case KEY_ESC: #ifndef __PDCURSES__ @@ -129,40 +137,85 @@ int main (int argc, char *argv[]) case ':': break; case 'd': - if (testlocation (boardhuman, length, x, y, orient ^ 1)) { - orient ^= 1; + if (length > 0) { + if (testlocation (boardhuman, length, x, y, orient ^ 1, " ")) { + orient ^= 1; + } } break; case KEY_UP: case 'i': - if (testlocation (boardhuman, length, x, y - 1, orient)) { - y--; + if (length > 0) { + if (testlocation (boardhuman, length, x, y - 1, orient, " ")) { + y--; + } + } else { + if (testlocation (boardcomputer, 1, x, y - 1, orient, " S")) { + y--; + } } break; case KEY_LEFT: case 'j': - if (testlocation (boardhuman, length, x - 1, y, orient)) { - x--; + if (length > 0) { + if (testlocation (boardhuman, length, x - 1, y, orient, " ")) { + x--; + } + } else { + if (testlocation (boardcomputer, 1, x - 1, y, orient, " S")) { + x--; + } } break; case KEY_DOWN: case 'k': - if (testlocation (boardhuman, length, x, y + 1, orient)) { - y++; + if (length > 0) { + if (testlocation (boardhuman, length, x, y + 1, orient, " ")) { + y++; + } + } else { + if (testlocation (boardcomputer, 1, x, y + 1, orient, " S")) { + y++; + } } break; case KEY_RIGHT: case 'l': - if (testlocation (boardhuman, length, x + 1, y, orient)) { - x++; + if (length > 0) { + if (testlocation (boardhuman, length, x + 1, y, orient, " ")) { + x++; + } + } else { + if (testlocation (boardcomputer, 1, x + 1, y, orient, " S")) { + x++; + } } break; case 'p': - putlocation (boardhuman, length, x, y, orient, 'S'); - length = boats[++n]; - if (!findlocation (boardhuman, length, &x, &y, &orient)) { - VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length)); - stop = 1; + if (length > 0) { + putlocation (boardhuman, length, x, y, orient, 'S'); + length = boats[++n]; + if (length == 0) { + x = y = orient = 0; + if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) { + VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1)); + stop = 1; + } + } else { + if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) { + VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length)); + stop = 1; + } + } + } else { + char symb = (testlocation (boardcomputer, 1, x, y, orient, " ")) ? 'O' : 'X'; + putlocation (boardcomputer, 1, x, y, orient, symb); + if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) { + VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1)); + stop = 1; + } + + /* computer turn */ } break; case 'q': @@ -174,7 +227,6 @@ int main (int argc, char *argv[]) case ERR: break; } - putlocation (boardhuman, length, x, y, orient, 'S'); displayboard (boardhuman, xhuman, yhuman, mode, 1); displayboard (boardcomputer, xcomputer, ycomputer, mode, 0); } diff --git a/board.c b/board.c index 0c9c0d3..21e46f0 100644 --- a/board.c +++ b/board.c @@ -13,7 +13,20 @@ board_t *initboard (int xsize, int ysize) return board; } -int testlocation (board_t *board, int length, int x, int y, int orient) +int isoneof (char t, char *symbs) +{ + int ret = 0; + while (*symbs != '\0') { + if (t == *symbs) { + ret = 1; + break; + } + symbs++; + } + return ret; +} + +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)) { @@ -23,7 +36,7 @@ int testlocation (board_t *board, int length, int x, int y, int orient) int ret = 1; int i; for (i = 0; i < length; i++) { - if (board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] != ' ') { + if (!isoneof (board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize], symbs)) { ret = 0; break; } @@ -31,10 +44,10 @@ int testlocation (board_t *board, int length, int x, int y, int orient) return ret; } -int findlocation (board_t *board, int length, int *x, int *y, int *orient) +int findlocation (board_t *board, int length, int *x, int *y, int *orient, char *symbs) { *x = *y = *orient = 0; - while (!testlocation (board, length, *x, *y, *orient)) { + while (!testlocation (board, length, *x, *y, *orient, symbs)) { (*x)++; if (*x == board->xsize) { *x = 0; @@ -55,7 +68,22 @@ void putlocation (board_t *board, int length, int x, int y, int orient, char sym { int i; for (i = 0; i < length; i++) { - board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] = symb; + board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize] = symb; } } +void drawship (board_t *board, int length) +{ + int x, y, orient; + + do { + x = rand () % board->xsize; + y = rand () % board->ysize; + orient = rand () % 2; + } while (!testlocation (board, length, x, y, orient, " ")); + + + putlocation (board, length, x, y, orient, 'S'); +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/board.h b/board.h index 40c29d6..ad6610f 100644 --- a/board.h +++ b/board.h @@ -9,10 +9,14 @@ typedef struct { board_t *initboard (int xsize, int ysize); -int testlocation (board_t *board, int length, int x, int y, int orient); +int testlocation (board_t *board, int length, int x, int y, int orient, char *symbs); -int findlocation (board_t *board, int length, int *x, int *y, int *orient); +int findlocation (board_t *board, int length, int *x, int *y, int *orient, char *symbs); void putlocation (board_t *board, int length, int x, int y, int orient, char symb); +void drawship (board_t *board, int length); + #endif /* __BOARD_H__ */ + +/* vim: set ts=4 sw=4 et: */ diff --git a/display.c b/display.c index 1140dff..76081a7 100644 --- a/display.c +++ b/display.c @@ -57,6 +57,9 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show) } else { c = board->tab[x + y * board->xsize]; switch (c) { + case 'B': + setcolor (magenta); + break; case 'O': setcolor (blue); break; @@ -82,6 +85,24 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show) } } +void displayelement (int length, int x, int y, int orient, char symb, int show) +{ + int i; + switch (symb) { + case 'S': + setcolor (yellow); + break; + case 'B': + setcolor (magenta); + break; + } + char c = (show) ? symb : ' '; + for (i = 0; i < length; i++) { + mvaddch (y + (orient ^ 1) * i, x + orient * i, c); + } + setcolor (white); +} + void displayhelp (int width, int xoffset, int yoffset) { char *message = "Battle Ships\n------------\n- arrow or i, j, k, l to move\n- p to put ship or bomb\n- d to rotate ship\n- q to quit"; diff --git a/display.h b/display.h index a6e4490..36cc993 100644 --- a/display.h +++ b/display.h @@ -7,6 +7,8 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show); +void displayelement (int length, int x, int y, int orient, char symb, int show); + void displayhelp (int width, int xoffset, int yoffset); void displaylogs (char *messages, int width, int height, int xoffset, int yoffset); -- 2.30.2