Created
December 2, 2013 14:51
-
-
Save dogles/7750584 to your computer and use it in GitHub Desktop.
Visualizer for map skeleton
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
#!/usr/bin/python | |
import re | |
import sys | |
road_vertex = re.compile("road_vertex\(([\w\d]+),([\w\d]+),([\w\d]+),([\w\d]+),([\w\d]+)\)") | |
def display_maze(facts): | |
"""turn a list of ansprolog facts into a nice ascii-art maze diagram""" | |
max_x = 1 | |
max_y = 1 | |
solid = {} | |
vertices = {} | |
connects = {} | |
for fact in facts: | |
m = road_vertex.match(fact) | |
if m: | |
n1, n2, i, x, y = m.groups() | |
n1, n2, i, x, y = int(n1), int(n2), int(i), int(x), int(y) | |
pos = (x,y) | |
conn = (n1,n2,i) | |
max_x, max_y = max(x, max_x), max(y, max_y) | |
vertices[conn] = pos | |
connects[(n1,n2)] = True | |
for (n1,n2) in connects: | |
i = 0 | |
k0 = (n1,n2,i) | |
k1 = (n1,n2,i+1) | |
while k1 in vertices: | |
x0,y0 = vertices[k0] | |
x1,y1 = vertices[k1] | |
if x0 == x1: | |
minY = min(y0,y1) | |
maxY = max(y0,y1) | |
for y in range(minY,maxY+1): | |
pos = (x0,y) | |
solid[pos] = True | |
else: | |
minX = min(x0,x1) | |
maxX = max(x0,x1) | |
for x in range(minX,maxX+1): | |
pos = (x,y0) | |
solid[pos] = True | |
i = i+1 | |
k0 = (n1,n2,i) | |
k1 = (n1,n2,i+1) | |
def code(x,y): | |
"""decide how a maze cell should be tpyeset""" | |
pos = (x,y) | |
if pos in solid: | |
return " @ " | |
else: | |
return " . " | |
for y in range(0,max_y+1): | |
print "".join([code(x,y) for x in range(0,max_x+1)]) | |
def main(): | |
"""look for lines that contain logical facts and try to turn each of those | |
into a maze""" | |
for line in sys.stdin.xreadlines(): | |
line = line.strip() | |
if line: | |
if line[0].islower(): | |
facts = line.split(' ') | |
display_maze(facts) | |
else: | |
print "% " + line | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment