return ret;
}
+/* stop loop */
+int stoploop (void)
+{
+ int stop = 0;
+ while (!stop) {
+ switch (getch ()) {
+ case KEY_ESC:
+ case 'q':
+ stop = 1;
+ }
+ }
+ return stop;
+}
+
/* main function */
int main (int argc, char *argv[])
{
}
}
} else {
- char symb = (testlocation (boardcomputer, 1, x, y, orient, " ")) ? 'O' : 'X';
- putlocation (boardcomputer, 1, x, y, orient, symb);
- if (symb == 'X') {
+ if (drawbomb (boardcomputer, x, y)) {
displaylogs ("Computer ship hited", wlogs, hlogs, xlogs, ylogs);
}
if (!findlocation (boardcomputer, 1, &x, &y, &orient, " S")) {
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;
- }
- }
+ stop = stoploop ();
}
/* computer turn */
- if (drawbomb (boardhuman)) {
+ if (drawbomb (boardhuman, -1, -1)) {
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;
- }
- }
+ stop = stoploop ();
}
}
break;
putlocation (board, length, x, y, orient, 'S');
}
-int drawbomb (board_t *board)
+int drawbomb (board_t *board, int x, int y)
{
- int x, y;
-
- do {
- x = rand () % board->xsize;
- y = rand () % board->ysize;
- } while (!testlocation (board, 1, x, y, 0, " S"));
+ if (x == -1) {
+ 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) {