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)
{
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__
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;
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 ();
#include <curses.h>
+#include <string.h>
#include "board.h"
#include "display.h"
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;
setcolor (red);
break;
case 'S':
- setcolor (yellow);
+ if (!show) {
+ c = ' ';
+ } else {
+ setcolor (yellow);
+ }
break;
}
if (!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;
+ }
+ }
+}
+