Sztuczna_Inteligencja/waiter.py

390 lines
16 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-05-15 11:35:42 +02:00
self.dfsStack = []
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-15 11:35:42 +02:00
self.bfsQueue = []
self.bfsPaths = []
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:
pass
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:
pass
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:
pass
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:
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-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-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]
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-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)
2019-05-08 01:24:04 +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("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)
2019-05-08 01:24:04 +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("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)
2019-05-08 01:24:04 +02:00
print("Dodalem do all path sciezke jedna z miliona")
2019-05-06 00:07:13 +02:00
return 0
2019-05-08 01:24:04 +02:00
#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)
2019-05-08 01:24:04 +02:00
if not allstep:
return 0
elif len(allstep) != 1:
2019-05-07 23:43:21 +02:00
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
2019-05-15 11:35:42 +02:00
self.dfsStack.append([newPosition, newCurrentOperation, idTable])
while self.dfsStack:
move = self.dfsStack.pop()
2019-05-07 23:43:21 +02:00
self.dfsFind(move[0], move[1], move[2])
else:
step = allstep.pop()
print("Wybrałem: ", step)
2019-05-15 11:35:42 +02:00
print("Pozycja kelnera: ", position)
2019-05-07 23:43:21 +02:00
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)
2019-05-08 01:24:04 +02:00
2019-05-15 11:35:42 +02:00
def bfsFind(self, position, currentOperation, idTable):
print("Sprawdzam czy stolik")
positionX = position[0]
positionY = position[1]
print(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)
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)
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)
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)
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[:]
if step == "Left":
newPosition[0] -= 1
elif step == "Right":
newPosition[0] += 1
elif step == "Up":
newPosition[1] -= 1
else:
newPosition[1] += 1
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])
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()
time.sleep(.500)