Created
October 15, 2019 15:25
-
-
Save ahmed4end/36524d3aa0d8627fa2afb66756a03d7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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