import libtcodpy as libtcod
from random import randint

nSquares = 30
nTiles = nSquares * 2 + 1

SCREEN_WIDTH = nTiles
SCREEN_HEIGHT = nTiles

libtcod.console_set_custom_font("cp437_12x12.png", libtcod.FONT_LAYOUT_ASCII_INROW)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'pyMazeBacktrack', False, libtcod.RENDERER_OPENGL)

def CheckDir(x,y,size,direction,table):

    if direction == 1:
        if y - 2 <= 0:
            return 0
        if table[x][y-2] == white:
            return 0
    elif direction == 2:
        if x + 2 >= size:
            return 0
        if table[x+2][y] == white:
            return 0
    elif direction == 3:
        if y + 2 >= size:
            return 0
        if table[x][y+2] == white:
            return 0
    elif direction == 4:
        if x - 2 <= 0:
            return 0
        if table[x-2][y] == white:
            return 0
    
    return 1

def Possible(x,y,table,size):

    if x+2 < size:
        if table[x+2][y] == black:
            return 1
    if x-2 > 0:
        if table[x-2][y] == black:
            return 1
    if y+2 < size:
        if table[x][y+2] == black:
            return 1
    if y-2 > 0:
        if table[x][y-2] == black:
            return 1        

    return 0

black = libtcod.black
white = libtcod.white

Table = [[0 for i in range(nTiles)]for i in range(nTiles)]

for x in range(nTiles):
    for y in range(nTiles):
        Table[x][y] = black
        libtcod.console_put_char_ex(None,x,y,219,Table[x][y],libtcod.white)

libtcod.console_flush()

Memory = []

CurrX = 1
CurrY = 1
Table[CurrX][CurrY] = white

end = 0

while end == 0:

    while Possible(CurrX,CurrY,Table,nTiles):

        Dir = randint(1,4)
        while CheckDir(CurrX,CurrY,nTiles,Dir,Table) == 0:
            Dir = randint(1,4)

        if Dir == 1:
            Table[CurrX][CurrY - 1] = white
            CurrY -= 2
            Table[CurrX][CurrY] = white
        elif Dir == 2:
            Table[CurrX + 1][CurrY] = white
            CurrX += 2
            Table[CurrX][CurrY] = white
        elif Dir == 3:
            Table[CurrX][CurrY + 1] = white
            CurrY += 2
            Table[CurrX][CurrY] = white
        elif Dir == 4:
            Table[CurrX - 1][CurrY] = white
            CurrX -= 2
            Table[CurrX][CurrY] = white

        Memory.append(Dir)

        #print
        for x in range(nTiles):
            for y in range(nTiles):
                libtcod.console_put_char_ex(None,x,y,219,Table[x][y],libtcod.white)
        libtcod.console_flush()

    while Possible(CurrX,CurrY,Table,nTiles) == 0:

        MemorySize = len(Memory)

        Dir = Memory[MemorySize-1]

        if Dir == 1:
            CurrY += 2
        elif Dir == 2:
            CurrX -= 2
        elif Dir == 3:
            CurrY -= 2
        elif Dir == 4:
            CurrX += 2

        del Memory[MemorySize-1]

        if CurrX == 1 and CurrY == 1:
            end = 1
            break

#print
for x in range(nTiles):
    for y in range(nTiles):
        libtcod.console_put_char_ex(None,x,y,219,Table[x][y],libtcod.white)
libtcod.console_flush()

libtcod.console_wait_for_keypress(True)





























    

[((10, 0, 10, 83), 'libtcodpy.console_set_custom_font', 'libtcod.console_set_custom_font', ({(10, 32, 10, 49): '"""cp437_12x12.png"""', (10, 51, 10, 82): 'libtcod.FONT_LAYOUT_ASCII_INROW'}, {}), "('cp437_12x12.png', libtcod.\n    FONT_LAYOUT_ASCII_INROW)", True, 'import libtcodpy as libtcod\n'), ((11, 0, 11, 105), 'libtcodpy.console_init_root', 'libtcod.console_init_root', ({(11, 26, 11, 38): 'SCREEN_WIDTH', (11, 40, 11, 53): 'SCREEN_HEIGHT', (11, 55, 11, 72): '"""pyMazeBacktrack"""', (11, 74, 11, 79): '(False)', (11, 81, 11, 104): 'libtcod.RENDERER_OPENGL'}, {}), "(SCREEN_WIDTH, SCREEN_HEIGHT, 'pyMazeBacktrack', \n    False, libtcod.RENDERER_OPENGL)", True, 'import libtcodpy as libtcod\n'), ((65, 0, 65, 23), 'libtcodpy.console_flush', 'libtcod.console_flush', ({}, {}), '()', True, 'import libtcodpy as libtcod\n'), ((133, 0, 133, 23), 'libtcodpy.console_flush', 'libtcod.console_flush', ({}, {}), '()', True, 'import libtcodpy as libtcod\n'), ((135, 0, 135, 39), 'libtcodpy.console_wait_for_keypress', 'libtcod.console_wait_for_keypress', ({(135, 34, 135, 38): '(True)'}, {}), '(True)', True, 'import libtcodpy as libtcod\n'), ((63, 8, 63, 75), 'libtcodpy.console_put_char_ex', 'libtcod.console_put_char_ex', ({(63, 36, 63, 40): 'None', (63, 41, 63, 42): 'x', (63, 43, 63, 44): 'y', (63, 45, 63, 48): '(219)', (63, 49, 63, 60): 'Table[x][y]', (63, 61, 63, 74): 'libtcod.white'}, {}), '(None, x, y, 219, Table[x][y], libtcod.white)', True, 'import libtcodpy as libtcod\n'), ((79, 14, 79, 26), 'random.randint', 'randint', ({(79, 22, 79, 23): '1', (79, 24, 79, 25): '4'}, {}), '(1, 4)', False, 'from random import randint\n'), ((106, 8, 106, 31), 'libtcodpy.console_flush', 'libtcod.console_flush', ({}, {}), '()', True, 'import libtcodpy as libtcod\n'), ((132, 8, 132, 75), 'libtcodpy.console_put_char_ex', 'libtcod.console_put_char_ex', ({(132, 36, 132, 40): 'None', (132, 41, 132, 42): 'x', (132, 43, 132, 44): 'y', (132, 45, 132, 48): '(219)', (132, 49, 132, 60): 'Table[x][y]', (132, 61, 132, 74): 'libtcod.white'}, {}), '(None, x, y, 219, Table[x][y], libtcod.white)', True, 'import libtcodpy as libtcod\n'), ((81, 18, 81, 30), 'random.randint', 'randint', ({(81, 26, 81, 27): '1', (81, 28, 81, 29): '4'}, {}), '(1, 4)', False, 'from random import randint\n'), ((105, 16, 105, 83), 'libtcodpy.console_put_char_ex', 'libtcod.console_put_char_ex', ({(105, 44, 105, 48): 'None', (105, 49, 105, 50): 'x', (105, 51, 105, 52): 'y', (105, 53, 105, 56): '(219)', (105, 57, 105, 68): 'Table[x][y]', (105, 69, 105, 82): 'libtcod.white'}, {}), '(None, x, y, 219, Table[x][y], libtcod.white)', True, 'import libtcodpy as libtcod\n')]