public
Created

  • Download Gist
maze
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
#!/usr/bin/env python
# amazer.py
#So far I've put in about 8 hours
# Copyright 2009 Chase Johnson
# Licensable under WTFPL v2
#http://www.reddit.com/r/programming/comments/a0x8g/hey_reddit_check_out_this_python_maze_solver_i/
"""
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
 
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 
0. You just DO WHAT THE FUCK YOU WANT TO.
"""
import Image
from math import floor
from sys import setrecursionlimit
setrecursionlimit(10000000)
outCount = 0
outLimit = 100000
verbose = False
frameperiod = 200
cleanupFramePeriod = 1000
cleaningUp = False
Up, Down, Left, Right = range(4)
def process(name):
im = Image.open(name)
data = im.getdata()
i = 0
j = 0
col = 0
row = 0
whitePixelsRight = []
whitePixelsLeft = []
whitePixelsUp = []
whitePixelsDown = []
pix = im.load()
for pixel in data:
i = i + 1
col = col+1
if(col > im.size[0]):
col = 1
row = row + 1
j = im.size[0]*row+col
if(isGreen(pixel)):
if(isWhite(pix[col-2,row])):
whitePixelsLeft.append((col-2,row))
if(isWhite(pix[col-1, row-1])):
whitePixelsUp.append((col-1,row-1))
if(isWhite(pix[col, row])):
whitePixelsRight.append((col,row))
if(isWhite(pix[col-1, row+1])):
whitePixelsDown.append((col-1,row+1))
 
leftFronts = collect_fronts(whitePixelsLeft)
rightFronts = collect_fronts(whitePixelsRight)
upFronts = collect_fronts(whitePixelsUp)
downFronts = collect_fronts(whitePixelsDown)
 
if verbose: print "Number of wavefronts found: ", str(len(rightFronts) + len(leftFronts) + len(upFronts) + len(downFronts))
move_wavefront(im, pix, rightFronts[0], Right)
 
def move_wavefront(im, pix, startFront, direction):
global outCount
global outLimit
global cleaningUp
global verbose
global frameperiod
outCount = outCount + 1
if verbose: print "------------------- OUTPUT FRAME #" + str(outCount) + "-------------------"
if outCount > outLimit:
return False
xMotion = 0
yMotion = 0
if direction == Right: xMotion = 1
elif direction == Down: yMotion = 1
elif direction == Up: yMotion = -1
elif direction == Left: xMotion = -1
whitePixelsRight = []
whitePixelsLeft = []
whitePixelsUp = []
whitePixelsDown = []
front = list(startFront)
if verbose: print "Starting traversal"
while not front_contains_wall(im, pix, front):
if verbose: print "no wall still at " + str(front)
tempFront = []
for pixel in front:
pix[pixel[0],pixel[1]] = (0,255,0)
tempFront.append((pixel[0]+xMotion, pixel[1]+yMotion))
front = tempFront
foundRed = False
#start over and check for fronts
front = list(startFront)
if verbose: print "Detecting wavefronts"
while not front_contains_wall(im, pix, front):
if verbose: print "no wall still at " + str(front)
tempFront = []
for pixel in front:
tempFront.append((pixel[0]+xMotion, pixel[1]+yMotion))
if(isGreen(pix[pixel[0],pixel[1]])):
col = pixel[0]
row = pixel[1]
if(isWhite(pix[col-1,row])):
whitePixelsLeft.append((col-1,row))
if verbose: print "Found white pixel Left at " + str((col-1,row))
if(isWhite(pix[col, row-1])):
whitePixelsUp.append((col,row-1))
if verbose: print "Found white pixel Up at " + str((col,row-1))
if(isWhite(pix[col+1, row])):
whitePixelsRight.append((col+1,row))
if verbose: print "Found white pixel Up at " + str((col+1,row))
if(isWhite(pix[col, row+1])):
whitePixelsDown.append((col,row+1))
if verbose: print "Found white pixel Up at " + str((col,row+1))
if(isRed(pix[col-1,row])):
foundRed = True
if verbose: print "Found red pixel Left at " + str((col-1,row))
if(isRed(pix[col, row-1])):
foundRed = True
if verbose: print "Found red pixel Up at " + str((col,row-1))
if(isRed(pix[col+1, row])):
foundRed = True
if verbose: print "Found red pixel Up at " + str((col+1,row))
if(isRed(pix[col, row+1])):
foundRed = True
if verbose: print "Found red pixel Up at " + str((col,row+1))
front = tempFront
leftFronts = collect_fronts(whitePixelsLeft)
rightFronts = collect_fronts(whitePixelsRight)
upFronts = collect_fronts(whitePixelsUp)
downFronts = collect_fronts(whitePixelsDown)
 
numWaves = len(rightFronts) + len(leftFronts) + len(upFronts) + len(downFronts);
if verbose: print "Number of wavefronts found: ", str(numWaves)
if verbose: print "Left side fronts: "+ str(leftFronts)
if verbose: print "Right side fronts: "+ str(rightFronts)
if verbose: print "Up side fronts: "+ str(upFronts)
if verbose: print "Down side fronts: "+ str(downFronts)
trueReturned = False
if(outCount % frameperiod == 0):
im.save("output" + str(outCount).rjust(5, "0")+".png")
if(cleaningUp and (outCount % cleanupFramePeriod ==0)):
im.save("output" + str(outCount).rjust(5, "0")+".png")
if(foundRed):
if verbose: print "========================== MAZE SOLVED =========================="
cleaningUp = True
return True
for front in leftFronts:
if(move_wavefront(im, pix, front, Left)):
trueReturned = True
for front in rightFronts:
if(move_wavefront(im, pix, front, Right)):
trueReturned = True
for front in upFronts:
if(move_wavefront(im, pix, front, Up)):
trueReturned = True
for front in downFronts:
if(move_wavefront(im, pix, front, Down)):
trueReturned = True
if not trueReturned:
if verbose: print "Re-coloring for dead path"
front = list(startFront)
while not front_contains_wall(im, pix, front):
if verbose: print "no wall still at " + str(front)
tempFront = []
for pixel in front:
pix[pixel[0],pixel[1]] = (128,128,128)
tempFront.append((pixel[0]+xMotion, pixel[1]+yMotion))
front = tempFront
if verbose: print "Returning " + str(trueReturned)
return trueReturned
 
def front_contains_wall(im, pix, front):
blackDetected = False
for pixel in front:
if isBlack(pix[pixel[0],pixel[1]]):
blackDetected = True
break
return blackDetected
 
def collect_fronts(array):
returnArray = []
for pixel in array:
added = False
# check each array in leftFronts for one that contains an adjacent pixel to pixel
for front in returnArray:
for testPixel in front:
if is_adjacent(testPixel, pixel):
front.append(pixel)
added = True
break
if not added:
tempArray = [pixel]
returnArray.append(tempArray)
return returnArray
def is_adjacent(tuple1, tuple2):
x1, y1 = tuple1
x2, y2 = tuple2
if x1 == x2 and abs(y1-y2)==1: return True
elif y1 == y2 and abs(x1-x2)==1: return True
else: return False
def compare_coordinates(tuple1, tuple2):
x1, y1 = tuple1
x2, y2 = tuple2
if x1 > x2:
return 1
elif x1 < x2:
return -1
elif x1 == x2:
if y1 > y2:
return 1
elif y1 < y2:
return -1
else: return 0
def isWhite(tuple):
R, G, B = tuple
return R+G+B > 600
 
def isBlack(tuple):
R, G, B = tuple
return R+G+B < 40
 
def isGreen(tuple):
R, G, B = tuple
return G > 0.8*(R+B)
 
def isRed(tuple):
R, G, B = tuple
return R > 0.8*(G+B)
 
def isBlue(tuple):
R, G, B = tuple
return B > 0.8*(R+G)
 
process("maze4.png")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.