#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <unistd.h>
#include "board.h"
#include "debug.h"
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) {
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));
} 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:
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: */
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: */
#include <curses.h>
+#include <stdlib.h>
#include <string.h>
#include "board.h"
}
}
-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);
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]);
}
}
}