BFS I DSF z Visited

This commit is contained in:
Arkadiusz 2019-05-19 21:46:37 +01:00
parent f7e3227185
commit a51cc452fc
6 changed files with 183 additions and 164 deletions

View File

@ -4,6 +4,7 @@
<list default="true" id="152ea650-8e02-4ed6-aa8c-04d44b275be4" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/game.py" beforeDir="false" afterPath="$PROJECT_DIR$/game.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/table.py" beforeDir="false" afterPath="$PROJECT_DIR$/table.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/waiter.py" beforeDir="false" afterPath="$PROJECT_DIR$/waiter.py" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -23,11 +24,11 @@
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/game.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="116">
<caret line="57" column="32" selection-start-line="57" selection-start-column="32" selection-end-line="57" selection-end-column="32" />
<state relative-caret-position="402">
<caret line="61" column="19" lean-forward="true" selection-start-line="61" selection-start-column="19" selection-end-line="61" selection-end-column="19" />
</state>
</provider>
</entry>
@ -44,8 +45,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/table.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="308">
<caret line="14" column="15" selection-start-line="14" selection-start-column="15" selection-end-line="14" selection-end-column="15" />
<state relative-caret-position="176">
<caret line="8" column="26" selection-start-line="8" selection-start-column="26" selection-end-line="8" selection-end-column="26" />
</state>
</provider>
</entry>
@ -53,8 +54,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/pool.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="176">
<caret line="9" column="26" selection-start-line="9" selection-start-column="26" selection-end-line="9" selection-end-column="26" />
<state relative-caret-position="88">
<caret line="4" column="6" selection-start-line="4" selection-start-column="6" selection-end-line="4" selection-end-column="6" />
<folding>
<element signature="e#0#13#0" expanded="true" />
</folding>
@ -71,16 +72,15 @@
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/waiter.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="387">
<caret line="361" column="20" selection-start-line="361" selection-start-column="20" selection-end-line="361" selection-end-column="20" />
<state relative-caret-position="248">
<caret line="254" column="67" lean-forward="true" selection-start-line="254" selection-start-column="67" selection-end-line="254" selection-end-column="67" />
<folding>
<element signature="e#0#14#0" expanded="true" />
<element signature="e#4604#5028#0" />
<element signature="e#5089#9586#0" />
<element signature="e#9658#12817#0" />
<element signature="e#5084#9581#0" />
<element signature="e#12891#16593#0" />
</folding>
</state>
</provider>
@ -99,6 +99,7 @@
<findStrings>
<find>self.last</find>
<find>tmp</find>
<find>visited</find>
</findStrings>
</component>
<component name="Git.Settings">
@ -120,11 +121,11 @@
<option value="$PROJECT_DIR$/gridElement.py" />
<option value="$PROJECT_DIR$/run.py" />
<option value="$PROJECT_DIR$/order.py" />
<option value="$PROJECT_DIR$/table.py" />
<option value="$PROJECT_DIR$/orderTable.py" />
<option value="$PROJECT_DIR$/pool.py" />
<option value="$PROJECT_DIR$/game.py" />
<option value="$PROJECT_DIR$/table.py" />
<option value="$PROJECT_DIR$/waiter.py" />
<option value="$PROJECT_DIR$/game.py" />
</list>
</option>
</component>
@ -141,7 +142,6 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
@ -158,6 +158,7 @@
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
@ -263,20 +264,19 @@
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.21443625" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32936078" />
<window_info active="true" anchor="bottom" id="Run" order="2" sideWeight="0.49894625" visible="true" weight="0.2979415" />
<window_info anchor="bottom" id="Run" order="2" sideWeight="0.49894625" weight="0.2979415" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.2713348" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" weight="0.32936078" />
<window_info anchor="bottom" id="Version Control" order="7" sideWeight="0.49947312" weight="0.32936078" />
<window_info anchor="bottom" id="Terminal" order="8" weight="0.32936078" />
<window_info active="true" anchor="bottom" id="Terminal" order="8" visible="true" weight="0.32936078" />
<window_info anchor="bottom" id="Event Log" order="9" sideWeight="0.50105375" side_tool="true" weight="0.32936078" />
<window_info anchor="bottom" id="Python Console" order="10" weight="0.32936078" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
@ -424,6 +424,30 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/orderTable.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
<caret line="17" column="58" selection-start-line="17" selection-start-column="58" selection-end-line="17" selection-end-column="58" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/table.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="176">
<caret line="8" column="26" selection-start-line="8" selection-start-column="26" selection-end-line="8" selection-end-column="26" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/pool.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="88">
<caret line="4" column="6" selection-start-line="4" selection-start-column="6" selection-end-line="4" selection-end-column="6" />
<folding>
<element signature="e#0#13#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/gridElement.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="154">
@ -431,30 +455,18 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/table.py">
<entry file="file://$PROJECT_DIR$/waiter.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="308">
<caret line="14" column="15" selection-start-line="14" selection-start-column="15" selection-end-line="14" selection-end-column="15" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/pool.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="176">
<caret line="9" column="26" selection-start-line="9" selection-start-column="26" selection-end-line="9" selection-end-column="26" />
<state relative-caret-position="248">
<caret line="254" column="67" lean-forward="true" selection-start-line="254" selection-start-column="67" selection-end-line="254" selection-end-column="67" />
<folding>
<element signature="e#0#13#0" expanded="true" />
<element signature="e#0#14#0" expanded="true" />
<element signature="e#5084#9581#0" />
<element signature="e#12891#16593#0" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/orderTable.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="374">
<caret line="17" column="58" selection-start-line="17" selection-start-column="58" selection-end-line="17" selection-end-column="58" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/order.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="132">
@ -464,21 +476,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/game.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="116">
<caret line="57" column="32" selection-start-line="57" selection-start-column="32" selection-end-line="57" selection-end-column="32" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/waiter.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="387">
<caret line="361" column="20" selection-start-line="361" selection-start-column="20" selection-end-line="361" selection-end-column="20" />
<folding>
<element signature="e#0#14#0" expanded="true" />
<element signature="e#4604#5028#0" />
<element signature="e#5089#9586#0" />
<element signature="e#9658#12817#0" />
</folding>
<state relative-caret-position="402">
<caret line="61" column="19" lean-forward="true" selection-start-line="61" selection-start-column="19" selection-end-line="61" selection-end-column="19" />
</state>
</provider>
</entry>

