DFS znajduje wszystkie mozliwe sciezki

This commit is contained in:
Arkadiusz 2019-05-07 22:43:21 +01:00
parent 8da63cae25
commit fe478e5ff8
4 changed files with 187 additions and 163 deletions

View File

@ -17,17 +17,17 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/order.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-7">
<state relative-caret-position="132">
<caret line="6" selection-start-line="6" selection-end-line="6" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/game.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="286">
<caret line="56" column="52" lean-forward="true" selection-start-line="56" selection-start-column="52" selection-end-line="56" selection-end-column="52" />
<state relative-caret-position="273">
<caret line="54" column="8" selection-start-line="54" selection-start-column="8" selection-end-line="54" selection-end-column="8" />
</state>
</provider>
</entry>
@ -35,8 +35,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/gridElement.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="308">
<caret line="14" column="59" selection-start-line="14" selection-start-column="59" selection-end-line="14" selection-end-column="59" />
<state relative-caret-position="154">
<caret line="7" column="17" selection-start-line="7" selection-start-column="17" selection-end-line="7" selection-end-column="17" />
</state>
</provider>
</entry>
@ -53,7 +53,7 @@
<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="198">
<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" />
<folding>
<element signature="e#0#13#0" expanded="true" />
@ -65,26 +65,27 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/orderTable.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-7">
<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>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/waiter.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-1225">
<caret line="175" column="27" selection-start-line="175" selection-start-column="27" selection-end-line="175" selection-end-column="27" />
<state relative-caret-position="281">
<caret line="290" column="53" selection-start-line="290" selection-start-column="53" selection-end-line="290" selection-end-column="53" />
<folding>
<element signature="e#0#13#0" expanded="true" />
<element signature="e#546#709#0" />
<element signature="e#758#1456#0" />
<element signature="e#1495#2175#0" />
<element signature="e#2213#2975#0" />
<element signature="e#3409#3781#0" />
<element signature="e#3825#4033#0" />
<element signature="e#4080#4470#0" />
<element signature="e#572#736#0" />
<element signature="e#778#1419#0" />
<element signature="e#1460#2110#0" />
<element signature="e#2789#3398#0" />
<element signature="e#3436#3808#0" />
<element signature="e#3839#4047#0" />
<element signature="e#4099#4564#0" />
<element signature="e#4621#8896#0" />
</folding>
</state>
</provider>
@ -126,16 +127,15 @@
<option value="$PROJECT_DIR$/table.py" />
<option value="$PROJECT_DIR$/orderTable.py" />
<option value="$PROJECT_DIR$/pool.py" />
<option value="$PROJECT_DIR$/waiter.py" />
<option value="$PROJECT_DIR$/game.py" />
<option value="$PROJECT_DIR$/waiter.py" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="716" />
<option name="y" value="77" />
<option name="width" value="970" />
<option name="height" value="1050" />
<option name="x" value="-7" />
<option name="width" value="974" />
<option name="height" value="1047" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
@ -269,13 +269,13 @@
<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.18336888" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.18335089" />
<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.29577464" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.3997833" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.27193934" />
<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" />
@ -332,18 +332,6 @@
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/waiter.py</url>
<line>284</line>
<option name="timeStamp" value="36" />
</line-breakpoint>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/game.py</url>
<line>54</line>
<option name="timeStamp" value="37" />
</line-breakpoint>
</breakpoints>
<default-breakpoints>
<breakpoint type="python-exception">
<properties notifyOnTerminate="true" exception="BaseException">
@ -354,10 +342,19 @@
</breakpoint-manager>
<watches-manager>
<configuration name="PythonConfigurationType">
<watch expression="grid" />
<watch expression="grid" language="Python" />
</configuration>
</watches-manager>
</component>
<component name="debuggerHistoryManager">
<expressions id="watch">
<expression>
<expression-string>grid</expression-string>
<language-id>Python</language-id>
<evaluation-mode>EXPRESSION</evaluation-mode>
</expression>
</expressions>
</component>
<component name="editorHistoryManager">
<entry file="file://$USER_HOME$/Desktop/pygame-elsa/sprites/olaf.py">
<provider selected="true" editor-type-id="text-editor">
@ -417,44 +414,6 @@
<entry file="file://$PROJECT_DIR$/Images/tlo.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/order.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-7">
<caret line="6" selection-start-line="6" selection-end-line="6" />
</state>
</provider>
</entry>
<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>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/pool.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="198">
<caret line="9" column="26" selection-start-line="9" selection-start-column="26" selection-end-line="9" selection-end-column="26" />
<folding>
<element signature="e#0#13#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/orderTable.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-7">
<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$/gridElement.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="308">
<caret line="14" column="59" selection-start-line="14" selection-start-column="59" selection-end-line="14" selection-end-column="59" />
</state>
</provider>
</entry>
<entry file="file://$USER_HOME$/.PyCharmCE2018.3/system/python_stubs/-258859485/builtins.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="299">
@ -469,27 +428,66 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/waiter.py">
<entry file="file://$PROJECT_DIR$/order.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-1225">
<caret line="175" column="27" selection-start-line="175" selection-start-column="27" selection-end-line="175" selection-end-column="27" />
<state relative-caret-position="132">
<caret line="6" selection-start-line="6" selection-end-line="6" />
</state>
</provider>
</entry>
<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>
</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" />
<folding>
<element signature="e#0#13#0" expanded="true" />
<element signature="e#546#709#0" />
<element signature="e#758#1456#0" />
<element signature="e#1495#2175#0" />
<element signature="e#2213#2975#0" />
<element signature="e#3409#3781#0" />
<element signature="e#3825#4033#0" />
<element signature="e#4080#4470#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$/gridElement.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="154">
<caret line="7" column="17" selection-start-line="7" selection-start-column="17" selection-end-line="7" selection-end-column="17" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/game.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="286">
<caret line="56" column="52" lean-forward="true" selection-start-line="56" selection-start-column="52" selection-end-line="56" selection-end-column="52" />
<state relative-caret-position="273">
<caret line="54" column="8" selection-start-line="54" selection-start-column="8" selection-end-line="54" selection-end-column="8" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/waiter.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="281">
<caret line="290" column="53" selection-start-line="290" selection-start-column="53" selection-end-line="290" selection-end-column="53" />
<folding>
<element signature="e#0#13#0" expanded="true" />
<element signature="e#572#736#0" />
<element signature="e#778#1419#0" />
<element signature="e#1460#2110#0" />
<element signature="e#2789#3398#0" />
<element signature="e#3436#3808#0" />
<element signature="e#3839#4047#0" />
<element signature="e#4099#4564#0" />
<element signature="e#4621#8896#0" />
</folding>
</state>
</provider>
</entry>

