Created
July 8, 2014 05:57
-
-
Save ZwodahS/5450a2f2e2aab91c1a57 to your computer and use it in GitHub Desktop.
Tile-based maze generation based off http://www.emanueleferonato.com/2008/12/06/perfect-maze-generation-tile-based-version/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_maze(x, y, config={}): | |
structure = [] | |
for i in range(x): | |
l = [] | |
for j in range(y): | |
l.append(6) | |
structure.append(l) | |
maze = { | |
"x" : x, | |
"y" : y, | |
"structure" : structure, | |
} | |
starting_pos_x = randint(0, x-1) | |
if not starting_pos_x % 2: | |
starting_pos_x -= 1 | |
starting_pos_y = randint(0, y-1) | |
if not starting_pos_y % 2: | |
starting_pos_y -= 1 | |
pos = (1, 1) | |
moves = [] | |
moves.append(pos) | |
while len(moves) > 0 : | |
possible_directions = [] | |
pos_x, pos_y = pos | |
if pos_x + 2 < x and not is_passable(structure[pos_x + 2][pos_y]): | |
possible_directions.append(EAST) | |
if pos_x - 2 > 0 and not is_passable(structure[pos_x - 2][pos_y]): | |
possible_directions.append(WEST) | |
if pos_y + 2 < y and not is_passable(structure[pos_x][pos_y + 2]): | |
possible_directions.append(SOUTH) | |
if pos_y - 2 > 0 and not is_passable(structure[pos_x][pos_y - 2]): | |
possible_directions.append(NORTH) | |
if(len(possible_directions)): | |
direction = choice(possible_directions) | |
direction_x, direction_y = get_direction_mod(direction) | |
target_x, target_y = (pos_x + direction_x, pos_y + direction_y) | |
set_passable(structure, target_x, target_y, True) | |
target_x, target_y = (target_x + direction_x, target_y + direction_y) | |
set_passable(structure, target_x, target_y, True) | |
pos = (target_x, target_y) | |
moves.append(pos) | |
else: | |
pos = moves.pop() | |
while True: | |
random_x = randint(1, x -1) | |
random_y = randint(1, y -1) | |
if is_passable(structure[random_x][random_y]) : | |
maze.update({ "start_x" : random_x, "start_y" : random_y }) | |
break | |
return maze |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment