Created
October 27, 2015 08:04
-
-
Save timkg/9545e5d8c1131de0782d to your computer and use it in GitHub Desktop.
This file contains 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
from game import Directions | |
n = Directions.NORTH | |
s = Directions.SOUTH | |
w = Directions.WEST | |
e = Directions.EAST | |
fringe = util.Stack() | |
closed = set() | |
def makeNode(state, prevActions=[], action=False): | |
actions = [] | |
if action in ( n, s, w, e ): | |
actions = prevActions + [action] | |
return ( state, actions ) | |
def search(fringe): | |
if fringe.isEmpty(): | |
raise Exception, 'depthFirstSearch could not find solution' | |
node = fringe.pop() | |
if problem.isGoalState(node[0]): | |
return node[1] | |
if node[0] not in closed: | |
closed.add(node[0]) | |
for triple in problem.getSuccessors(node[0]): | |
fringe.push(makeNode(triple[0], node[1], triple[1])) | |
return search(fringe) | |
fringe.push(makeNode(problem.getStartState())) | |
return search(fringe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment