Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Maze Solution Checker
import sys, os
from subprocess import Popen, PIPE
if sys.argv[1]=="-d":
debug=True
prog=sys.argv[2]
else:
debug=False
prog=sys.argv[1]
def solved():
process.stdin.write('solved\n')
process.stdin.flush()
process.stdin.close()
process.wait()
print "Maze "+fname+" solved."
for fname in os.listdir("mazes"):
with open(os.path.join('mazes', fname)) as f:
maze=list(f)
has_exit=False
for i, l in enumerate(maze):
if "S" in l:
y=i
x=l.index("S")
if "X" in l: has_exit=True
moves = set()
dirs={"right":(1, 0), "left":(-1, 0), "up": (0, -1), "down": (0, 1)}
process=Popen([prog], stdin=PIPE, stdout=PIPE)
while True:
move = process.stdout.readline()[:-1]
if debug: print (x,y, move)
if (x, y, move) in moves:
print >> sys.stderr, "Repeat move"
sys.exit(1)
if move in dirs:
newx=x+dirs[move][0]
newy=y+dirs[move][1]
elif move=="no exit":
if has_exit==False:
solved()
break
else:
print >> sys.stderr, "There is an exit"
sys.exit(1)
else:
print >> sys.stderr, "Invalid move"
sys.exit(1)
moves.add((x, y, move))
if maze[newy][newx]=="#":
process.stdin.write('wall\n')
process.stdin.flush()
elif maze[newy][newx]=="X":
solved()
break
else:
x=newx
y=newy
process.stdin.write('ok\n')
process.stdin.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment