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;
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) {
#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 ();
}
}
}
+
+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;
+ }
+}
+
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__ */