From d1e9e541089dcaf24fc4fba4789bbd713425e33a Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sat, 1 Jun 2024 21:53:24 +0200 Subject: [PATCH] add computer play --- battleships.c | 36 +++++++++++++++++++++++++++++++++++- board.c | 31 +++++++++++++++++++++++++++++++ board.h | 4 ++++ display.c | 33 ++++++++++++++++++++++++++++----- 4 files changed, 98 insertions(+), 6 deletions(-) diff --git a/battleships.c b/battleships.c index 18a1513..799b9a2 100644 --- a/battleships.c +++ b/battleships.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "board.h" #include "debug.h" @@ -117,7 +118,8 @@ int main (int argc, char *argv[]) displayhelp (whelp, xhelp, yhelp); - displaylogs ("Welcome to Battle Ships\0Put your ships on board\0", wlogs, hlogs, xlogs, ylogs); + displaylogs ("Welcome to Battle Ships", wlogs, hlogs, xlogs, ylogs); + displaylogs ("Put your ships on board", wlogs, hlogs, xlogs, ylogs); int stop = 0; while (!stop) { @@ -197,6 +199,7 @@ int main (int argc, char *argv[]) VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1)); stop = 1; } + displaylogs ("Send bomb on computer board", wlogs, hlogs, xlogs, ylogs); } else { if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) { VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length)); @@ -206,12 +209,43 @@ 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') { + displaylogs ("Computer ship hited", wlogs, hlogs, xlogs, ylogs); + } if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) { VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1)); stop = 1; } + if (testsunk (boardcomputer)) { + 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; + } + } + } /* computer turn */ + + if (drawbomb (boardhuman)) { + 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; + } + } + } } break; case KEY_ESC: diff --git a/board.c b/board.c index 21e46f0..108db53 100644 --- a/board.c +++ b/board.c @@ -86,4 +86,35 @@ void drawship (board_t *board, int length) putlocation (board, length, x, y, orient, 'S'); } +int drawbomb (board_t *board) +{ + int x, y; + + 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) { + putlocation (board, 1, x, y, 0, 'O'); + } else { + putlocation (board, 1, x, y, 0, 'X'); + } + return !ret; +} + +int testsunk (board_t *board) +{ + int ret = 1; + int i; + for (i = 0; i < board->xsize * board->ysize; i++) { + if (board->tab[i] == 'S') { + ret = 0; + break; + } + } + return ret; +} + /* vim: set ts=4 sw=4 et: */ diff --git a/board.h b/board.h index ad6610f..0dc3e9d 100644 --- a/board.h +++ b/board.h @@ -17,6 +17,10 @@ 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 testsunk (board_t *board); + #endif /* __BOARD_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/display.c b/display.c index 76081a7..e91111d 100644 --- a/display.c +++ b/display.c @@ -1,4 +1,5 @@ #include +#include #include #include "board.h" @@ -124,9 +125,29 @@ void displayhelp (int width, int xoffset, int yoffset) } } -void displaylogs (char *messages, int width, int height, int xoffset, int yoffset) +void displaylogs (char *msg, int width, int height, int xoffset, int yoffset) { - int i; + int i, j; + + static char **buffer = NULL; + if (buffer == NULL) { + buffer = (char **) malloc (height); + for (i = 0; i < height; i++) { + buffer[i] = (char *) calloc (1, width + 1); + } + } + for (i = 0; i < height; i++) { + if (*buffer[i] == '\0') { + strncpy (buffer[i], msg, width); + break; + } + } + if (i == height) { + for (i = 1; i < height; i++) { + strcpy (buffer[i - 1], buffer[i]); + } + strncpy (buffer[height - 1], msg, width); + } mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER); mvaddch (yoffset + height, xoffset - 1, ACS_LLCORNER); @@ -136,14 +157,16 @@ void displaylogs (char *messages, int width, int height, int xoffset, int yoffse for (i = 0; i < width; i++) { mvaddch (yoffset - 1, xoffset + i, ACS_HLINE); mvaddch (yoffset + height, xoffset + i, ACS_HLINE); + for (j = 0; j < height; j++) { + mvaddch (yoffset + j, xoffset + i, ' '); + } } for (i = 0; i < height; i++) { mvaddch (yoffset + i, xoffset - 1, ACS_VLINE); mvaddch (yoffset + i, xoffset + width, ACS_VLINE); - if (*messages != 0) { - mvaddstr (yoffset + i, xoffset, messages); - messages += strlen (messages) + 1; + if (*buffer[i] != 0) { + mvaddstr (yoffset + i, xoffset, buffer[i]); } } } -- 2.30.2