Example Trippy Maze Generator

ASnake Documentation

Table Of Contents

    Uses cellular automata to generate a maze.

    This code requires an actual console, ASnake Code Playground will not work.

    Also do not run if flashing lights is an issue!

    Code:

    import random
    from os import system, get_terminal_size
    from time import sleep
    from platform import system as OSCheck
    
    int consoleLength is get_terminal_size()[0]-1
    int consoleHeight is get_terminal_size()[1]//1.2 into int
    
    str os = OSCheck()
    clearConsole does 
        if 'Windows' is os then 'cls' to system  
        else 'clear' to system
    str:
        wall is u"\u2588"
        notWall is ' '
        xb
    list:
        board ; yb
    int:
        ybb ; xbb
    board is consoleHeight : ybb [random.choice([notWall,notWall,wall]) for xbb in range(0,consoleLength)]
    clearConsole
    for yb in board:
        yb : xb pipe xb,end='' to print
        pipe '\n',end='' to print
    'original'
    clearConsole
    
    genMaze does
        int: 
            change 0 ; count ; iteration ; x ; y
        for iteration in  pipe 0,iterations to range
            change 0
            for y in board to len to range
                for x in len(board[y]) to range
                    count 0
                    if y < len(board)-1:
                        if board[y+1][x] is wall then count++
                        if x < len(board[y])-1 and board[y+1][x+1] is wall do count+=mode
                        if x > 0 and board[y+1][x-1] is wall then count+=mode
                    if y > 0:
                        if board[y-1][x] is wall then count++
                        if x < len(board[y])-1 and board[y-1][x+1] is wall then count+=mode
                        if x > 0 and board[y-1][x-1] is wall then count+=mode
                    if x < len(board[y])-1:
                        if board[y][x+1] is wall then count++
                    if x > 0 and board[y][x-1] is wall do count++
                    if board[y][x] is wall:
                        if groove < count < 4 do nothing
                        else board[y][x] is notWall then change++
                    elif count 3 and board[y][x] is notWall:
                        board[y][x] is wall then change++
            if show
                clearConsole
                for yb in board
                    yb : xb pipe xb,end='' to print
                    pipe '\n',end='' to print
                0.08 to sleep
            if change is 0 then return board
            if groove > 0 and iteration is iterations//2 do groove is 0 then mode=1
        return board
    from list board, int iterations=150, str wall=wall, str notWall=notWall, int mode=1,bool show=True, int groove=0
    
    board is pipe board,mode=1,groove=1 to genMaze  
    

    (Potential) Result:

       ██████████████          ███████████████████████████ █████   ████████            
                      ██                                       ██       ███    ██ ██ 
    ██  ██ ████████             ████████████ ██████ ███                  
                ██████                               ████   ████       
          ████                 ████████ ██████████               ███████ 
    ██ ██      ██ ██ ████████                          ████   ███         
                         ██ ████████    ████              ████████ 
     ██     ████    █████                         █████ ████         
                            ███████████                       ██████ 
          █████    █████                         ███████████         
              ██       ████  ██████ ████                      █████  
     ██   ██████                              ██████          ███
                 ██ ██     ██                                   
     █████  ████                     ██    ████               ██
              ████████    ██                ███  ███            
    ██████                            █████                     ██
             ████████                       ███████████   █████     
    ████   ██                 ████      ████              ███          
                  ███                      █████████        ███ 
    ██     ██             █████     ████               ████        
        ████       ██   ██             ████   █████         █████ 
    ██                             ███                ██         
      ████        ███               ███████ █████         █████ 
                    ██          █████                           ██ 
                           ████      ██████████   ██    ██  ███    
          ██             ██                                              
     ███         ███    ██       ███████  ██   ██     ███████████ ████    █████       
    
    Scroll to top