Sztuczna_Inteligencja/game.py

161 lines
5.6 KiB
Python
Raw Normal View History

2019-06-05 01:09:16 +02:00
import pygame, sys, random
2019-03-19 23:36:06 +01:00
from waiter import Waiter
from table import Table
2019-03-20 23:46:50 +01:00
from gridElement import GridElement
2019-03-24 19:57:16 +01:00
from pygame.math import Vector2
2019-03-26 11:05:21 +01:00
from orderTable import OrderTable
2019-04-17 00:12:39 +02:00
from pool import Pool
2019-06-05 02:19:55 +02:00
from wasteTable import WasteTable
2019-06-05 01:09:16 +02:00
from operator import attrgetter
2019-05-06 00:07:13 +02:00
import copy
2019-03-19 23:36:06 +01:00
class Game(object):
def __init__(self):
pygame.init()
2019-04-17 00:12:39 +02:00
self.x = 10
self.y = 10
2019-03-24 19:57:16 +01:00
self.screen = pygame.display.set_mode((self.x * 50, self.y * 50))
2019-03-19 23:36:06 +01:00
self.fpsClock = pygame.time.Clock()
self.idTable = 0
2019-03-20 20:42:55 +01:00
self.idOrder = 0
2019-03-20 23:46:50 +01:00
self.idItem = -1
2019-03-26 11:05:21 +01:00
self.idOrderTable = 0
2019-03-24 19:57:16 +01:00
self.waiterNumberInGrid = Vector2()
2019-03-19 23:36:06 +01:00
pygame.display.set_caption('Automatic Waiter')
self.background = pygame.image.load("./Images/tlo.jpg")
2019-03-24 19:57:16 +01:00
# The most important lists
2019-03-20 20:42:55 +01:00
self.grid = []
2019-03-24 19:57:16 +01:00
self.row = []
2019-03-26 21:12:14 +01:00
self.tableList = []
2019-06-05 01:09:16 +02:00
self.orderTableList = []
2019-05-20 00:05:16 +02:00
# TWORZENIE ELEMENTÓW PLANSZY
2019-03-24 19:57:16 +01:00
for b in range(self.y):
for i in range(self.x):
2019-04-17 00:12:39 +02:00
if ((i == 0) or (i == 9)) and ((b == 0) or (b == 6)):
2019-06-05 01:09:16 +02:00
tmp = random.randint(0, 3)
table = Table(i * 50, b * 50, self)
if tmp == 1:
table.isOrdering = True
elif tmp == 2:
table.isWaiting = True
elif tmp == 3:
table.isClean = False
2019-03-26 21:12:14 +01:00
self.row.append(table)
self.tableList.append(table)
2019-06-05 01:09:16 +02:00
elif (b == self.y-1 or b == self.y-2 or b == self.y-3) and (i == 5):
orderTable = OrderTable(i * 50, b * 50, self)
self.row.append(orderTable)
self.orderTableList.append(orderTable)
2019-04-17 00:12:39 +02:00
elif i == 4 and b == 9:
2019-04-17 01:28:24 +02:00
self.waiter = Waiter(self, i * 50, b * 50)
self.row.append(self.waiter)
2019-03-24 19:57:16 +01:00
self.waiterNumberInGrid.x = i
self.waiterNumberInGrid.y = b
2019-05-20 00:05:16 +02:00
elif (((((b > 0 and b < 6) and (i > 0 and i < 9)) and not((i == 1 or i == 8) and (b == 1 or b == 5))) \
2019-06-05 02:19:55 +02:00
or (b > 6 and (i < 4 or i > 5)))) and not i == 4 and not (b == 3) and not (i==3 and b==9):
2019-04-17 00:12:39 +02:00
self.row.append(Pool(i * 50, b * 50, self))
2019-06-05 02:19:55 +02:00
elif i == 3 and b == 9:
self.row.append(WasteTable(i * 50, b * 50, self))
2019-03-20 23:46:50 +01:00
else:
2019-03-24 19:57:16 +01:00
self.row.append(GridElement(i * 50, b * 50, self))
2019-04-17 00:12:39 +02:00
if i == self.x-1:
2019-03-24 19:57:16 +01:00
self.grid.append(list(self.row))
self.row.clear()
2019-05-08 01:24:04 +02:00
2019-06-05 01:09:16 +02:00
#Wydawka jedzenia
for table in self.tableList:
if table.isWaiting:
self.orderTableList[0].hasDish = True
self.orderTableList.sort(key=attrgetter("hasDish"))
2019-05-08 01:24:04 +02:00
2019-05-15 11:35:42 +02:00
#self.dfs(self.waiter, 1, [])
2019-05-08 01:24:04 +02:00
#self.dfs(self.waiter, 3, [])
2019-05-06 00:07:13 +02:00
2019-05-20 00:05:16 +02:00
self.bfs(self.waiter, 1, [])
self.bfs(self.waiter, 4, [])
2019-05-15 11:35:42 +02:00
#path = self.waiter.bfsFind([self.waiter.positionX, self.waiter.positionY], [], 1)
2019-05-20 00:05:16 +02:00
# GŁÓWNA PĘTLA GRY
2019-03-19 23:36:06 +01:00
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
self.draw()
2019-04-17 01:28:24 +02:00
self.waiter.move(self)
2019-05-06 00:07:13 +02:00
#print(self.waiter.checkPoss(self.grid, self.waiter.lastStep))
2019-05-08 01:24:04 +02:00
2019-03-26 11:05:21 +01:00
#print(self.waiterNumberInGrid)
2019-03-19 23:36:06 +01:00
self.fpsClock.tick(10)
2019-05-20 00:05:16 +02:00
# WYRYSOWANIE KAZDEGO ELEMENTU Z GRIDU NA EKRAN
2019-03-19 23:36:06 +01:00
def draw(self):
2019-03-20 23:46:50 +01:00
tempTable = []
2019-03-24 19:57:16 +01:00
for row in self.grid:
for gridElement in row:
if gridElement.type == "gridElement":
gridElement.draw()
else:
tempTable.append(gridElement)
2019-03-20 23:46:50 +01:00
for otherElement in tempTable:
otherElement.draw()
2019-05-08 01:24:04 +02:00
pygame.display.flip()
2019-03-20 23:46:50 +01:00
2019-03-24 19:57:16 +01:00
def convert(self, object):
if object.type == "gridElement":
return 'G'
elif object.type == "table":
return 'T'
elif object.type == "waiter":
return 'W'
2019-03-26 11:05:21 +01:00
elif object.type == "orderTable":
return 'O'
2019-04-17 00:12:39 +02:00
elif object.type == "pool":
return 'P'
2019-06-05 02:19:55 +02:00
elif object.type == "wasteTable":
return 'Z'
2019-03-24 19:57:16 +01:00
2019-05-20 00:05:16 +02:00
2019-03-24 19:57:16 +01:00
def showGrid(self, grid):
charakterList = []
row = []
for y in range(self.y):
row = list(map(self.convert, grid[y]))
charakterList.append(row)
print(row)
2019-05-20 00:05:16 +02:00
# ZNAJDUJE NAJKRÓTSZĄ ŚCIEŻKĘ DFS I PRZECHODZI WEDŁUG LISTY KROKÓW
2019-05-08 01:24:04 +02:00
def dfs(self, waiter, soughtID, operation=[]):
2019-05-19 22:46:37 +02:00
visited = []
waiter.dfsFind([waiter.positionX, waiter.positionY], operation, visited, soughtID)
paths = waiter.dfsPaths
2019-05-08 01:24:04 +02:00
paths.sort(key=len)
2019-05-19 22:46:37 +02:00
print("Wszystkie sciezki dfs: ", paths)
2019-05-20 00:05:16 +02:00
if paths:
bestPath = paths[0]
waiter.dfsPaths = []
waiter.followThePath(bestPath)
2019-03-19 23:36:06 +01:00
2019-05-20 00:05:16 +02:00
# ZNAJDUJE NAJKRÓTSZĄ ŚCIEŻKĘ BFS I PRZECHODZI WEDŁUG LISTY KROKÓW
2019-05-15 11:35:42 +02:00
def bfs(self, waiter, soughtID, operation=[]):
2019-05-19 22:46:37 +02:00
waiter.bfsFind([waiter.positionX, waiter.positionY], operation, [], soughtID)
2019-05-15 11:35:42 +02:00
paths = waiter.bfsPaths
paths.sort(key=len)
2019-05-19 22:46:37 +02:00
print("Wszystkie sciezki bfs: ", paths)
2019-05-20 00:05:16 +02:00
if paths:
bestPath = paths[0]
savedPaths = open("savedPaths.txt", "a+")
print(str(bestPath))
savedPaths.write(str(bestPath) + "\n")
2019-05-20 00:05:16 +02:00
waiter.bfsPaths = []
2019-06-05 01:09:16 +02:00
waiter.bfsQueue = []
2019-05-20 00:05:16 +02:00
waiter.followThePath(bestPath)
savedPaths.close()
2019-03-19 23:36:06 +01:00
if __name__ == "__main__":
2019-05-15 11:35:42 +02:00
v1 = Game()