Example #1
0
void TilePlacement::applyMove(const Move& move) {
	Direction direction = move.getDirection();
	Coordinates startCoords = move.getCoordinates();
	const Tiles& tiles = move.getTiles();
	auto segment = segmentFrom(startCoords, direction);

	if (!this->checkMove(move)) {
		throw InvalidMove();
	}

	uint index = 0;
	for (auto tile : tiles) {
		while (index < segment.length() && segment[index] != nullptr) {
			index++;
		}
		if (index < segment.length()) {
			atCoords(segment.coordinates(index)) = tile;
			index++;

		} else {
			throw InvalidMove();
		}
	}
}
Example #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);
}
Example #3
0
int
main(int argc, char **argv)
{
int NTiles, FromCol, ToCol;
unsigned char AutoFlag = 0;

	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);
			exit(1);
		}
		break;
	case 3:
		if (strcmp(argv[2], "a")) {
			Usage();
			exit(1);
		}
		NTiles = atoi(argv[1]);
		if (NTiles > MAXTILES || NTiles < MINTILES) {
			fprintf(stderr, "Range %d to %d\n", MINTILES, MAXTILES);
			exit(1);
		}
		AutoFlag = TRUE;
		break;
	default:
		Usage();
		exit(1);
	}
#ifdef NCURSES_VERSION
	trace(TRACE_MAXIMUM);
#endif
	initscr();
	if (!has_colors()) {
		endwin();
		puts("terminal doesn't support color.");
		exit(1);
	}
	start_color();
	{
	int i;
	for (i = 0; i < 9; i++)
		init_pair(i+1, COLOR_BLACK, TileColour[i]);
	}
	cbreak();
	if (LINES < 24) {
		endwin();
		fprintf(stderr, "Min screen length 24 lines\n");
		exit(1);
	}
	if(AutoFlag)
		leaveok(stdscr, TRUE);	/* Attempt to remove cursor */
	InitTiles(NTiles);
	DisplayTiles();
	if(AutoFlag) {
		do {
			noecho();
			AutoMove(0, 2, NTiles);
		} while(!Solved(NTiles));
		sleep(2);
	} else {
		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;
			}
		}
	}
	curs_set(1);
	endwin();
	exit(0);
}
Example #4
0
File: haino.c Project: shixv/test
    int
main(int argc, char **argv)
{
    int NTiles, FromCol, ToCol,opt;
    unsigned char AutoFlag = 0;
    if(argc<2 || argc>3){ Usage();exit(0);}
    while( (opt=getopt(argc,argv,"a:n:h") )!=-1)
        switch (opt)
        {
            case 'n':
                NTiles = atoi(optarg);
                if (NTiles > MAXTILES || NTiles < MINTILES)
                {   fprintf(stderr, "步数从 %d 自 %d之间\n", MINTILES, MAXTILES);
                    return EXIT_FAILURE;
                }
                AutoFlag = FALSE;
                break;
            case 'a':
                NTiles = atoi(optarg);
                if (NTiles > MAXTILES || NTiles < MINTILES)
                {       fprintf(stderr, "步数从 %d 自 %d之间\n", MINTILES, MAXTILES);
                    return EXIT_FAILURE;
                }
                AutoFlag = TRUE;
                break;
            case 'h':
            case '?':
            case ':':
                Usage();
                exit(0);
        }
    initscr();
    if (has_colors())
    {
        int i;
        int bg = COLOR_BLACK;
        start_color();
        if (use_default_colors() == OK)
            bg = -1;
        for (i = 0; i < 9; i++)
            init_pair(i + 1, bg, TileColour[i]);
    }
    cbreak();
    /*LINES和COLS是Curses内部变量,用于存储当前终端的行数和列数*/
    if (LINES < 24 || COLS < 80)          /*标准的终端为24行80列*/
    {
        endwin();
        fprintf(stderr, "当前小于 24x80 \n");
        return EXIT_FAILURE;
    }
    if (AutoFlag)
    {
        curs_set(0);
        leaveok(stdscr, TRUE);
    }
    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, "移 动 无 效 !!");
                refresh();
                beep();
                sleep(2);
                continue;
            }
            MakeMove(FromCol, ToCol);
            if (Solved(NTiles)) {
                mvprintw(STATUSLINE, 0, "恭喜!! 恭喜!! 你用 %d 步赢了", NMoves);
                refresh();
                sleep(5);
                break;
            }
        }
    }
    endwin();
    return EXIT_SUCCESS;
}