138 lines
4.9 KiB
Python
138 lines
4.9 KiB
Python
from DataModels.Cell import Cell
|
|
from DataModels.Road import Road
|
|
from DataModels.House import House
|
|
from DataModels.Dump import Dump
|
|
from config import GRID_WIDTH, GRID_HEIGHT, DELAY, CLOSE_ON_END
|
|
from utilities import movement, check_moves, save_moveset
|
|
from vowpal_utils import parse_list, MAP_CONTENT
|
|
from Traversal.DFS import DFS
|
|
from Traversal.BestFS import BestFS
|
|
from Traversal.BFS import BFS
|
|
import pygame, sys
|
|
|
|
class GC(Cell):
|
|
moves_made = 0
|
|
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()
|
|
|
|
def move(self, direction, environment):
|
|
self.x, self.y = movement(environment, self.x, self.y)[0][direction]
|
|
self.update_rect(self.x, self.y)
|
|
self.moves_made = self.moves_made + 1 #moves counter
|
|
|
|
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
|
|
try:
|
|
item = enviromnent[coordinate[0]][coordinate[1]]
|
|
except:
|
|
continue
|
|
|
|
if(type(item) == House or type(item) == Dump):
|
|
item.return_trash(self)
|
|
self.update_image()
|
|
|
|
def get_moves_count(self):
|
|
return self.moves_made
|
|
|
|
def find_houses(self,enviromnent, house_count,dump_count, mode):
|
|
self.algorithm_run = True
|
|
|
|
x = self.x
|
|
y = self.y
|
|
result = []
|
|
whole_result=[]
|
|
element_list=[]
|
|
house_count_after_search=house_count
|
|
for home in range(house_count):
|
|
last_x = x
|
|
last_y = y
|
|
avalible_moves = check_moves(enviromnent, x,y)
|
|
if mode == "DFS":
|
|
house,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House)
|
|
elif mode == "BFS":
|
|
house,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House)
|
|
self.moves.extend(parse_list(result[1:], last_x,last_y))
|
|
element_list.append(house)
|
|
|
|
MAP_CONTENT[house.y][house.x] = 'V'
|
|
for dump in range(dump_count):
|
|
last_x = x
|
|
last_y = y
|
|
avalible_moves = check_moves(enviromnent, x,y)
|
|
if mode == "DFS":
|
|
dump,[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump)
|
|
elif mode == "BFS":
|
|
dump,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],Dump)
|
|
self.moves.extend(parse_list(result[1:], last_x,last_y))
|
|
element_list.append(dump)
|
|
for x in element_list:
|
|
x.unvisited = True
|
|
self.moves.reverse()
|
|
save_moveset(self.moves)
|
|
|
|
def find_houses_BestFS(self, environment):
|
|
self.algorithm_run = True
|
|
|
|
x = self.x
|
|
y = self.y
|
|
result = [[x,y]]
|
|
whole_result=[]
|
|
houses_list = []
|
|
dump_list = []
|
|
a = 0
|
|
for row in environment:
|
|
b = 0
|
|
for col in row:
|
|
if (type(col) is House):
|
|
houses_list.append([col,[a,b]])
|
|
if (type(col) is Dump):
|
|
dump_list.append([col,[a,b]])
|
|
b += 1
|
|
a += 1
|
|
|
|
x, y = self.x, self.y
|
|
print(MAP_CONTENT)
|
|
for i in range(len(houses_list)):
|
|
last_x = x
|
|
last_y = y
|
|
available_movement = check_moves(environment, x, y)
|
|
output = BestFS(environment, available_movement, [[x,y]], houses_list)
|
|
if(output != None):
|
|
[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))
|
|
for i in range(len(dump_list)):
|
|
last_x = x
|
|
last_y = y
|
|
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]
|
|
self.moves.extend(parse_list(result[1:], last_x,last_y))
|
|
self.moves.reverse()
|
|
print(MAP_CONTENT)
|
|
save_moveset(self.moves)
|
|
|
|
def make_actions_from_list(self,environment):
|
|
now = pygame.time.get_ticks()
|
|
if len(self.moves)==0 or now - self.old_time <= DELAY:
|
|
if(len(self.moves)==0 and CLOSE_ON_END=="true" and self.algorithm_run):
|
|
print("DONE")
|
|
sys.exit()
|
|
return
|
|
|
|
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)
|