コード例 #1
0
ファイル: visual.c プロジェクト: hspak/maze
void update_maze(long cl, char (*maze)[cl], long rl, long sr, long sc, long sr_old, long sc_old, int type)
{
        int srl = sr*2 + 1;
        int scl = sc*2 + 1;
        int e_off = 0;
        char symbol[5] = {'o', 'x', '*', 'S', 'E'};

        if (type == BACK) {
                maze[sr_old*2+1][sc_old*2+1] = 'x';
        }

        // a small hack to get rid of the double E
        if (type == END) {
                e_off = 2;
        }

        if (sr > sr_old)
                maze[srl-1][scl] = symbol[type-e_off];
        else if (sr < sr_old)
                maze[srl+1][scl] = symbol[type-e_off];
        else if (sc > sc_old)
                maze[srl][scl-1] = symbol[type-e_off];
        else if (sc < sc_old)
                maze[srl][scl+1] = symbol[type-e_off];
        else if (sc != sc_old && sr != sr_old)
                fprintf(stderr, "error: something broke traversing (%ld, %ld) -> (%ld, %ld)\n",
                        sc_old, sr_old, sc, sr);

        if (maze[srl][scl] != 'E' && maze[srl][scl] != 'S')
                maze[srl][scl] = symbol[type];
        display_maze(cl, maze, rl);

        // deprecated, but for aesthetics anyway
        usleep(400000);
}
コード例 #2
0
ファイル: maze.cpp プロジェクト: glt2109/cs3136
/*
 * Recursively "dig" maze corridors starting from row r and column c.
 *
 * The maze matrix m is initialized in alternating walls and cells
 * like this:
 *
 *      #########
 *      #.#.#.#.#
 *      #########
 *      #.#.#.#.#
 *      #########
 *      #.#.#.#.#
 *      #########
 */
void dig_maze(vector<vector<char> >& m, int r, int c, char *wall)
{
    // Are we out of bounds?
    if (r < 0 || c < 0 || r >= m.size() || c >= m[0].size())
        return;
    
    // Are we on a wall, or on a cell that we have visited already?
    if (m[r][c] == MAZE_WALL || m[r][c] == MAZE_SPACE)
        return;
    
    // The wall pointer points to the wall that we jumped over
    // between recursive calls.  Knock it down.
    // (wall == NULL for the 1st invocation of this function.)
    if (wall) {
        *wall = MAZE_SPACE;
    }
    
    // Dig this cell.
    m[r][c] = MAZE_SPACE;
    display_maze(m);
    
    // Randomly decide the order in which we explore the directions
    // N, S, E, W.  We use STL's random_shuffle() to shuffle an array.
    char D[4] = { 'N', 'S', 'E', 'W' };
    random_shuffle(D, D+4, random_n);
    
    for (int i = 0; i < 4; i++) {
        
        // m[rr][cc] will be the cell that we'll try to dig.
        int rr = r;
        int cc = c;
        char *wall;
        
        switch (D[i]) {
            case 'N':
                rr -= 2;
                wall = &m[r-1][c];
                break;
            case 'S':
                rr += 2;
                wall = &m[r+1][c];
                break;
            case 'W':
                cc -= 2;
                wall = &m[r][c-1];
                break;
            case 'E':
                cc += 2;
                wall = &m[r][c+1];
                break;
            default:
                cerr << "unknown direction" << endl;
                exit(1);
        }
        
        // recursively dig from (rr,cc).
        dig_maze(m, rr, cc, wall);
    }
}
コード例 #3
0
int
main(int argc, char *argv[])
{
	algorithm carvers[] = {
		carve_aldous_broder,
		carve_binary_tree
	};

	int size = argc > 1 ? atoi(argv[1]) : 8; // rudimentary CLI
	srandom((int) &size); // An address is sufficiently random. (+1 Recycling)

	Maze test_maze = init_maze(size, size, 15);
	carve_maze(&test_maze, carvers[BINARY_TREE], SW); //
	display_maze(&test_maze);
	puts("");

	reset_maze(&test_maze, 15);
	carve_maze(&test_maze, carvers[ALDOUS_BRODER], 0);
	display_maze(&test_maze);

	destroy_maze(&test_maze);
	return 0;
}
コード例 #4
0
ファイル: astar.c プロジェクト: ubertil/my_projects
int		main(int ac, char **av)
{
  t_maze	*maze;

  if (ac != 2)
    return (my_putstr_err(USAGE));
  if ((maze = init_maze()) == NULL)
    return (my_putstr_err(ERR_MALLOC));
  if (open_file(maze, av[1]) == 0)
    return (my_putstr_err(WRONG_FORMAT));
  if (solve_maze(maze) == 1)
    display_maze(maze);
  else
    printf("%s\n", NOTHING);
  free_all(maze);
  return (0);
}
コード例 #5
0
ファイル: maze_controller.c プロジェクト: thekoc/CCpp2016
int main(int argc, char const *argv[]) {
    int wall[MAZE_HEIGHT][MAZE_WIDTH] = {
        {1, 0, 1, 1},
        {1, 0, 0, 1},
        {1, 1, 0, 1},
        {1, 1, 0, 1}
    };
    Maze *maze = construct_maze(wall, 1, 0, 2, 3);
    while (1) {
        display_maze(maze);
        char input = getchar();
        int dx = 0, dy = 0;
        switch (input) {
            case 'w':
                dy = -1;
                break;
            case 's':
                dy = 1;
                break;
            case 'a':
                dx = -1;
                break;
            case 'd':
                dx = 1;
                break;
            case '\n':
                break;
            default:
                printf("An Error Occured");
        }
        condition cond = move_player(maze, dx, dy);
        if (cond == WIN) {
            display_win_page();
            break;
        }
    }
    return 0;
}