Sztuczna_Inteligencja/waiter.py

312 lines
12 KiB
Python
Raw Normal View History

2019-03-19 23:36:06 +01:00
import pygame
import time
2019-03-19 23:36:06 +01:00
from pygame.math import Vector2
class Waiter(object):
2019-03-20 23:46:50 +01:00
def __init__(self, game, x, y):
2019-03-19 23:36:06 +01:00
self.game = game
2019-05-07 23:43:21 +02:00
self.grid = game.grid
2019-03-20 23:46:50 +01:00
game.idItem += 1
2019-03-19 23:36:06 +01:00
self.size= self.game.screen.get_size()
2019-03-20 23:46:50 +01:00
self.x = x
self.y = y
2019-04-17 01:28:24 +02:00
self.positionX = int(x / 50)
self.positionY = int(y / 50)
2019-04-17 00:12:39 +02:00
self.image = pygame.image.load("./Images/waiter.png").convert()
2019-03-19 23:36:06 +01:00
self.image.set_colorkey((255, 255, 255))
2019-03-20 23:46:50 +01:00
self.type = "waiter"
2019-04-17 00:43:36 +02:00
self.lastStep = []
2019-05-07 23:43:21 +02:00
self.tmp = []
2019-05-06 00:07:13 +02:00
self.allPath = []
2019-03-26 11:05:21 +01:00
2019-03-19 23:36:06 +01:00
2019-05-06 00:07:13 +02:00
def findWaiter(self, grid):
for y in range(10):
for x in range(10):
if grid[y][x].type =="waiter":
#print(x,y)
return [x, y]
def moveLeft(self, grid):
position = self.findWaiter(grid)
positionX = position[0]
positionY = position[1]
if positionX != 0:
collisionObject = grid[positionY][positionX - 1]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-06 00:07:13 +02:00
grid[positionY][positionX - 1].x += 50
grid[positionY][positionX].x -= 50
grid[positionY][positionX - 1], \
grid[positionY][positionX] = \
grid[positionY][positionX], \
grid[positionY][positionX - 1]
2019-04-17 01:33:18 +02:00
#print(self.positionX)
2019-05-06 00:07:13 +02:00
return grid
2019-03-24 19:57:16 +01:00
else:
pass
2019-05-06 00:07:13 +02:00
def moveRight(self, grid):
position = self.findWaiter(grid)
positionX = position[0]
positionY = position[1]
if positionX != 9:
collisionObject = grid[positionY][positionX + 1]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-06 00:07:13 +02:00
grid[positionY][positionX + 1].x -= 50
grid[positionY][positionX].x += 50
grid[positionY][positionX + 1], \
grid[positionY][positionX] = \
grid[positionY][positionX], \
grid[positionY][positionX + 1]
# print(self.positionX)
return grid
2019-03-24 19:57:16 +01:00
else:
pass
2019-05-06 00:07:13 +02:00
def moveUp(self, grid):
position = self.findWaiter(grid)
positionX = position[0]
positionY = position[1]
if positionY != 0:
collisionObject = grid[positionY - 1][positionX]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-06 00:07:13 +02:00
grid[positionY - 1][positionX].y += 50
grid[positionY][positionX].y -= 50
grid[positionY - 1][positionX], \
grid[positionY][positionX] = \
grid[positionY][positionX], \
grid[positionY - 1][positionX]
return grid
2019-03-24 19:57:16 +01:00
else:
pass
2019-05-06 00:07:13 +02:00
def moveDown(self, grid):
position = self.findWaiter(grid)
positionX = position[0]
positionY = position[1]
if positionY != 9:
collisionObject = grid[positionY + 1][positionX]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-06 00:07:13 +02:00
grid[positionY + 1][positionX].y -= 50
grid[positionY][positionX].y += 50
grid[positionY + 1][positionX], \
grid[positionY][positionX] = \
grid[positionY][positionX], \
grid[positionY + 1][positionX]
return grid
2019-03-24 19:57:16 +01:00
else:
pass
2019-03-19 23:36:06 +01:00
2019-03-20 23:46:50 +01:00
def move(self, game):
2019-03-19 23:36:06 +01:00
keys = pygame.key.get_pressed()
2019-03-20 23:46:50 +01:00
2019-03-24 19:57:16 +01:00
if keys[pygame.K_LEFT]:
2019-05-06 00:07:13 +02:00
self.moveLeft(game.grid)
2019-03-24 19:57:16 +01:00
if keys[pygame.K_RIGHT]:
2019-05-06 00:07:13 +02:00
self.moveRight(game.grid)
2019-03-24 19:57:16 +01:00
if keys[pygame.K_UP]:
2019-05-06 00:07:13 +02:00
self.moveUp(game.grid)
2019-03-24 19:57:16 +01:00
if keys[pygame.K_DOWN]:
2019-05-06 00:07:13 +02:00
self.moveDown(game.grid)
2019-03-24 19:57:16 +01:00
if keys[pygame.K_s]:
game.showGrid(game.grid)
2019-03-19 23:36:06 +01:00
2019-05-06 00:07:13 +02:00
2019-03-19 23:36:06 +01:00
def draw(self):
2019-03-20 23:46:50 +01:00
self.rect1 = pygame.Rect(self.x, self.y, 50, 50)
2019-03-20 20:42:55 +01:00
pygame.draw.rect(self.game.screen, (0, 150, 255), self.rect1)
2019-04-17 21:25:33 +02:00
#print(self.lastStep)
2019-04-17 00:12:39 +02:00
self.game.screen.blit(self.image, (self.x, self.y))
2019-04-17 21:25:33 +02:00
2019-05-07 23:43:21 +02:00
def isMoveInRange(self, move, position):
2019-05-06 00:07:13 +02:00
positionX = position[0]
positionY = position[1]
2019-04-17 21:25:33 +02:00
if move == "Left":
2019-05-06 00:07:13 +02:00
if positionX == 0:
2019-04-17 21:25:33 +02:00
return False
if move == "Right":
2019-05-06 00:07:13 +02:00
if positionX == 9:
2019-04-17 21:25:33 +02:00
return False
if move == "Up":
2019-05-06 00:07:13 +02:00
if positionY == 0:
2019-04-17 21:25:33 +02:00
return False
if move == "Down":
2019-05-06 00:07:13 +02:00
if positionY == 9:
2019-04-17 21:25:33 +02:00
return False
return True
2019-05-07 23:43:21 +02:00
def checkPoss(self, position, lastOperation):
2019-05-06 00:07:13 +02:00
stackMove = []
positionX = position[0]
positionY = position[1]
if len(lastOperation) == 0:
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement" or collisionObjectUp.type == "waiter":
2019-05-06 00:07:13 +02:00
stackMove.append("Up")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement" or collisionObjectDown.type == "waiter":
2019-05-06 00:07:13 +02:00
stackMove.append("Down")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement" or collisionObjectLeft.type == "waiter":
2019-05-06 00:07:13 +02:00
stackMove.append("Left")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement" or collisionObjectRight.type == "waiter":
2019-05-06 00:07:13 +02:00
stackMove.append("Right")
return stackMove
2019-04-17 21:25:33 +02:00
else:
2019-05-06 00:07:13 +02:00
last = lastOperation[-1]
if last == "Left":
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
2019-05-06 00:07:13 +02:00
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
2019-05-06 00:07:13 +02:00
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
2019-05-06 00:07:13 +02:00
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
return stackMove
if last == "Right":
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
2019-05-06 00:07:13 +02:00
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
2019-05-06 00:07:13 +02:00
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
2019-05-06 00:07:13 +02:00
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
if last == "Up":
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Up", position):
collisionObjectUp = self.grid[positionY - 1][positionX]
2019-05-06 00:07:13 +02:00
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
2019-05-06 00:07:13 +02:00
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
2019-05-06 00:07:13 +02:00
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
if last == "Down":
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Down", position):
collisionObjectDown = self.grid[positionY + 1][positionX]
2019-05-06 00:07:13 +02:00
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Left", position):
collisionObjectLeft = self.grid[positionY][positionX - 1]
2019-05-06 00:07:13 +02:00
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Right", position):
collisionObjectRight = self.grid[positionY][positionX + 1]
2019-05-06 00:07:13 +02:00
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
2019-05-07 23:43:21 +02:00
def dfsFind(self, position, currentOperation, idTable):
2019-05-06 00:07:13 +02:00
2019-04-17 22:08:51 +02:00
print("Sprawdzam czy stolik")
2019-05-07 23:43:21 +02:00
2019-05-06 00:07:13 +02:00
positionX = position[0]
positionY = position[1]
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Up", position):
if self.grid[positionY - 1][positionX].type == "table":
if self.grid[positionY - 1][positionX].id == idTable:
2019-05-06 00:07:13 +02:00
self.allPath.append(currentOperation)
2019-05-07 23:43:21 +02:00
print("Dodalem do all path sciezke jedna z miliona")
2019-05-06 00:07:13 +02:00
return 0
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Down", position):
if self.grid[positionY + 1][positionX].type == "table":
if self.grid[positionY + 1][positionX].id == idTable:
2019-05-06 00:07:13 +02:00
self.allPath.append(currentOperation)
return 0
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Left", position):
if self.grid[positionY][positionX - 1].type == "table":
if self.grid[positionY][positionX - 1].id == idTable:
2019-05-06 00:07:13 +02:00
self.allPath.append(currentOperation)
return 0
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Right", position):
if self.grid[positionY][positionX + 1].type == "table":
if self.grid[positionY][positionX + 1].id == idTable:
2019-05-06 00:07:13 +02:00
self.allPath.append(currentOperation)
return 0
print("Sprawdzilem nie jest to stolik")
2019-04-17 22:08:51 +02:00
steps = []
2019-05-07 23:43:21 +02:00
steps.append(self.checkPoss(position, currentOperation))
allstep = steps[-1]
print("Naszymi mozliwosciami sa ", allstep)
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)