├── maze.py └── maze2.py /maze.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from random import randint 3 | 4 | cell_size = 9 #pixels 5 | ms = 100 # rows and columns 6 | visited_cells = [] 7 | walls = [] 8 | 9 | 10 | map = [['w' for _ in range(ms)]for _ in range(ms)] 11 | 12 | 13 | def create(): 14 | for row in range(ms): 15 | for col in range(ms): 16 | if map[row][col] == 'P': 17 | color = 'White' 18 | elif map[row][col] == 'w': 19 | color = 'black' 20 | draw(row, col, color) 21 | 22 | def draw(row, col, color): 23 | x1 = col*cell_size 24 | y1 = row*cell_size 25 | x2 = x1+cell_size 26 | y2 = y1+cell_size 27 | ffs.create_rectangle(x1, y1, x2, y2, fill=color) 28 | 29 | 30 | 31 | def check_neighbours(ccr, ccc): 32 | neighbours = [[ccr, ccc-1, ccr-1, ccc-2, ccr, ccc-2, ccr+1, ccc-2, ccr-1, ccc-1, ccr+1, ccc-1], #left 33 | [ccr, ccc+1, ccr-1, ccc+2, ccr, ccc+2, ccr+1, ccc+2, ccr-1, ccc+1, ccr+1, ccc+1], #right 34 | [ccr-1, ccc, ccr-2, ccc-1, ccr-2, ccc, ccr-2, ccc+1, ccr-1, ccc-1, ccr-1, ccc+1], #top 35 | [ccr+1, ccc, ccr+2, ccc-1, ccr+2, ccc, ccr+2, ccc+1, ccr+1, ccc-1, ccr+1, ccc+1]] #bottom 36 | visitable_neighbours = [] 37 | for i in neighbours: #find neighbours to visit 38 | if i[0] > 0 and i[0] < (ms-1) and i[1] > 0 and i[1] < (ms-1): 39 | if map[i[2]][i[3]] == 'P' or map[i[4]][i[5]] == 'P' or map[i[6]][i[7]] == 'P' or map[i[8]][i[9]] == 'P' or map[i[10]][i[11]] == 'P': 40 | walls.append(i[0:2]) 41 | else: 42 | visitable_neighbours.append(i[0:2]) 43 | return visitable_neighbours 44 | 45 | #StartingPoint 46 | 47 | scr = randint(1, ms) 48 | scc = randint(1, ms) 49 | start_color = 'Green' 50 | ccr, ccc = scr, scc 51 | 52 | map[ccr][ccc] = 'P' 53 | finished = False 54 | while not finished: 55 | visitable_neighbours = check_neighbours(ccr, ccc) 56 | if len(visitable_neighbours) != 0: 57 | d = randint(1, len(visitable_neighbours))-1 58 | ncr, ncc = visitable_neighbours[d] 59 | map[ncr][ncc] = 'P' 60 | visited_cells.append([ncr, ncc]) 61 | ccr, ccc = ncr, ncc 62 | if len(visitable_neighbours) == 0: 63 | try: 64 | ccr, ccc = visited_cells.pop() 65 | except: 66 | finished = True 67 | 68 | 69 | window = Tk() 70 | window.title('Maze') 71 | canvas_side = ms*cell_size 72 | ffs = Canvas(window, width = canvas_side, height = canvas_side, bg = 'grey') 73 | ffs.pack() 74 | 75 | 76 | create() 77 | draw(scr, scc, start_color) 78 | window.mainloop() -------------------------------------------------------------------------------- /maze2.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from random import randint 3 | 4 | cell_size = 9 #pixels 5 | ms = 100 # rows and columns 6 | visited_cells = [] 7 | walls = [] 8 | revisited_cells = [] 9 | 10 | map = [['w' for _ in range(ms)]for _ in range(ms)] 11 | 12 | 13 | def create(): 14 | for row in range(ms): 15 | for col in range(ms): 16 | if map[row][col] == 'P': 17 | color = 'White' 18 | elif map[row][col] == 'w': 19 | color = 'black' 20 | draw(row, col, color) 21 | 22 | def draw(row, col, color): 23 | x1 = col*cell_size 24 | y1 = row*cell_size 25 | x2 = x1+cell_size 26 | y2 = y1+cell_size 27 | ffs.create_rectangle(x1, y1, x2, y2, fill=color) 28 | 29 | 30 | 31 | def check_neighbours(ccr, ccc): 32 | neighbours = [[ccr, ccc-1, ccr-1, ccc-2, ccr, ccc-2, ccr+1, ccc-2, ccr-1, ccc-1, ccr+1, ccc-1], #left 33 | [ccr, ccc+1, ccr-1, ccc+2, ccr, ccc+2, ccr+1, ccc+2, ccr-1, ccc+1, ccr+1, ccc+1], #right 34 | [ccr-1, ccc, ccr-2, ccc-1, ccr-2, ccc, ccr-2, ccc+1, ccr-1, ccc-1, ccr-1, ccc+1], #top 35 | [ccr+1, ccc, ccr+2, ccc-1, ccr+2, ccc, ccr+2, ccc+1, ccr+1, ccc-1, ccr+1, ccc+1]] #bottom 36 | visitable_neighbours = [] 37 | for i in neighbours: #find neighbours to visit 38 | if i[0] > 0 and i[0] < (ms-1) and i[1] > 0 and i[1] < (ms-1): 39 | if map[i[2]][i[3]] == 'P' or map[i[4]][i[5]] == 'P' or map[i[6]][i[7]] == 'P' or map[i[8]][i[9]] == 'P' or map[i[10]][i[11]] == 'P': 40 | walls.append(i[0:2]) 41 | else: 42 | visitable_neighbours.append(i[0:2]) 43 | return visitable_neighbours 44 | 45 | #StartingPoint 46 | 47 | scr = randint(1, ms) 48 | scc = randint(1, ms) 49 | start_color = 'Green' 50 | ccr, ccc = scr, scc 51 | 52 | map[ccr][ccc] = 'P' 53 | finished = False 54 | while not finished: 55 | visitable_neighbours = check_neighbours(ccr, ccc) 56 | if len(visitable_neighbours) != 0: 57 | d = randint(1, len(visitable_neighbours))-1 58 | ncr, ncc = visitable_neighbours[d] 59 | map[ncr][ncc] = 'P' 60 | visited_cells.append([ncr, ncc]) 61 | ccr, ccc = ncr, ncc 62 | if len(visitable_neighbours) == 0: 63 | try: 64 | ccr, ccc = visited_cells.pop() 65 | revisited_cells.append([ccr, ccc]) 66 | 67 | except: 68 | finished = True 69 | 70 | 71 | window = Tk() 72 | window.title('Maze') 73 | canvas_side = ms*cell_size 74 | ffs = Canvas(window, width = canvas_side, height = canvas_side, bg = 'grey') 75 | ffs.pack() 76 | 77 | 78 | create() 79 | draw(scr, scc, start_color) 80 | e = randint(1, len(revisited_cells))-1 81 | ecr = revisited_cells[e][0] 82 | ecc = revisited_cells[e][1] 83 | end_color = 'red' 84 | draw(ecr, ecc, end_color) 85 | print(revisited_cells) 86 | window.mainloop() --------------------------------------------------------------------------------