Sztuczna_Inteligencja/waiter.py

448 lines
19 KiB
Python
Raw Normal View History

2019-05-15 11:35:42 +02:00
import asyncio
from multiprocessing import Queue
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
2019-05-15 11:35:42 +02:00
2019-03-19 23:36:06 +01:00
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-06-05 02:19:55 +02:00
self.whatKeep = 0
2019-05-15 11:35:42 +02:00
self.dfsStack = []
2019-05-19 22:46:37 +02:00
self.dfsPaths = []
2019-03-26 11:05:21 +01:00
2019-03-19 23:36:06 +01:00
2019-05-15 11:35:42 +02:00
self.bfsQueue = []
self.bfsPaths = []
2019-05-20 00:05:16 +02:00
# NIEUZYWANA FUNKCJA ZNAJDOWANIA KELNERA NA PLANSZY; ZWRACA POZYCJĘ KELNERA
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]
2019-05-08 01:24:04 +02:00
def moveLeft(self):
if self.positionX != 0:
collisionObject = self.game.grid[self.positionY][self.positionX - 1]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-08 01:24:04 +02:00
self.game.grid[self.positionY][self.positionX - 1].x += 50
self.game.grid[self.positionY][self.positionX].x -= 50
self.game.grid[self.positionY][self.positionX - 1], \
self.game.grid[self.positionY][self.positionX] = \
self.game.grid[self.positionY][self.positionX], \
self.game.grid[self.positionY][self.positionX - 1]
self.positionX -= 1
2019-04-17 01:33:18 +02:00
#print(self.positionX)
2019-05-08 01:24:04 +02:00
self.lastStep.append("Left")
2019-03-24 19:57:16 +01:00
else:
2019-06-05 02:19:55 +02:00
self.work(collisionObject)
2019-05-08 01:24:04 +02:00
def moveRight(self):
if self.positionX != self.game.x-1:
collisionObject = self.game.grid[self.positionY][self.positionX + 1]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-08 01:24:04 +02:00
self.game.grid[self.positionY][self.positionX + 1].x -= 50
self.game.grid[self.positionY][self.positionX].x += 50
self.game.grid[self.positionY][self.positionX + 1], \
self.game.grid[self.positionY][self.positionX] = \
self.game.grid[self.positionY][self.positionX], \
self.game.grid[self.positionY][self.positionX + 1]
self.positionX += 1
#print(self.positionX)
self.lastStep.append("Right")
2019-03-24 19:57:16 +01:00
else:
2019-06-05 02:19:55 +02:00
self.work(collisionObject)
2019-03-24 19:57:16 +01:00
2019-05-08 01:24:04 +02:00
def moveUp(self):
if self.positionY != 0:
collisionObject = self.game.grid[self.positionY - 1][self.positionX]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-08 01:24:04 +02:00
self.game.grid[self.positionY - 1][self.positionX].y += 50
self.game.grid[self.positionY][self.positionX].y -= 50
self.game.grid[self.positionY - 1][self.positionX], \
self.game.grid[self.positionY][self.positionX] = \
self.game.grid[self.positionY][self.positionX], \
self.game.grid[self.positionY - 1][self.positionX]
self.positionY -= 1
self.lastStep.append("Up")
2019-03-24 19:57:16 +01:00
else:
2019-06-05 02:19:55 +02:00
self.work(collisionObject)
2019-03-24 19:57:16 +01:00
2019-05-08 01:24:04 +02:00
def moveDown(self):
if self.positionY != self.game.y-1:
collisionObject = self.game.grid[self.positionY + 1][self.positionX]
2019-03-24 19:57:16 +01:00
if collisionObject.type == "gridElement":
2019-05-08 01:24:04 +02:00
self.game.grid[self.positionY + 1][self.positionX].y -= 50
self.game.grid[self.positionY][self.positionX].y += 50
self.game.grid[self.positionY + 1][self.positionX], \
self.game.grid[self.positionY][self.positionX] = \
self.game.grid[self.positionY][self.positionX], \
self.game.grid[self.positionY + 1][self.positionX]
self.positionY += 1
self.lastStep.append("Down")
2019-03-24 19:57:16 +01:00
else:
2019-06-05 02:19:55 +02:00
self.work(collisionObject)
2019-03-24 19:57:16 +01:00
2019-03-19 23:36:06 +01:00
2019-05-20 00:05:16 +02:00
# SPRAWDZA NACIŚNIĘTE KLAWISZE
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-08 01:24:04 +02:00
self.moveLeft()
2019-03-24 19:57:16 +01:00
if keys[pygame.K_RIGHT]:
2019-05-08 01:24:04 +02:00
self.moveRight()
2019-03-24 19:57:16 +01:00
if keys[pygame.K_UP]:
2019-05-08 01:24:04 +02:00
self.moveUp()
2019-03-24 19:57:16 +01:00
if keys[pygame.K_DOWN]:
2019-05-08 01:24:04 +02:00
self.moveDown()
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-06-05 02:19:55 +02:00
if self.whatKeep == 0:
self.game.screen.blit(self.image, (self.x, self.y))
elif self.whatKeep == 1:
self.game.screen.blit(pygame.image.load("./Images/waiterJedzenie.png"), (self.x, self.y))
elif self.whatKeep == 2:
self.game.screen.blit(pygame.image.load("./Images/waiterBrudneTalerze.png"), (self.x, self.y))
2019-04-17 21:25:33 +02:00
2019-05-20 00:05:16 +02:00
# SPRAWDZA CZY ELELEMNT, KTORY POBIERAMY NIE JEST SPOZA PLANSZY
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-20 00:05:16 +02:00
# SPRAWDZA MOZLIWOSCI RUCHU ZGODNIE Z OSTATNIM RUCHEM
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]
2019-05-08 01:24:04 +02:00
if collisionObjectDown.type == "gridElement":
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-20 00:05:16 +02:00
# DFS
2019-05-19 22:46:37 +02:00
def dfsFind(self, position, currentOperation, visited, idTable):
#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-19 22:46:37 +02:00
#print("Pozycja kelnera: ", position)
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Up", position):
if self.grid[positionY - 1][positionX].type == "table":
2019-05-19 22:46:37 +02:00
if self.grid[positionY - 1][positionX].id == idTable:
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Down", position):
if self.grid[positionY + 1][positionX].type == "table":
2019-05-19 22:46:37 +02:00
if self.grid[positionY + 1][positionX].id == idTable:
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Left", position):
if self.grid[positionY][positionX - 1].type == "table":
2019-05-19 22:46:37 +02:00
if self.grid[positionY][positionX - 1].id == idTable:
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", currentOperation)
return 0
2019-05-07 23:43:21 +02:00
if self.isMoveInRange("Right", position):
if self.grid[positionY][positionX + 1].type == "table":
2019-05-19 22:46:37 +02:00
if self.grid[positionY][positionX + 1].id == idTable:
self.dfsPaths.append(currentOperation)
print("Dodalem sciezke: ", 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]
2019-05-19 22:46:37 +02:00
# print("Naszymi mozliwosciami sa ", allstep)
2019-05-08 01:24:04 +02:00
if not allstep:
return 0
2019-05-19 22:46:37 +02:00
else:
for step in allstep:
2019-05-07 23:43:21 +02:00
newCurrentOperation = currentOperation[:]
2019-05-19 22:46:37 +02:00
newCurrentOperation.append(step)
2019-05-07 23:43:21 +02:00
newPosition = position[:]
2019-05-19 22:46:37 +02:00
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
2019-05-07 23:43:21 +02:00
newPosition[0] -= 1
2019-05-19 22:46:37 +02:00
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
2019-05-07 23:43:21 +02:00
newPosition[0] += 1
2019-05-19 22:46:37 +02:00
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
2019-05-07 23:43:21 +02:00
newPosition[1] -= 1
2019-05-19 22:46:37 +02:00
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
2019-05-07 23:43:21 +02:00
newPosition[1] += 1
2019-05-19 22:46:37 +02:00
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
self.dfsStack.append([newPosition, newCurrentOperation, newVisited, idTable])
2019-05-15 11:35:42 +02:00
while self.dfsStack:
move = self.dfsStack.pop()
2019-05-19 22:46:37 +02:00
self.dfsFind(move[0], move[1], move[2], move[3])
2019-05-20 00:05:16 +02:00
# BFS
2019-05-19 22:46:37 +02:00
def bfsFind(self, position, currentOperation, visited, idTable):
#print("Sprawdzam czy stolik")
2019-05-15 11:35:42 +02:00
positionX = position[0]
positionY = position[1]
2019-05-19 22:46:37 +02:00
# print("Pozycja kelnera: ", position)
2019-05-15 11:35:42 +02:00
if self.isMoveInRange("Up", position):
if self.grid[positionY - 1][positionX].type == "table":
if self.grid[positionY - 1][positionX].id == idTable:
2019-06-05 02:19:55 +02:00
currentOperation.append("Up")
2019-05-15 11:35:42 +02:00
self.bfsPaths.append(currentOperation)
2019-05-19 22:46:37 +02:00
print("Dodalem sciezke: ", currentOperation)
2019-05-15 11:35:42 +02:00
return 0
if self.isMoveInRange("Down", position):
if self.grid[positionY + 1][positionX].type == "table":
if self.grid[positionY + 1][positionX].id == idTable:
2019-06-05 02:19:55 +02:00
currentOperation.append("Down")
2019-05-15 11:35:42 +02:00
self.bfsPaths.append(currentOperation)
2019-05-19 22:46:37 +02:00
print("Dodalem sciezke: ", currentOperation)
2019-05-15 11:35:42 +02:00
return 0
if self.isMoveInRange("Left", position):
if self.grid[positionY][positionX - 1].type == "table":
if self.grid[positionY][positionX - 1].id == idTable:
2019-06-05 02:19:55 +02:00
currentOperation.append("Left")
2019-05-15 11:35:42 +02:00
self.bfsPaths.append(currentOperation)
2019-05-19 22:46:37 +02:00
print("Dodalem sciezke: ", currentOperation)
2019-05-15 11:35:42 +02:00
return 0
if self.isMoveInRange("Right", position):
if self.grid[positionY][positionX + 1].type == "table":
if self.grid[positionY][positionX + 1].id == idTable:
2019-06-05 02:19:55 +02:00
currentOperation.append("Right")
2019-05-15 11:35:42 +02:00
self.bfsPaths.append(currentOperation)
2019-05-19 22:46:37 +02:00
print("Dodalem sciezke: ", currentOperation)
2019-05-15 11:35:42 +02:00
return 0
# print("Sprawdzilem nie jest to stolik")
steps = []
steps.append(self.checkPoss(position, currentOperation))
allstep = steps[-1]
2019-05-19 22:46:37 +02:00
# print("Naszymi mozliwosciami sa ", allstep)
2019-05-15 11:35:42 +02:00
if not allstep:
return 0
else:
for step in allstep:
newCurrentOperation = currentOperation[:]
newCurrentOperation.append(step)
newPosition = position[:]
2019-05-19 22:46:37 +02:00
newVisited = visited[:]
# print("newVisited", newVisited)
2019-05-15 11:35:42 +02:00
if step == "Left":
2019-05-19 22:46:37 +02:00
if [newPosition[0] - 1, newPosition[1]] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
2019-05-15 11:35:42 +02:00
newPosition[0] -= 1
2019-05-19 22:46:37 +02:00
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
2019-05-15 11:35:42 +02:00
elif step == "Right":
2019-05-19 22:46:37 +02:00
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)
2019-05-15 11:35:42 +02:00
elif step == "Up":
2019-05-19 22:46:37 +02:00
if [newPosition[0], newPosition[1] - 1] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
2019-05-15 11:35:42 +02:00
newPosition[1] -= 1
2019-05-19 22:46:37 +02:00
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
2019-05-15 11:35:42 +02:00
else:
2019-05-19 22:46:37 +02:00
if [newPosition[0], newPosition[1] + 1] in visited:
# print("Tam gdzie chce isc bylem juz: ", step)
continue
2019-05-15 11:35:42 +02:00
newPosition[1] += 1
2019-05-19 22:46:37 +02:00
if len(self.checkPoss(newPosition, newCurrentOperation)) == 1:
newVisited.append(newPosition)
self.bfsQueue.append([newPosition, newCurrentOperation, newVisited, idTable])
2019-06-05 01:09:16 +02:00
while not(self.bfsPaths) and self.bfsQueue:
2019-05-19 22:46:37 +02:00
move = self.bfsQueue.pop(0)
self.bfsFind(move[0], move[1],move[2], move[3])
2019-05-15 11:35:42 +02:00
2019-05-20 00:05:16 +02:00
# PORUSZA KELNERA WEDŁUG LISTY KROKOW
2019-05-08 01:24:04 +02:00
def followThePath(self, path):
for direction in path:
if direction == "Left":
self.moveLeft()
if direction == "Right":
self.moveRight()
if direction == "Up":
self.moveUp()
if direction == "Down":
self.moveDown()
self.game.draw()
2019-06-05 02:19:55 +02:00
time.sleep(.50)
def takeDish(self, orderTable):
orderTable.hasDish = False
self.whatKeep = 1
def putDish(self, table):
self.whatKeep = 0
table.hasDish = True
table.isWaiting = False
def takePlate(self, table):
table.hasDish = False
table.isClean = True
self.whatKeep = 2
def putPlate(self):
self.whatKeep = 0
def takeOrder(self, table):
table.isOrdering = False
def work(self, object: object) -> object:
if object.type == "wasteTable":
self.putPlate()
elif object.type == "orderTable":
self.takeDish(object)
elif object.type == "table":
if not object.isClean:
self.takePlate(object)
elif object.isOrdering:
self.takeOrder(object)
elif object.isWaiting and self.whatKeep == 1:
self.putDish(object)