Binary file not shown.

View File

@ -51,8 +51,8 @@ class Game(object):
#copyGrid = copy.copy(self.grid)
#copyGrid = []
#kopia = copy.deepcopy(self.grid[9][4])
kopia = [[num for num in line] for line in self.grid]
self.waiter.dfsFind(kopia, [], 2)
#kopia = [[num for num in line] for line in self.grid]
path = self.waiter.dfsFind([self.waiter.positionX, self.waiter.positionY], [], 2)
#print("Prawdziwa pozycja kelnera: {0}, {1} ", self.waiter.positionX, self.waiter.positionY)
#print(self.waiter.findWaiter(self.grid[:]))

172
waiter.py
View File

@ -6,6 +6,7 @@ class Waiter(object):
def __init__(self, game, x, y):
self.game = game
self.grid = game.grid
game.idItem += 1
self.size= self.game.screen.get_size()
self.x = x
@ -16,6 +17,7 @@ class Waiter(object):
self.image.set_colorkey((255, 255, 255))
self.type = "waiter"
self.lastStep = []
self.tmp = []
self.allPath = []
@ -124,8 +126,8 @@ class Waiter(object):
self.game.screen.blit(self.image, (self.x, self.y))
def isMoveInRange(self, move, grid):
position = self.findWaiter(grid)
def isMoveInRange(self, move, position):
positionX = position[0]
positionY = position[1]
@ -143,143 +145,167 @@ class Waiter(object):
return False
return True
def checkPoss(self, grid, lastOperation):
def checkPoss(self, position, lastOperation):
stackMove = []
position = self.findWaiter(grid)
positionX = position[0]
positionY = position[1]
if len(lastOperation) == 0:
if self.isMoveInRange("Up", grid):
collisionObjectUp = grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement":
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement" or collisionObjectUp.type == "waiter":
stackMove.append("Up")
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement" or collisionObjectDown.type == "waiter":
stackMove.append("Down")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement" or collisionObjectLeft.type == "waiter":
stackMove.append("Left")
if self.isMoveInRange("Right", grid):
collisionObjectRight = grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement":
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement" or collisionObjectRight.type == "waiter":
stackMove.append("Right")
return stackMove
else:
last = lastOperation[-1]
if last == "Left":
if self.isMoveInRange("Up", grid):
collisionObjectUp = grid[positionY - 1][positionX]
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
return stackMove
if last == "Right":
if self.isMoveInRange("Up", grid):
collisionObjectUp = grid[positionY - 1][positionX]
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Right", grid):
collisionObjectRight = grid[positionY][positionX + 1]
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
if last == "Up":
if self.isMoveInRange("Up", grid):
collisionObjectUp = grid[positionY - 1][positionX]
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
if self.isMoveInRange("Right", grid):
collisionObjectRight = grid[positionY][positionX + 1]
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
if last == "Down":
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
if self.isMoveInRange("Right", grid):
collisionObjectRight = grid[positionY][positionX + 1]
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
def dfsFind(self, grid, currentOperation, idTable):
def dfsFind(self, position, currentOperation, idTable):
print("Sprawdzam czy stolik")
position = self.findWaiter(grid)
positionX = position[0]
positionY = position[1]
if self.isMoveInRange("Up", grid):
if grid[positionY - 1][positionX].type == "table":
if grid[positionY - 1][positionX].id == idTable:
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)
return 0
if self.isMoveInRange("Down", grid):
if grid[positionY + 1][positionX].type == "table":
if grid[positionY + 1][positionX].id == idTable:
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)
return 0
if self.isMoveInRange("Left", grid):
if grid[positionY][positionX - 1].type == "table":
if grid[positionY][positionX - 1].id == idTable:
self.allPath.append(currentOperation)
return 0
if self.isMoveInRange("Right", grid):
if grid[positionY][positionX + 1].type == "table":
if grid[positionY][positionX + 1].id == idTable:
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)
return 0
print("Sprawdzilem nie jest to stolik")
steps = []
steps.append(self.checkPoss(grid, currentOperation))
print("Naszymi mozliwosciami sa ", steps)
step = steps[-1]
print("Wybieram : ", step[-1])
if step[-1] == "Left":
self.moveLeft(grid)
currentOperation.append("Left")
if step[-1] == "Right":
self.moveRight(grid)
currentOperation.append("Right")
if step[-1] == "Up":
self.moveUp(grid)
currentOperation.append("Up")
if step[-1] == "Down":
self.moveDown(grid)
currentOperation.append("Down")
steps.append(self.checkPoss(position, currentOperation))
allstep = steps[-1]
print("Naszymi mozliwosciami sa ", allstep)
self.dfsFind(grid[:], currentOperation[:], idTable)
if 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.tmp.append([newPosition, newCurrentOperation, idTable])
while self.tmp:
move = self.tmp.pop()
self.dfsFind(move[0], move[1], move[2])
else:
step = allstep.pop()
print("Wybrałem: ", step)
print("Pozycja tego cwela: ", 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)