Sztuczna_Inteligencja/waiter.py

286 lines
11 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-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-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-06 00:07:13 +02:00
def isMoveInRange(self, move, grid):
position = self.findWaiter(grid)
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-06 00:07:13 +02:00
def checkPoss(self, grid, 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":
stackMove.append("Up")
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
if self.isMoveInRange("Right", grid):
collisionObjectRight = grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement":
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":
if self.isMoveInRange("Up", grid):
collisionObjectUp = grid[positionY - 1][positionX]
if collisionObjectUp.type == "gridElement":
stackMove.append("Up")
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = 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 collisionObjectUp.type == "gridElement":
stackMove.append("Up")
if self.isMoveInRange("Down", grid):
collisionObjectDown = grid[positionY + 1][positionX]
if collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Right", grid):
collisionObjectRight = 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 collisionObjectUp.type == "gridElement":
stackMove.append("Up")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
if self.isMoveInRange("Right", grid):
collisionObjectRight = 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 collisionObjectDown.type == "gridElement":
stackMove.append("Down")
if self.isMoveInRange("Left", grid):
collisionObjectLeft = grid[positionY][positionX - 1]
if collisionObjectLeft.type == "gridElement":
stackMove.append("Left")
if self.isMoveInRange("Right", grid):
collisionObjectRight = grid[positionY][positionX + 1]
if collisionObjectRight.type == "gridElement":
stackMove.append("Right")
return stackMove
def dfsFind(self, grid, currentOperation, idTable):
2019-04-17 22:08:51 +02:00
print("Sprawdzam czy stolik")
2019-05-06 00:07:13 +02:00
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:
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:
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:
self.allPath.append(currentOperation)
return 0
print("Sprawdzilem nie jest to stolik")
2019-04-17 22:08:51 +02:00
steps = []
2019-05-06 00:07:13 +02:00
steps.append(self.checkPoss(grid, currentOperation))
print("Naszymi mozliwosciami sa ", steps)
2019-04-17 22:08:51 +02:00
step = steps[-1]
2019-05-06 00:07:13 +02:00
print("Wybieram : ", step[-1])
if step[-1] == "Left":
2019-05-06 00:07:13 +02:00
self.moveLeft(grid)
currentOperation.append("Left")
if step[-1] == "Right":
2019-05-06 00:07:13 +02:00
self.moveRight(grid)
currentOperation.append("Right")
if step[-1] == "Up":
2019-05-06 00:07:13 +02:00
self.moveUp(grid)
currentOperation.append("Up")
if step[-1] == "Down":
2019-05-06 00:07:13 +02:00
self.moveDown(grid)
currentOperation.append("Down")
self.dfsFind(grid[:], currentOperation[:], idTable)