Exemple #1
0
def generate_maze(x, y, config={}):
    structure = []
    for i in range(x): 
        l = []
        for j in range(y):
            l.append(1)
        structure.append(l)

    maze = {
        "x" : x, 
        "y" : y,
        "structure" : structure,
    }

    starting_pos_x = randint(1, x-2)
    if not starting_pos_x % 2:
        starting_pos_x -= 1
    starting_pos_y = randint(1, y-2)
    if not starting_pos_y % 2:
        starting_pos_y -= 1
    pos = (starting_pos_x, starting_pos_y)
       
    moves = [] 
    moves.append(pos)
    loop = config["loop"] if "loop" in config else 0
    
    print( "".join(["loop : ", str(loop)]))
    while len(moves) > 0 :
        possible_directions = []
        pos_x, pos_y = pos
        if pos_x + 2 < x and (is_wall(structure[pos_x + 2][pos_y]) or (randint(0, 100) < loop)) :
            possible_directions.append(EAST)
        if pos_x - 2 > 0 and (is_wall(structure[pos_x - 2][pos_y]) or (randint(0, 100) < loop)) :
            possible_directions.append(WEST)
        if pos_y + 2 < y and (is_wall(structure[pos_x][pos_y + 2]) or (randint(0, 100) < loop)) :
            possible_directions.append(SOUTH)
        if pos_y - 2 > 0 and (is_wall(structure[pos_x][pos_y - 2]) or (randint(0, 100) < loop)) :
            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_wall(maze, target_x, target_y, False)
            target_x, target_y = (target_x + direction_x, target_y + direction_y)
            dontadd = False
            if not is_wall(structure[target_x][target_y]):
                dontadd = True
            set_wall(maze, target_x, target_y, False)
            if not dontadd :
                pos = (target_x, target_y)
                moves.append(pos)
        else:
            pos = moves.pop()
    
    random_x, random_y = get_random_passable_position(maze["x"], maze["y"])
    maze.update({ "start_x" : random_x, "start_y" : random_y })

    return maze
Exemple #2
0
def move_in_this_direction(maze, traverser):
    time_required = 1
    passable = mazedef.get_passable_relative_direction(maze, traverser["x"], traverser["y"], traverser["direction"], mazedef.can_pass_through)
    direction = traverser["direction"]
    if passable[FRONT]:
        direction = traverser["direction"]
    else:
        if passable[LEFT] and passable[RIGHT]:
            return False, 0
        elif passable[LEFT]:
            direction = mazedef.get_direction_of(LEFT, direction)
        elif passable[RIGHT]:
            direction = mazedef.get_direction_of(RIGHT, direction)
        else:
            return False, 0

    traverser["direction"] = direction
    direction_mod_x, direction_mod_y = mazedef.get_direction_mod(traverser["direction"])
    target_pos_x, target_pos_y = traverser["x"] + direction_mod_x, traverser["y"] + direction_mod_y
    kime_required = mazedef.move_to(maze, target_pos_x, target_pos_y, traverser)
    return True, time_required