Beispiel #1
0
static void
DisplayTiles(void)
{
    int Line, peg, SlotNo;
    char TileBuf[BUFSIZ];

    erase();
    MvAddStr(1, 24, "T O W E R S   O F   H A N O I");
    MvAddStr(3, 34, "SJR 1990");
    MvPrintw(19, 5, "Moves : %d", NMoves);
    (void) attrset(A_REVERSE);
    MvAddStr(BASELINE, 8,
	     "                                                               ");

    for (Line = TOPLINE; Line < BASELINE; Line++) {
	MvAddCh(Line, LEFTPEG, ' ');
	MvAddCh(Line, MIDPEG, ' ');
	MvAddCh(Line, RIGHTPEG, ' ');
    }
    MvAddCh(BASELINE, LEFTPEG, '1');
    MvAddCh(BASELINE, MIDPEG, '2');
    MvAddCh(BASELINE, RIGHTPEG, '3');
    (void) attrset(A_NORMAL);

    /* Draw tiles */
    for (peg = 0; peg < NPEGS; peg++) {
	for (SlotNo = 0; SlotNo < Pegs[peg].Count; SlotNo++) {
	    size_t len = Pegs[peg].Length[SlotNo];
	    if (len < sizeof(TileBuf) - 1 && len < (size_t) PegPos[peg]) {
		memset(TileBuf, ' ', len);
		TileBuf[len] = '\0';
		if (has_colors())
		    (void) attrset((attr_t) COLOR_PAIR(LENTOIND(len)));
		else
		    (void) attrset(A_REVERSE);
		MvAddStr(BASELINE - (SlotNo + 1),
			 (PegPos[peg] - (int) len / 2),
			 TileBuf);
	    }
	}
    }
    (void) attrset(A_NORMAL);
    refresh();
}
Beispiel #2
0
int
main(int argc, char **argv)
{
    int NTiles, FromCol, ToCol;

    setlocale(LC_ALL, "");

    switch (argc) {
    case 1:
	NTiles = DEFAULTTILES;
	break;
    case 2:
	NTiles = atoi(argv[1]);
	if (NTiles > MAXTILES || NTiles < MINTILES) {
	    fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
	    ExitProgram(EXIT_FAILURE);
	}
	break;
    case 3:
	if (strcmp(argv[2], "a")) {
	    Usage();
	    ExitProgram(EXIT_FAILURE);
	}
	NTiles = atoi(argv[1]);
	if (NTiles > MAXTILES || NTiles < MINTILES) {
	    fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
	    ExitProgram(EXIT_FAILURE);
	}
	AutoFlag = TRUE;
	break;
    default:
	Usage();
	ExitProgram(EXIT_FAILURE);
    }
#ifdef TRACE
    trace(TRACE_MAXIMUM);
#endif
    initscr();
    if (has_colors()) {
	int i;
	short bg = COLOR_BLACK;
	start_color();
#if HAVE_USE_DEFAULT_COLORS
	if (use_default_colors() == OK)
	    bg = -1;
#endif
	for (i = 0; i < 9; i++)
	    init_pair((short) (i + 1), bg, TileColour[i]);
    }
    cbreak();
    if (LINES < 24) {
	endwin();
	fprintf(stderr, "Min screen length 24 lines\n");
	ExitProgram(EXIT_FAILURE);
    }
    if (AutoFlag) {
	curs_set(0);
	leaveok(stdscr, TRUE);	/* Attempt to remove cursor */
    }
    InitTiles(NTiles);
    DisplayTiles();
    if (AutoFlag) {
	do {
	    noecho();
	    AutoMove(0, 2, NTiles);
	} while (!Solved(NTiles));
	sleep(2);
    } else {
	echo();
	for (;;) {
	    if (GetMove(&FromCol, &ToCol))
		break;
	    if (InvalidMove(FromCol, ToCol)) {
		MvAddStr(STATUSLINE, 0, "Invalid Move !!");
		refresh();
		beep();
		continue;
	    }
	    MakeMove(FromCol, ToCol);
	    if (Solved(NTiles)) {
		MvPrintw(STATUSLINE, 0,
			 "Well Done !! You did it in %d moves", NMoves);
		refresh();
		sleep(5);
		break;
	    }
	}
    }
    endwin();
    ExitProgram(EXIT_SUCCESS);
}
static
void
explode(int row, int col)
{
    chtype bold;
    erase();
    MvPrintw(row, col, "-");
    showit();

    init_pair(1, get_colour(&bold), my_bg);
    (void) attrset(COLOR_PAIR(1) | bold);
    MvPrintw(row - 1, col - 1, " - ");
    MvPrintw(row + 0, col - 1, "-+-");
    MvPrintw(row + 1, col - 1, " - ");
    showit();

    init_pair(1, get_colour(&bold), my_bg);
    (void) attrset(COLOR_PAIR(1) | bold);
    MvPrintw(row - 2, col - 2, " --- ");
    MvPrintw(row - 1, col - 2, "-+++-");
    MvPrintw(row + 0, col - 2, "-+#+-");
    MvPrintw(row + 1, col - 2, "-+++-");
    MvPrintw(row + 2, col - 2, " --- ");
    showit();

    init_pair(1, get_colour(&bold), my_bg);
    (void) attrset(COLOR_PAIR(1) | bold);
    MvPrintw(row - 2, col - 2, " +++ ");
    MvPrintw(row - 1, col - 2, "++#++");
    MvPrintw(row + 0, col - 2, "+# #+");
    MvPrintw(row + 1, col - 2, "++#++");
    MvPrintw(row + 2, col - 2, " +++ ");
    showit();

    init_pair(1, get_colour(&bold), my_bg);
    (void) attrset(COLOR_PAIR(1) | bold);
    MvPrintw(row - 2, col - 2, "  #  ");
    MvPrintw(row - 1, col - 2, "## ##");
    MvPrintw(row + 0, col - 2, "#   #");
    MvPrintw(row + 1, col - 2, "## ##");
    MvPrintw(row + 2, col - 2, "  #  ");
    showit();

    init_pair(1, get_colour(&bold), my_bg);
    (void) attrset(COLOR_PAIR(1) | bold);
    MvPrintw(row - 2, col - 2, " # # ");
    MvPrintw(row - 1, col - 2, "#   #");
    MvPrintw(row + 0, col - 2, "     ");
    MvPrintw(row + 1, col - 2, "#   #");
    MvPrintw(row + 2, col - 2, " # # ");
    showit();
}