SZI2019SmieciarzWmi/DataModels/GC.py

138 lines
4.9 KiB
Python
Raw Normal View History

2019-04-01 13:29:13 +02:00
from DataModels.Cell import Cell
from DataModels.Road import Road
2019-04-01 15:45:48 +02:00
from DataModels.House import House
from DataModels.Dump import Dump
2019-05-30 19:37:26 +02:00
from config import GRID_WIDTH, GRID_HEIGHT, DELAY, CLOSE_ON_END
2019-06-01 21:46:02 +02:00
from utilities import movement, check_moves, save_moveset
2019-06-04 23:16:42 +02:00
from vowpal_utils import parse_list, MAP_CONTENT
from Traversal.DFS import DFS
2019-05-13 10:29:34 +02:00
from Traversal.BestFS import BestFS
2019-05-13 13:16:36 +02:00
from Traversal.BFS import BFS
2019-05-30 19:37:26 +02:00
import pygame, sys
2019-05-22 10:39:56 +02:00
class GC(Cell):
2019-05-12 10:24:43 +02:00
moves_made = 0
2019-05-30 20:23:56 +02:00
algorithm_run = False
def __init__(self, x, y, max_rubbish, yellow=0, green=0, blue=0):
Cell.__init__(self, x, y, max_rubbish, yellow, green, blue)
self.moves = []
self.old_time = pygame.time.get_ticks()
2019-05-30 21:32:56 +02:00
2019-04-10 11:01:20 +02:00
def move(self, direction, environment):
self.x, self.y = movement(environment, self.x, self.y)[0][direction]
2019-04-01 14:36:27 +02:00
self.update_rect(self.x, self.y)
2019-05-12 19:38:11 +02:00
self.moves_made = self.moves_made + 1 #moves counter
2019-04-01 15:45:48 +02:00
def collect(self, enviromnent):
x, y = [self.x, self.y]
coordinates = [(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)]
for coordinate in coordinates:
if coordinate[0]<0 or coordinate[1]<0:
continue
2019-04-01 15:45:48 +02:00
try:
item = enviromnent[coordinate[0]][coordinate[1]]
except:
continue
if(type(item) == House or type(item) == Dump):
2019-04-01 15:45:48 +02:00
item.return_trash(self)
self.update_image()
2019-05-12 10:24:43 +02:00
2019-05-13 10:29:34 +02:00
def get_moves_count(self):
2019-05-12 19:33:54 +02:00
return self.moves_made
2019-05-13 13:16:36 +02:00
def find_houses(self,enviromnent, house_count,dump_count, mode):
2019-05-30 20:23:56 +02:00
self.algorithm_run = True
2019-04-23 21:45:16 +02:00
x = self.x
y = self.y
result = []
2019-06-04 23:16:42 +02:00
whole_result=[]
element_list=[]
house_count_after_search=house_count
2019-04-23 21:45:16 +02:00
for home in range(house_count):
2019-06-04 23:16:42 +02:00
last_x = x
last_y = y
2019-04-23 21:45:16 +02:00
avalible_moves = check_moves(enviromnent, x,y)
2019-05-13 13:16:36 +02:00
if mode == "DFS":
house,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House)
2019-05-13 13:16:36 +02:00
elif mode == "BFS":
house,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House)
2019-06-04 23:16:42 +02:00
self.moves.extend(parse_list(result[1:], last_x,last_y))
element_list.append(house)
2019-06-04 23:16:42 +02:00
MAP_CONTENT[house.y][house.x] = 'V'
2019-05-13 09:45:09 +02:00
for dump in range(dump_count):
2019-06-04 23:16:42 +02:00
last_x = x
last_y = y
2019-05-13 09:45:09 +02:00
avalible_moves = check_moves(enviromnent, x,y)
2019-05-13 13:16:36 +02:00
if mode == "DFS":
dump,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump)
2019-05-13 13:16:36 +02:00
elif mode == "BFS":
dump,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],Dump)
2019-06-04 23:16:42 +02:00
self.moves.extend(parse_list(result[1:], last_x,last_y))
element_list.append(dump)
for x in element_list:
x.unvisited = True
2019-06-04 23:16:42 +02:00
self.moves.reverse()
save_moveset(self.moves)
def find_houses_BestFS(self, environment):
2019-05-30 20:23:56 +02:00
self.algorithm_run = True
x = self.x
y = self.y
result = [[x,y]]
2019-05-30 21:32:56 +02:00
whole_result=[]
houses_list = []
dump_list = []
a = 0
for row in environment:
b = 0
2019-05-30 21:32:56 +02:00
for col in row:
if (type(col) is House):
houses_list.append([col,[a,b]])
2019-05-30 21:32:56 +02:00
if (type(col) is Dump):
dump_list.append([col,[a,b]])
b += 1
a += 1
2019-05-30 21:32:56 +02:00
x, y = self.x, self.y
2019-06-04 23:16:42 +02:00
print(MAP_CONTENT)
for i in range(len(houses_list)):
2019-06-04 23:16:42 +02:00
last_x = x
last_y = y
available_movement = check_moves(environment, x, y)
2019-05-14 21:32:56 +02:00
output = BestFS(environment, available_movement, [[x,y]], houses_list)
if(output != None):
2019-06-04 23:16:42 +02:00
[x,y],result,houses_list,house = output[0], output[1], output[2], output[3]
MAP_CONTENT[house.y][house.x] = 'V'
self.moves.extend(parse_list(result[1:], last_x,last_y))
2019-05-14 23:29:19 +02:00
for i in range(len(dump_list)):
2019-06-04 23:16:42 +02:00
last_x = x
last_y = y
2019-05-14 23:29:19 +02:00
available_movement = check_moves(environment, x, y)
output = BestFS(environment, available_movement, [[x,y]], dump_list)
if(output != None):
[x,y],result,dump_list = output[0], output[1], output[2]
2019-06-04 23:16:42 +02:00
self.moves.extend(parse_list(result[1:], last_x,last_y))
self.moves.reverse()
2019-06-04 23:16:42 +02:00
print(MAP_CONTENT)
2019-05-22 10:39:56 +02:00
save_moveset(self.moves)
2019-05-13 09:45:09 +02:00
def make_actions_from_list(self,environment):
now = pygame.time.get_ticks()
if len(self.moves)==0 or now - self.old_time <= DELAY:
2019-05-30 20:23:56 +02:00
if(len(self.moves)==0 and CLOSE_ON_END=="true" and self.algorithm_run):
2019-05-30 19:37:26 +02:00
print("DONE")
sys.exit()
return
2019-05-30 21:32:56 +02:00
self.old_time = pygame.time.get_ticks()
if self.moves[-1] == "pick_garbage":
self.collect(environment)
self.moves.pop()
return
direction = self.moves.pop()
self.move(direction,environment)