From: Laurent Mazet Date: Sun, 26 May 2024 22:16:58 +0000 (+0200) Subject: add help message, logs window and computer board X-Git-Tag: v1.0~15 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=fa791aacb71a11e151a8eef6235a89c2781ee50d;p=bs.git add help message, logs window and computer board --- diff --git a/battleships.c b/battleships.c index 0f4b2cf..ee3b990 100644 --- a/battleships.c +++ b/battleships.c @@ -15,6 +15,8 @@ char *progname = NULL; char *version = "0.1"; +int boats[9] = {1, 1, 1, 1, 2, 2, 3, 4, 0}; + /* help message */ int usage (int ret) { @@ -79,26 +81,44 @@ int main (int argc, char *argv[]) int xsize = 20; int ysize = 10; - int xoffset = 3; - int yoffset = 3; + int whelp = 20; + int wlogs = 70; + int hlogs = 4; + + int xhuman = 3; + int yhuman = 3; + + int xcomputer = xhuman + xsize + 5; + int ycomputer = yhuman; + + int xhelp = xcomputer + xsize + 4; + int yhelp = ycomputer - 1; - board_t *board = initboard (xsize, ysize); - board->tab[3] = 'X'; - board->tab[4] = 'O'; - board->tab[5] = 'S'; + int xlogs = xhuman; + int ylogs = yhuman + ysize + 5; + + + board_t *boardhuman = initboard (xsize, ysize); + board_t *boardcomputer = initboard (xsize, ysize); int x, y, orient; - int boatlength = (random () % 4) + 1; - findlocation (board, boatlength, &x, &y, &orient); - putlocation (board, boatlength, x, y, orient, 'S'); + int n = 0; + int length = boats[n]; + findlocation (boardhuman, length, &x, &y, &orient); + putlocation (boardhuman, length, x, y, orient, 'S'); int mode = 0; - displayboard (board, xoffset, yoffset, mode); + displayboard (boardhuman, xhuman, yhuman, mode, 1); + displayboard (boardcomputer, xcomputer, ycomputer, mode, 0); + + displayhelp (whelp, xhelp, yhelp); + + displaylogs ("Welcome to Battle Ships\0Put your ships on board\0", wlogs, hlogs, xlogs, ylogs); int stop = 0; while (!stop) { int c = getch (); - putlocation (board, boatlength, x, y, orient, ' '); + putlocation (boardhuman, length, x, y, orient, ' '); switch (c) { case KEY_ESC: #ifndef __PDCURSES__ @@ -109,39 +129,39 @@ int main (int argc, char *argv[]) case ':': break; case 'd': - if (testlocation (board, boatlength, x, y, orient ^ 1)) { + if (testlocation (boardhuman, length, x, y, orient ^ 1)) { orient ^= 1; } break; case KEY_UP: case 'i': - if (testlocation (board, boatlength, x, y - 1, orient)) { + if (testlocation (boardhuman, length, x, y - 1, orient)) { y--; } break; case KEY_LEFT: case 'j': - if (testlocation (board, boatlength, x - 1, y, orient)) { + if (testlocation (boardhuman, length, x - 1, y, orient)) { x--; } break; case KEY_DOWN: case 'k': - if (testlocation (board, boatlength, x, y + 1, orient)) { + if (testlocation (boardhuman, length, x, y + 1, orient)) { y++; } break; case KEY_RIGHT: case 'l': - if (testlocation (board, boatlength, x + 1, y, orient)) { + if (testlocation (boardhuman, length, 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 (WARNING, fprintf (stderr, "can't position for boat %d\n", boatlength)); + 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; } break; @@ -154,8 +174,9 @@ int main (int argc, char *argv[]) case ERR: break; } - putlocation (board, boatlength, x, y, orient, 'S'); - displayboard (board, xoffset, yoffset, mode); + putlocation (boardhuman, length, x, y, orient, 'S'); + displayboard (boardhuman, xhuman, yhuman, mode, 1); + displayboard (boardcomputer, xcomputer, ycomputer, mode, 0); } endwin (); diff --git a/display.c b/display.c index b4f48f5..1140dff 100644 --- a/display.c +++ b/display.c @@ -1,4 +1,5 @@ #include +#include #include "board.h" #include "display.h" @@ -28,7 +29,7 @@ void setcolor (color_t color) attrset (COLOR_PAIR (color)); } -void displayboard (board_t *board, int xoffset, int yoffset, int mode) +void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show) { int x, y; @@ -63,7 +64,11 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode) setcolor (red); break; case 'S': - setcolor (yellow); + if (!show) { + c = ' '; + } else { + setcolor (yellow); + } break; } if (!mode) { @@ -76,3 +81,49 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode) } } } + +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"; + + int i = 0, j = 0, l = 0; + char c; + while ((c = message[l++]) != 0) { + if (c != '\n') { + if (i == width) { + i = 0; + j++; + } + mvaddch (yoffset + j, xoffset + i, c); + i++; + } else { + i = 0; + j++; + } + } +} + +void displaylogs (char *messages, int width, int height, int xoffset, int yoffset) +{ + int i; + + mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER); + mvaddch (yoffset + height, xoffset - 1, ACS_LLCORNER); + mvaddch (yoffset + height, xoffset + width, ACS_LRCORNER); + mvaddch (yoffset - 1, xoffset + width, ACS_URCORNER); + + for (i = 0; i < width; i++) { + mvaddch (yoffset - 1, xoffset + i, ACS_HLINE); + mvaddch (yoffset + height, xoffset + i, ACS_HLINE); + } + + 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; + } + } +} + diff --git a/display.h b/display.h index 9c95a43..a6e4490 100644 --- a/display.h +++ b/display.h @@ -5,6 +5,10 @@ #define KEY_ESC 0x1b -void displayboard (board_t *board, int xoffset, int yoffset, int mode); +void displayboard (board_t *board, int xoffset, int yoffset, int mode, int show); + +void displayhelp (int width, int xoffset, int yoffset); + +void displaylogs (char *messages, int width, int height, int xoffset, int yoffset); #endif /* __DISPLAY_H__ */