diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 28ef5c2..2fc22d1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,17 +17,17 @@ - + - + - - + + @@ -35,8 +35,8 @@ - - + + @@ -53,7 +53,7 @@ - + @@ -65,26 +65,27 @@ - + - + - - + + - - - - - - - + + + + + + + + @@ -126,16 +127,15 @@ - @@ -269,13 +269,13 @@ - + - + @@ -332,18 +332,6 @@ - - - file://$PROJECT_DIR$/waiter.py - 284 - - - file://$PROJECT_DIR$/game.py - 54 - - @@ -354,10 +342,19 @@ - + + + + + grid + Python + EXPRESSION + + + @@ -417,44 +414,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -469,27 +428,66 @@ - + - - + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + diff --git a/__pycache__/waiter.cpython-37.pyc b/__pycache__/waiter.cpython-37.pyc index 446510f..6615e37 100644 Binary files a/__pycache__/waiter.cpython-37.pyc and b/__pycache__/waiter.cpython-37.pyc differ diff --git a/game.py b/game.py index 650bff4..b89e362 100644 --- a/game.py +++ b/game.py @@ -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[:])) diff --git a/waiter.py b/waiter.py index 7118110..35f45e8 100644 --- a/waiter.py +++ b/waiter.py @@ -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)