#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
+#include <time.h>
#include "board.h"
#include "debug.h"
curs_set (0);
start_color ();
+ srand (time (NULL));
int xsize = 20;
int ysize = 10;
int xlogs = xhuman;
int ylogs = yhuman + ysize + 5;
-
- board_t *boardhuman = initboard (xsize, ysize);
board_t *boardcomputer = initboard (xsize, ysize);
+ int n = 0;
+ while (boats[n] > 0) {
+ drawship (boardcomputer, boats[n++]);
+ }
+ board_t *boardhuman = initboard (xsize, ysize);
int x, y, orient;
- int n = 0;
+ n = 0;
int length = boats[n];
- findlocation (boardhuman, length, &x, &y, &orient);
- putlocation (boardhuman, length, x, y, orient, 'S');
+ findlocation (boardhuman, length, &x, &y, &orient, " ");
int mode = 0;
displayboard (boardhuman, xhuman, yhuman, mode, 1);
int stop = 0;
while (!stop) {
+ if (length > 0) {
+ displayelement (length, xhuman + x, yhuman + y, orient, 'S', mode);
+ } else {
+ displayelement (1, xcomputer + x, ycomputer + y, orient, 'B', mode);
+ }
int c = getch ();
- putlocation (boardhuman, length, x, y, orient, ' ');
switch (c) {
case KEY_ESC:
#ifndef __PDCURSES__
case ':':
break;
case 'd':
- if (testlocation (boardhuman, length, x, y, orient ^ 1)) {
- orient ^= 1;
+ if (length > 0) {
+ if (testlocation (boardhuman, length, x, y, orient ^ 1, " ")) {
+ orient ^= 1;
+ }
}
break;
case KEY_UP:
case 'i':
- if (testlocation (boardhuman, length, x, y - 1, orient)) {
- y--;
+ if (length > 0) {
+ if (testlocation (boardhuman, length, x, y - 1, orient, " ")) {
+ y--;
+ }
+ } else {
+ if (testlocation (boardcomputer, 1, x, y - 1, orient, " S")) {
+ y--;
+ }
}
break;
case KEY_LEFT:
case 'j':
- if (testlocation (boardhuman, length, x - 1, y, orient)) {
- x--;
+ if (length > 0) {
+ if (testlocation (boardhuman, length, x - 1, y, orient, " ")) {
+ x--;
+ }
+ } else {
+ if (testlocation (boardcomputer, 1, x - 1, y, orient, " S")) {
+ x--;
+ }
}
break;
case KEY_DOWN:
case 'k':
- if (testlocation (boardhuman, length, x, y + 1, orient)) {
- y++;
+ if (length > 0) {
+ if (testlocation (boardhuman, length, x, y + 1, orient, " ")) {
+ y++;
+ }
+ } else {
+ if (testlocation (boardcomputer, 1, x, y + 1, orient, " S")) {
+ y++;
+ }
}
break;
case KEY_RIGHT:
case 'l':
- if (testlocation (boardhuman, length, x + 1, y, orient)) {
- x++;
+ if (length > 0) {
+ if (testlocation (boardhuman, length, x + 1, y, orient, " ")) {
+ x++;
+ }
+ } else {
+ if (testlocation (boardcomputer, 1, x + 1, y, orient, " S")) {
+ x++;
+ }
}
break;
case 'p':
- 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;
+ if (length > 0) {
+ putlocation (boardhuman, length, x, y, orient, 'S');
+ length = boats[++n];
+ if (length == 0) {
+ x = y = orient = 0;
+ if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
+ VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1));
+ stop = 1;
+ }
+ } else {
+ if (!findlocation (boardhuman, length, &x, &y, &orient, " ")) {
+ VERBOSE (WARNING, fprintf (stderr, "can't position for boat %d\n", length));
+ stop = 1;
+ }
+ }
+ } else {
+ char symb = (testlocation (boardcomputer, 1, x, y, orient, " ")) ? 'O' : 'X';
+ putlocation (boardcomputer, 1, x, y, orient, symb);
+ if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
+ VERBOSE (WARNING, fprintf (stderr, "can't position for bomb %d\n", 1));
+ stop = 1;
+ }
+
+ /* computer turn */
}
break;
case 'q':
case ERR:
break;
}
- putlocation (boardhuman, length, x, y, orient, 'S');
displayboard (boardhuman, xhuman, yhuman, mode, 1);
displayboard (boardcomputer, xcomputer, ycomputer, mode, 0);
}
return board;
}
-int testlocation (board_t *board, int length, int x, int y, int orient)
+int isoneof (char t, char *symbs)
+{
+ int ret = 0;
+ while (*symbs != '\0') {
+ if (t == *symbs) {
+ ret = 1;
+ break;
+ }
+ symbs++;
+ }
+ return ret;
+}
+
+int testlocation (board_t *board, int length, int x, int y, int orient, char *symbs)
{
if ((x < 0) || (x + orient * (length - 1) == board->xsize) ||
(y < 0) || (y + (orient^1) * (length - 1) == board->ysize)) {
int ret = 1;
int i;
for (i = 0; i < length; i++) {
- if (board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] != ' ') {
+ if (!isoneof (board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize], symbs)) {
ret = 0;
break;
}
return ret;
}
-int findlocation (board_t *board, int length, int *x, int *y, int *orient)
+int findlocation (board_t *board, int length, int *x, int *y, int *orient, char *symbs)
{
*x = *y = *orient = 0;
- while (!testlocation (board, length, *x, *y, *orient)) {
+ while (!testlocation (board, length, *x, *y, *orient, symbs)) {
(*x)++;
if (*x == board->xsize) {
*x = 0;
{
int i;
for (i = 0; i < length; i++) {
- board->tab[x + orient * i + (y + (orient^1) *i)* board->xsize] = symb;
+ board->tab[x + orient * i + (y + (orient ^ 1) * i) * board->xsize] = symb;
}
}
+void drawship (board_t *board, int length)
+{
+ int x, y, orient;
+
+ do {
+ x = rand () % board->xsize;
+ y = rand () % board->ysize;
+ orient = rand () % 2;
+ } while (!testlocation (board, length, x, y, orient, " "));
+
+
+ putlocation (board, length, x, y, orient, 'S');
+}
+
+/* vim: set ts=4 sw=4 et: */