示例#1
0
文件: bs.c 项目: kusumi/DragonFlyBSD
/* generate a valid random ship placement into px,py */
static void
randomplace(int b, ship_t *ss)
{
    do {
		ss->y = rnd(BDEPTH);
		ss->x = rnd(BWIDTH);
		ss->dir = rnd(2) ? E : S;
    } while (!checkplace(b, ss, FALSE));
}
示例#2
0
static void
randomplace(int b, ship_t * ss)
/* generate a valid random ship placement into px,py */
{

    do {
	ss->dir = rnd(2) ? E : S;
	ss->x = rnd(BWIDTH - (ss->dir == E ? ss->length : 0));
	ss->y = rnd(BDEPTH - (ss->dir == S ? ss->length : 0));
    } while
	(!checkplace(b, ss, FALSE));
}
示例#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();
}