From abf0cf34a65b81ad4275e35beef815a388ee85b2 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sat, 25 May 2024 16:09:00 +0200 Subject: [PATCH] add functions to put ships on board --- battleships.c | 39 ++++++++++++++++++++++++++++----------- board.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ board.h | 6 ++++++ 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/battleships.c b/battleships.c index 7e1f010..6ac20db 100644 --- a/battleships.c +++ b/battleships.c @@ -75,7 +75,7 @@ int main (int argc, char *argv[]) keypad (stdscr, TRUE); /* enable cursor keys */ scrollok (stdscr, TRUE); /* enable scrolling in main window */ - curs_set (2); + curs_set (0); start_color (); int xsize = 20; @@ -88,16 +88,19 @@ int main (int argc, char *argv[]) board->tab[4] = 'O'; board->tab[5] = 'S'; + int x, y, orient; + int boatlength = (random () % 4) + 1; + findlocation (board, boatlength, &x, &y, &orient); + putlocation (board, boatlength, x, y, orient, 'S'); + int mode = 0; displayboard (board, xoffset, yoffset, mode); - int x = (xsize + 1) / 2 - 1; - int y = (ysize + 1) / 2 - 1; - int stop = 0; while (!stop) { - move (y + yoffset, x + xoffset); - switch (getch ()) { + int c = getch (); + putlocation (board, boatlength, x, y, orient, ' '); + switch (c) { case KEY_ESC: #ifndef __PDCURSES__ if (getch () != KEY_ESC) { @@ -106,40 +109,54 @@ int main (int argc, char *argv[]) #endif /* __PDCURSES__ */ case ':': break; + case 'd': + if (testlocation (board, boatlength, x, y, orient ^ 1)) { + orient ^= 1; + } + break; case KEY_UP: case 'i': - if (y > 0) { + if (testlocation (board, boatlength, x, y - 1, orient)) { y--; } break; case KEY_LEFT: case 'j': - if (x > 0) { + if (testlocation (board, boatlength, x - 1, y, orient)) { x--; } break; case KEY_DOWN: case 'k': - if (y < ysize - 1) { + if (testlocation (board, boatlength, x, y + 1, orient)) { y++; } break; case KEY_RIGHT: case 'l': - if (x < xsize - 1) { + if (testlocation (board, boatlength, 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 (ERROR, fprintf (stderr, "can't position for boat %d\n", boatlength)); + exit (1); + } + break; case 'q': stop = 1; break; case 't': mode ^= 1; - displayboard (board, xoffset, yoffset, mode); break; case ERR: break; } + putlocation (board, boatlength, x, y, orient, 'S'); + displayboard (board, xoffset, yoffset, mode); } endwin (); diff --git a/board.c b/board.c index 6d91d0d..aef8af5 100644 --- a/board.c +++ b/board.c @@ -87,3 +87,50 @@ void displayboard (board_t *board, int xoffset, int yoffset, int mode) } } } + +int testlocation (board_t *board, int length, int x, int y, int orient) +{ + if ((x < 0) || (x + orient * (length - 1) == board->xsize) || + (y < 0) || (y + (orient^1) * (length - 1) == board->ysize)) { + return 0; + } + + int ret = 1; + int i; + for (i = 0; i < length; i++) { + if (board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] != ' ') { + ret = 0; + break; + } + } + return ret; +} + +int findlocation (board_t *board, int length, int *x, int *y, int *orient) +{ + *x = *y = *orient = 0; + while (!testlocation (board, length, *x, *y, *orient)) { + (*x)++; + if (*x == board->xsize) { + *x = 0; + (*y)++; + if (*y == board->ysize) { + *x = *y = 0; + *orient ^= 1; + if (*orient == 0) { + return 0; + } + } + } + } + return 1; +} + +void putlocation (board_t *board, int length, int x, int y, int orient, char symb) +{ + int i; + for (i = 0; i < length; i++) { + board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] = symb; + } +} + diff --git a/board.h b/board.h index 7c83702..5ba3ebc 100644 --- a/board.h +++ b/board.h @@ -11,4 +11,10 @@ board_t *initboard (int xsize, int ysize); void displayboard (board_t *board, int xoffset, int yoffset, int mode); +int testlocation (board_t *board, int length, int x, int y, int orient); + +int findlocation (board_t *board, int length, int *x, int *y, int *orient); + +void putlocation (board_t *board, int length, int x, int y, int orient, char symb); + #endif /* __BOARD_H__ */ -- 2.30.2