Binary file not shown.

Binary file not shown.

17
game.py
View File

@ -40,8 +40,8 @@ class Game(object):
self.row.append(self.waiter)
self.waiterNumberInGrid.x = i
self.waiterNumberInGrid.y = b
elif ((((b > 0 and b < 6) and (i > 0 and i < 9)) and not((i==1 or i == 8) and (b==1 or b==5))) \
or (b>6 and (i<4 or i > 5) ) ):
elif (((((b > 0 and b < 6) and (i > 0 and i < 9)) and not((i==1 or i == 8) and (b==1 or b==5))) \
or (b>6 and (i<4 or i > 5) ))) and not i == 4:
self.row.append(Pool(i * 50, b * 50, self))
else:
self.row.append(GridElement(i * 50, b * 50, self))
@ -54,7 +54,7 @@ class Game(object):
#self.dfs(self.waiter, 3, [])
self.bfs(self.waiter, 1, [])
#self.bfs(self.waiter, 1, [])
self.bfs(self.waiter, 3, [])
#path = self.waiter.bfsFind([self.waiter.positionX, self.waiter.positionY], [], 1)
@ -105,17 +105,20 @@ class Game(object):
print(row)
def dfs(self, waiter, soughtID, operation=[]):
waiter.dfsFind([waiter.positionX, waiter.positionY], operation, soughtID)
paths = waiter.allPath
visited = []
waiter.dfsFind([waiter.positionX, waiter.positionY], operation, visited, soughtID)
paths = waiter.dfsPaths
paths.sort(key=len)
print("Wszystkie sciezki dfs: ", paths)
bestPath = paths[0]
waiter.allPath = []
waiter.dfsPaths = []
waiter.followThePath(bestPath)
def bfs(self, waiter, soughtID, operation=[]):
waiter.bfsFind([waiter.positionX, waiter.positionY], operation, soughtID)
waiter.bfsFind([waiter.positionX, waiter.positionY], operation, [], soughtID)
paths = waiter.bfsPaths
paths.sort(key=len)
print("Wszystkie sciezki bfs: ", paths)
bestPath = paths[0]
waiter.bfsPaths = []
waiter.followThePath(bestPath)

