Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created October 15, 2019 15:25
Show Gist options
  • Save ahmed4end/36524d3aa0d8627fa2afb66756a03d7f to your computer and use it in GitHub Desktop.
Save ahmed4end/36524d3aa0d8627fa2afb66756a03d7f to your computer and use it in GitHub Desktop.
state = [[0 for i in range(50)] for j in range(50)]
import random
def access(state, x, y):
try:return state[x][y]
except:pass
neighbours = lambda x, ngrid=len(state):list(filter(lambda x:x is not False,map(lambda x:x if 0<=x[0]<=ngrid-1 and x[1]>=0 and access(state, x[0], x[1]) == 0 else False, random.sample([(x[0],x[1]-1),(x[0]-1,x[1]),(x[0],x[1]+1),(x[0]+1,x[1])], 4))))
head = lambda node, dir: [(node[0]+i[0]+j[0],node[1]+i[1]+j[1]) for i in (dir, (0,0)) for j in {(0,-1):((-1,0), (0,0), (1,0)), (0,1):((-1,0), (0,0), (1,0)), (-1,0):((0,-1), (0,0), (0,1)), (1,0):((0,-1), (0,0), (0,1))}[dir] ]
#directions = {(-1,0):"N", (1, 0):"S", (0,1):"E", (0,-1):"W"}
direction = lambda node, node2: (node2[0]-node[0], node2[1]-node[1])
x = (1,1)
state[1][1] = 5
def maze(state, x, trac=[]):
nei = neighbours(x)
if nei == 0:return False
for cell in nei:
dir = direction(x, cell)
if not all(map(lambda x: True if access(state, x[0], x[1]) == 0 or access(state, x[0], x[1]) == None else False, set(head(cell, dir))-{(x)})):
continue
state[cell[0]][cell[1]] = 5
trac.append(cell)
maze(state, cell, trac=trac)
return trac
steps = maze(state, x)
for i in state:print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment