Skip to content

Instantly share code, notes, and snippets.

@magnetik
Created April 24, 2014 07:26
Show Gist options
  • Save magnetik/11244886 to your computer and use it in GitHub Desktop.
Save magnetik/11244886 to your computer and use it in GitHub Desktop.
Python Maze Cheese Solver
class MazeSolver:
def __init__(self):
self.points = [Point()]
def findCheese(self, maze):
maze.initialize()
while(True):
currentPoint = self.points[-1]
if maze.testAndMove(maze, currentPoint) == False:
else:
def testAndMove(self, maze, point):
nextDirection = point.nextDirection()
if nextDirection != False:
if maze.move(nextDirection) == True:
#compute new point
if nextDirection == Direction.RIGHT:
newPoint = Point(point.x++, point.y)
else if nextDirection == Direction.LEFT:
newPoint = Point(point.x--, point.y)
else if nextDirection == Direction.UP:
newPoint = Point(point.x, point.y++)
else: # DOWN
newPoint = Point(point.x, point.y--)
self.points.append(newPoint)
return True
else:
return False
else:
raise Exception()
def getLastPointNonTested(self):
for point in self.points.reverse():
if !point.isFullyTested():
return point
return False
class Direction:
LEFT = 1
DOWN = 2
RIGHT = 3
UP = 4
class Point:
def __init__(self, x = 0, y = 0, directionTested = 0):
self.x = x
self.y = y
self.directionTested = directionTested
def nextDirection(self):
if self.directionTested == Direction.UP:
return False
return self.directionTested++
def isFullyTested(self):
if self.directionTested == Direction.UP:
return true
else:
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment