Beispiel #1
0
static bool
plyturn(void)
{
    ship_t *ss;
    bool hit;
    NCURSES_CONST char *m = NULL;

    prompt(1, "Where do you want to shoot? ", "");
    for (;;) {
	(void) getcoord(COMPUTER);
	if (hits[PLAYER][curx][cury]) {
	    prompt(1, "You shelled this spot already! Try again.", "");
	    beep();
	} else
	    break;
    }
    hit = IS_SHIP(board[COMPUTER][curx][cury]);
    hits[PLAYER][curx][cury] = (hit ? MARK_HIT : MARK_MISS);
    cgoto(cury, curx);
#ifdef A_COLOR
    if (has_colors()) {
	if (hit)
	    attron(COLOR_PAIR(COLOR_RED));
	else
	    attron(COLOR_PAIR(COLOR_GREEN));
    }
#endif /* A_COLOR */
    (void) addch((chtype) hits[PLAYER][curx][cury]);
#ifdef A_COLOR
    attrset(0);
#endif /* A_COLOR */

    prompt(1, "You %s.", hit ? "scored a hit" : "missed");
    if (hit && (ss = hitship(curx, cury))) {
	switch (rnd(5)) {
	case 0:
	    m = " You sank my %s!";
	    break;
	case 1:
	    m = " I have this sinking feeling about my %s....";
	    break;
	case 2:
	    m = " My %s has gone to Davy Jones's locker!";
	    break;
	case 3:
	    m = " Glub, glub -- my %s is headed for the bottom!";
	    break;
	case 4:
	    m = " You'll pick up survivors from my %s, I hope...!";
	    break;
	}
	(void) printw(m, ss->name);
	(void) beep();
	return (awinna() == -1);
    }
    return (hit);
}
int main() {
    int player = 0, moves = 0, end = -1, x, y, successplace;
    int board[3][3] = { { 0 } };

    do {
        successplace = 1;
        printf("Player %d's turn\n", player + 1);

        if (player == 1) {
            x = computer_ai(board, 1, moves);
        } else {
            do {
                x = getcoord(0); /* input and checks bounds */
                y = getcoord(1);
                successplace = placeitem(player, x-1, y-1, board, 0, 1);
            } while (successplace == 1);
        }

        printmap(board);

        if (moves > 3) {
            end = checkwin(board);
        }

        moves++;
        player = (player + 1) % 2;
    } while (end == -1 && moves < 9);

    if (end == -1) {
        printf("Draw\n");
    } else {
        printf("Player %d wins!\n", end + 1);
    }

    return EXIT_SUCCESS;
}
Beispiel #3
0
static void
initgame(void)
{
    int i, j, unplaced;
    ship_t *ss;

    (void) clear();
    (void) mvaddstr(0, 35, "BATTLESHIPS");
    (void) move(PROMPTLINE + 2, 0);
    announceopts();

    memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
    memset(hits, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
    for (i = 0; i < SHIPTYPES; i++) {
	ss = cpuship + i;

	ss->x =
	    ss->y =
	    ss->dir =
	    ss->hits = 0;
	ss->placed = FALSE;

	ss = plyship + i;

	ss->x =
	    ss->y =
	    ss->dir =
	    ss->hits = 0;
	ss->placed = FALSE;
    }

    /* draw empty boards */
    (void) mvaddstr(PYBASE - 2, PXBASE + 5, "Main Board");
    (void) mvaddstr(PYBASE - 1, PXBASE - 3, numbers);
    for (i = 0; i < BDEPTH; ++i) {
	(void) mvaddch(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
#ifdef A_COLOR
	if (has_colors())
	    attron(COLOR_PAIR(COLOR_BLUE));
#endif /* A_COLOR */
	(void) addch(' ');
	for (j = 0; j < BWIDTH; j++)
	    (void) addstr(" . ");
#ifdef A_COLOR
	(void) attrset(0);
#endif /* A_COLOR */
	(void) addch(' ');
	(void) addch((chtype) (i + 'A'));
    }
    (void) mvaddstr(PYBASE + BDEPTH, PXBASE - 3, numbers);
    (void) mvaddstr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
    (void) mvaddstr(CYBASE - 1, CXBASE - 3, numbers);
    for (i = 0; i < BDEPTH; ++i) {
	(void) mvaddch(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
#ifdef A_COLOR
	if (has_colors())
	    attron(COLOR_PAIR(COLOR_BLUE));
#endif /* A_COLOR */
	(void) addch(' ');
	for (j = 0; j < BWIDTH; j++)
	    (void) addstr(" . ");
#ifdef A_COLOR
	(void) attrset(0);
#endif /* A_COLOR */
	(void) addch(' ');
	(void) addch((chtype) (i + 'A'));
    }

    (void) mvaddstr(CYBASE + BDEPTH, CXBASE - 3, numbers);

    (void) mvprintw(HYBASE, HXBASE,
		    "To position your ships: move the cursor to a spot, then");
    (void) mvprintw(HYBASE + 1, HXBASE,
		    "type the first letter of a ship type to select it, then");
    (void) mvprintw(HYBASE + 2, HXBASE,
		    "type a direction ([hjkl] or [4862]), indicating how the");
    (void) mvprintw(HYBASE + 3, HXBASE,
		    "ship should be pointed. You may also type a ship letter");
    (void) mvprintw(HYBASE + 4, HXBASE,
		    "followed by `r' to position it randomly, or type `R' to");
    (void) mvprintw(HYBASE + 5, HXBASE,
		    "place all remaining ships randomly.");

    (void) mvaddstr(MYBASE, MXBASE, "Aiming keys:");
    (void) mvaddstr(SYBASE, SXBASE, "y k u    7 8 9");
    (void) mvaddstr(SYBASE + 1, SXBASE, " \\|/      \\|/ ");
    (void) mvaddstr(SYBASE + 2, SXBASE, "h-+-l    4-+-6");
    (void) mvaddstr(SYBASE + 3, SXBASE, " /|\\      /|\\ ");
    (void) mvaddstr(SYBASE + 4, SXBASE, "b j n    1 2 3");

    /* have the computer place ships */
    for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
	randomplace(COMPUTER, ss);
	placeship(COMPUTER, ss, FALSE);
    }

    ss = (ship_t *) NULL;
    do {
	char c, docked[SHIPTYPES + 2], *cp = docked;

	/* figure which ships still wait to be placed */
	*cp++ = 'R';
	for (i = 0; i < SHIPTYPES; i++)
	    if (!plyship[i].placed)
		*cp++ = plyship[i].symbol;
	*cp = '\0';

	/* get a command letter */
	prompt(1, "Type one of [%s] to pick a ship.", docked + 1);
	do {
	    c = (char) getcoord(PLAYER);
	} while
	    (!strchr(docked, c));

	if (c == 'R')
	    (void) ungetch('R');
	else {
	    /* map that into the corresponding symbol */
	    for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
		if (ss->symbol == c)
		    break;

	    prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
	    pgoto(cury, curx);
	}

	do {
	    c = (char) getch();
	} while
	    (!(strchr("hjklrR", c) || c == FF));

	if (c == FF) {
	    (void) clearok(stdscr, TRUE);
	    (void) refresh();
	} else if (c == 'r') {
	    assert(ss != 0);
	    prompt(1, "Random-placing your %s", ss->name);
	    randomplace(PLAYER, ss);
	    placeship(PLAYER, ss, TRUE);
	    error((char *) NULL);
	    ss->placed = TRUE;
	} else if (c == 'R') {
	    prompt(1, "Placing the rest of your fleet at random...", "");
	    for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
		if (!ss->placed) {
		    randomplace(PLAYER, ss);
		    placeship(PLAYER, ss, TRUE);
		    ss->placed = TRUE;
		}
	    error((char *) NULL);
	} else if (strchr("hjkl8462", c)) {
	    assert(ss != 0);
	    ss->x = curx;
	    ss->y = cury;

	    switch (c) {
	    case 'k':
	    case '8':
		ss->dir = N;
		break;
	    case 'j':
	    case '2':
		ss->dir = S;
		break;
	    case 'h':
	    case '4':
		ss->dir = W;
		break;
	    case 'l':
	    case '6':
		ss->dir = E;
		break;
	    }

	    if (checkplace(PLAYER, ss, TRUE)) {
		placeship(PLAYER, ss, TRUE);
		error((char *) NULL);
		ss->placed = TRUE;
	    }
	}

	for (unplaced = i = 0; i < SHIPTYPES; i++)
	    unplaced += !plyship[i].placed;
    } while
	(unplaced);

    turn = rnd(2);

    (void) mvprintw(HYBASE, HXBASE,
		    "To fire, move the cursor to your chosen aiming point   ");
    (void) mvprintw(HYBASE + 1, HXBASE,
		    "and strike any key other than a motion key.            ");
    (void) mvprintw(HYBASE + 2, HXBASE,
		    "                                                       ");
    (void) mvprintw(HYBASE + 3, HXBASE,
		    "                                                       ");
    (void) mvprintw(HYBASE + 4, HXBASE,
		    "                                                       ");
    (void) mvprintw(HYBASE + 5, HXBASE,
		    "                                                       ");

    (void) prompt(0, "Press any key to start...", "");
    (void) getch();
}