/* depend: */
/* cflags: */
-/* linker: board.o debug.o display.o -lcurses */
-/* winlnk: board.o debug.o display.o -lpdcurses */
+/* linker: board.o debug.o display.o log.o -lcurses */
+/* winlnk: board.o debug.o display.o log.o -lpdcurses */
#include <curses.h>
#include <stdio.h>
#include "board.h"
#include "debug.h"
#include "display.h"
+#include "log.h"
/* static variables */
char *progname = NULL;
displayhelp (help, xhelp, yhelp, whelp);
- displaylogs ("Welcome to Battle Ships", wlogs, hlogs, xlogs, ylogs);
- displaylogs ("Put your ships on board", wlogs, hlogs, xlogs, ylogs);
+ log_t *logs = initlog (wlogs, hlogs);
+ displaylog (logs, "Welcome to Battle Ships", xlogs, ylogs);
+ displaylog (logs, "Put your ships on board", 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);
+ displaylog (logs, "Send bomb on computer board", xlogs, ylogs);
} else {
if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) {
VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length));
}
} else {
if (drawbomb (boardcomputer, x, y)) {
- displaylogs ("Computer ship hitted", wlogs, hlogs, xlogs, ylogs);
+ displaylog (logs, "Computer ship hitted", xlogs, ylogs);
}
if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 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);
+ displaylog (logs, "All computer ships sunk", xlogs, ylogs);
stop = stoploop ();
}
/* computer turn */
if (drawbomb (boardhuman, -1, -1)) {
- displaylogs ("Humain ship hitted", wlogs, hlogs, xlogs, ylogs);
+ displaylog (logs, "Humain ship hitted", 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);
+ displaylog (logs, "All human ships sunk", xlogs, ylogs);
stop = stoploop ();
}
}
endwin ();
+ freelog (logs);
+ freeboard (boardhuman);
+ freeboard (boardcomputer);
+
return 0;
}
return board;
}
+void freeboard (board_t *board)
+{
+ if (board) {
+ free (board->tab);
+ free (board->title);
+ }
+ free (board);
+}
+
int isoneof (char t, char *symbs)
{
int ret = 0;
board_t *initboard (char *title, int xsize, int ysize);
+void freeboard (board_t *board);
+
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, char *symbs);
}
}
-void displaylogs (char *msg, int width, int height, int xoffset, int yoffset)
-{
- 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);
- 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 (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 (*buffer[i] != 0) {
- mvaddstr (yoffset + i, xoffset, buffer[i]);
- }
- }
-}
-
+/* vim: set ts=4 sw=4 et: */
void displayhelp (char *msg, int xoffset, int yoffset, int length);
-void displaylogs (char *messages, int width, int height, int xoffset, int yoffset);
-
#endif /* __DISPLAY_H__ */
+
+/* vim: set ts=4 sw=4 et: */
--- /dev/null
+#include <curses.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "log.h"
+
+log_t *initlog (int width, int height)
+{
+ int i;
+
+ log_t *logs = (log_t *) malloc (sizeof (log_t));
+ logs->buffer = (char **) malloc (height * sizeof (char *));
+ for (i = 0; i < height; i++) {
+ logs->buffer[i] = (char *) calloc (1, width + 1);
+ }
+ logs->width = width;
+ logs->height = height;
+
+ return logs;
+}
+
+void freelog (log_t *logs)
+{
+ int i;
+ if (logs) {
+ if (logs->buffer) {
+ for (i = 0; i < logs->height; i++) {
+ free (logs->buffer[i]);
+ }
+ free (logs->buffer);
+ }
+ }
+ free (logs);
+}
+
+void displaylog (log_t *logs, char *msg, int xoffset, int yoffset)
+{
+ int i, j;
+
+ for (i = 0; i < logs->height; i++) {
+ if (*logs->buffer[i] == '\0') {
+ strncpy (logs->buffer[i], msg, logs->width);
+ break;
+ }
+ }
+ if (i == logs->height) {
+ for (i = 1; i < logs->height; i++) {
+ strcpy (logs->buffer[i - 1], logs->buffer[i]);
+ }
+ strncpy (logs->buffer[logs->height - 1], msg, logs->width);
+ }
+
+ mvaddch (yoffset - 1, xoffset - 1, ACS_ULCORNER);
+ mvaddch (yoffset + logs->height, xoffset - 1, ACS_LLCORNER);
+ mvaddch (yoffset + logs->height, xoffset + logs->width, ACS_LRCORNER);
+ mvaddch (yoffset - 1, xoffset + logs->width, ACS_URCORNER);
+
+ for (i = 0; i < logs->width; i++) {
+ mvaddch (yoffset - 1, xoffset + i, ACS_HLINE);
+ mvaddch (yoffset + logs->height, xoffset + i, ACS_HLINE);
+ for (j = 0; j < logs->height; j++) {
+ mvaddch (yoffset + j, xoffset + i, ' ');
+ }
+ }
+
+ for (i = 0; i < logs->height; i++) {
+ mvaddch (yoffset + i, xoffset - 1, ACS_VLINE);
+ mvaddch (yoffset + i, xoffset + logs->width, ACS_VLINE);
+ if (*logs->buffer[i] != 0) {
+ mvaddstr (yoffset + i, xoffset, logs->buffer[i]);
+ }
+ }
+}
+
+/* vim: set ts=4 sw=4 et: */
--- /dev/null
+#ifndef __LOG_H__
+#define __LOG_H__
+
+typedef struct {
+ char **buffer;
+ int width;
+ int height;
+ int xoffset;
+ int yoffset;
+} log_t;
+
+log_t *initlog (int width, int height);
+
+void freelog (log_t *logs);
+
+void displaylog (log_t *logs, char *msg, int xoffset, int yoffset);
+
+#endif /* __LOG_H__ */