View File

@ -12,6 +12,7 @@ class Table(object):
game.idItem += 1
self.number = game.idItem
self.isBusy = False
self.isClean = True
self.id = game.idTable
self.image = pygame.image.load("./Images/tableSmall.png")
self.type = "table"

226
waiter.py
View File

@ -23,7 +23,7 @@ class Waiter(object):
self.dfsStack = []
self.allPath = []
self.dfsPaths = []
self.bfsQueue = []
@ -233,123 +233,42 @@ class Waiter(object):
stackMove.append("Right")
return stackMove
def dfsFind(self, position, currentOperation, idTable):
print("Sprawdzam czy stolik")
def dfsFind(self, position, currentOperation, visited, idTable):
#print("Sprawdzam czy stolik")
positionX = position[0]
positionY = position[1]
if self.isMoveInRange("Up", position):
if self.grid[positionY - 1][positionX].type == "table":
if self.grid[positionY - 1][positionX].id == idTable:
self.allPath.append(currentOperation)
print("Dodalem do all path sciezke jedna z miliona")
return 0
if self.isMoveInRange("Down", position):
if self.grid[positionY + 1][positionX].type == "table":
if self.grid[positionY + 1][positionX].id == idTable:
self.allPath.append(currentOperation)
print("Dodalem do all path sciezke jedna z miliona")
return 0
if self.isMoveInRange("Left", position):
if self.grid[positionY][positionX - 1].type == "table":
if self.grid[positionY][positionX - 1].id == idTable:
self.allPath.append(currentOperation)
print("Dodalem do all path sciezke jedna z miliona")
return 0
if self.isMoveInRange("Right", position):
if self.grid[positionY][positionX + 1].type == "table":
if self.grid[positionY][positionX + 1].id == idTable:
self.allPath.append(currentOperation)
print("Dodalem do all path sciezke jedna z miliona")
return 0
#print("Sprawdzilem nie jest to stolik")
steps = []
steps.append(self.checkPoss(position, currentOperation))
allstep = steps[-1]
print("Naszymi mozliwosciami sa ", allstep)
if not allstep:
return 0
elif len(allstep) != 1:
for i in allstep:
newCurrentOperation = currentOperation[:]
newCurrentOperation.append(i)
newPosition = position[:]
if i == "Left":
newPosition[0] -= 1
if i == "Right":
newPosition[0] += 1
if i == "Up":
newPosition[1] -= 1
if i == "Down":
newPosition[1] += 1
self.dfsStack.append([newPosition, newCurrentOperation, idTable])
while self.dfsStack:
move = self.dfsStack.pop()
self.dfsFind(move[0], move[1], move[2])
else:
step = allstep.pop()
print("Wybrałem: ", step)
print("Pozycja kelnera: ", position)
if step == "Left":
position[0] -= 1
currentOperation.append("Left")
if step == "Right":
position[0] += 1
currentOperation.append("Right")
if step == "Up":
position[1] -= 1
currentOperation.append("Up")
if step == "Down":
position[1] += 1
currentOperation.append("Down")
self.dfsFind(position, currentOperation, idTable)
def bfsFind(self, position, currentOperation, idTable):
print("Sprawdzam czy stolik")
positionX = position[0]
positionY = position[1]
print(position)
#print("Pozycja kelnera: ", position)
if self.isMoveInRange("Up", position):
if self.grid[positionY - 1][positionX].type == "table":
if self.grid[positionY - 1][positionX].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem do BFS path sciezke jedna z miliona")
print(self.bfsPaths)
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
if self.isMoveInRange("Down", position):
if self.grid[positionY + 1][positionX].type == "table":
if self.grid[positionY + 1][positionX].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem do BFS path sciezke jedna z miliona")
print(self.bfsPaths)
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
if self.isMoveInRange("Left", position):
if self.grid[positionY][positionX - 1].type == "table":
if self.grid[positionY][positionX - 1].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem do BFS path sciezke jedna z miliona")
print(self.bfsPaths)
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
if self.isMoveInRange("Right", position):
if self.grid[positionY][positionX + 1].type == "table":
if self.grid[positionY][positionX + 1].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem do BFS path sciezke jedna z miliona")
print(self.bfsPaths)
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
# print("Sprawdzilem nie jest to stolik")
steps = []
steps.append(self.checkPoss(position, currentOperation))
allstep = steps[-1]
print("Naszymi mozliwosciami sa ", allstep)
# print("Naszymi mozliwosciami sa ", allstep)
if not allstep:
return 0
else:
@ -357,24 +276,121 @@ class Waiter(object):
newCurrentOperation = currentOperation[:]
newCurrentOperation.append(step)
newPosition = position[:]
if step == "Left":
newPosition[0] -= 1
newVisited = visited[:]
#print("newVisited", newVisited)
if step == "Left":
if [newPosition[0] - 1, newPosition[1]] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[0] -= 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
elif step == "Right":
newPosition[0] += 1
if [newPosition[0] + 1, newPosition[1]] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[0] += 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
elif step == "Up":
if [newPosition[0], newPosition[1] - 1] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[1] -= 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
else:
if [newPosition[0], newPosition[1] + 1] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[1] += 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
self.dfsStack.append([newPosition, newCurrentOperation, newVisited, idTable])
while self.dfsStack:
move = self.dfsStack.pop()
self.dfsFind(move[0], move[1], move[2], move[3])
self.bfsQueue.append([newPosition, newCurrentOperation, idTable])
#print("Liczba elementow w kolejce", self.bfsQueue)
while self.bfsQueue:
move = self.bfsQueue.pop(0)
self.bfsFind(move[0], move[1], move[2])
def bfsFind(self, position, currentOperation, visited, idTable):
#print("Sprawdzam czy stolik")
positionX = position[0]
positionY = position[1]
# print("Pozycja kelnera: ", position)
if self.isMoveInRange("Up", position):
if self.grid[positionY - 1][positionX].type == "table":
if self.grid[positionY - 1][positionX].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
if self.isMoveInRange("Down", position):
if self.grid[positionY + 1][positionX].type == "table":
if self.grid[positionY + 1][positionX].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
if self.isMoveInRange("Left", position):
if self.grid[positionY][positionX - 1].type == "table":
if self.grid[positionY][positionX - 1].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
if self.isMoveInRange("Right", position):
if self.grid[positionY][positionX + 1].type == "table":
if self.grid[positionY][positionX + 1].id == idTable:
self.bfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
# print("Sprawdzilem nie jest to stolik")
steps = []
steps.append(self.checkPoss(position, currentOperation))
allstep = steps[-1]
# print("Naszymi mozliwosciami sa ", allstep)
if not allstep:
return 0
else:
for step in allstep:
newCurrentOperation = currentOperation[:]
newCurrentOperation.append(step)
newPosition = position[:]
newVisited = visited[:]
# print("newVisited", newVisited)
if step == "Left":
if [newPosition[0] - 1, newPosition[1]] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[0] -= 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
elif step == "Right":
if [newPosition[0] + 1, newPosition[1]] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[0] += 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
elif step == "Up":
if [newPosition[0], newPosition[1] - 1] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[1] -= 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
else:
if [newPosition[0], newPosition[1] + 1] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
newPosition[1] += 1
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
self.bfsQueue.append([newPosition, newCurrentOperation, newVisited, idTable])
while self.bfsQueue:
move = self.bfsQueue.pop(0)
self.bfsFind(move[0], move[1],move[2], move[3])
def followThePath(self, path):
for direction in path: