61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
|
from .obj.TemporaryState import TemporaryState
|
||
|
|
||
|
|
||
|
class StateController:
|
||
|
def __init__(self, istate):
|
||
|
self.path = []
|
||
|
self.explored = []
|
||
|
self.fringe = []
|
||
|
self.istate = istate
|
||
|
|
||
|
def reset(self):
|
||
|
self.path.clear()
|
||
|
self.explored.clear()
|
||
|
self.fringe.clear()
|
||
|
|
||
|
def build_path(self, goal_state):
|
||
|
self.path.append(goal_state)
|
||
|
while self.path[-1].agent_role != "blank":
|
||
|
self.path.append(self.path[-1].parent)
|
||
|
|
||
|
return self.path
|
||
|
|
||
|
def graphsearch(self, engine): # BFS
|
||
|
print("Search path")
|
||
|
|
||
|
self.reset()
|
||
|
|
||
|
self.fringe.append(TemporaryState(self.istate))
|
||
|
|
||
|
while self.fringe:
|
||
|
self.explored.append(self.fringe.pop(0))
|
||
|
|
||
|
if self.explored[-1].goal_test(engine):
|
||
|
print("Goal!")
|
||
|
goal_state = self.explored[-1]
|
||
|
self.reset()
|
||
|
return self.build_path(goal_state)
|
||
|
|
||
|
self.succ(self.explored[-1].front(), engine)
|
||
|
self.succ(self.explored[-1].left(), engine)
|
||
|
self.succ(self.explored[-1].right(), engine)
|
||
|
|
||
|
self.path = self.fringe
|
||
|
engine.redraw()
|
||
|
|
||
|
self.reset()
|
||
|
|
||
|
print("Not found")
|
||
|
|
||
|
return False
|
||
|
|
||
|
def succ(self, state, engine):
|
||
|
if state.collide_test():
|
||
|
return
|
||
|
elif any(e.compare(state) for e in self.explored):
|
||
|
return
|
||
|
elif any([o.collide_test(state) for o in engine.objects]):
|
||
|
return
|
||
|
|
||
|
self.fringe.append(state)
|