Dodano parser

This commit is contained in:
Anna Nowak 2019-05-30 21:32:56 +02:00
parent 635292d9f5
commit 42d124cc6e
3 changed files with 46 additions and 30 deletions

View File

@ -3,7 +3,7 @@ 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 utilities import movement, check_moves, save_moveset,parse_list
from Traversal.DFS import DFS
from Traversal.BestFS import BestFS
from Traversal.BFS import BFS
@ -15,13 +15,12 @@ class GC(Cell):
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
print(check_moves(environment, self.x, self.y,direction))
def collect(self, enviromnent):
x, y = [self.x, self.y]
coordinates = [(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)]
@ -43,6 +42,7 @@ class GC(Cell):
def find_houses(self,enviromnent, house_count,dump_count, mode):
x = self.x
y = self.y
whole_result=[]
result = []
element_list=[]
house_count_after_search=house_count
@ -53,7 +53,7 @@ class GC(Cell):
elif mode == "BFS":
house,[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House)
result = result[1::]
self.moves.extend(result)
whole_result.extend(result)
element_list.append(house)
for dump in range(dump_count):
@ -62,11 +62,11 @@ class GC(Cell):
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(result)
whole_result.extend(result)
element_list.append(dump)
for x in element_list:
x.unvisited = True
self.moves.reverse()
self.moves.extend(parse_list(whole_result))
save_moveset(self.moves)
@ -74,34 +74,35 @@ class GC(Cell):
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):
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]])
if (type(col) is Dump):
dump_list.append([col,[a,b]])
b += 1
a += 1
x, y = self.x, self.y
for i in range(len(houses_list)):
available_movement = check_moves(environment, x, y)
output = BestFS(environment, available_movement, [[x,y]], houses_list)
if(output != None):
[x,y],result,houses_list = output[0], output[1], output[2]
self.moves.extend(result[1:])
whole_result.extend(result[1:])
for i in range(len(dump_list)):
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(result[1:])
whole_result.extend(result[1:])
self.moves.extend(parse_list(whole_result,self.x,self.y))
self.moves.reverse()
save_moveset(self.moves)
@ -109,15 +110,15 @@ class GC(Cell):
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):
print("DONE")
sys.exit()
# if(len(self.moves)==0 and CLOSE_ON_END):
# 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
self.x, self.y = self.moves.pop()
self.moves_made = self.moves_made + 1 #moves counter
self.update_rect(self.x,self.y)
self.move(self.moves[-1],environment)
self.moves.pop()

View File

@ -2,7 +2,7 @@ import sys, random, MapGenerator
CELL_SIZE = 64
FPS = 60
DELAY = 50
DELAY = 500
try:
map_mode = sys.argv[1]

View File

@ -1,7 +1,6 @@
from config import GRID_WIDTH, GRID_HEIGHT
from DataModels.Road import Road
import json, os, platform
import platform,os,json
def movement(environment, x ,y):
movement = {
"right": (x + 1, y) if x + 1 < GRID_WIDTH and type(environment[x + 1][y]) == Road else (x, y),
@ -30,7 +29,7 @@ def save_moveset(moveset):
else:
path = '/moveset_data.json'
output_file = os.path.normpath(os.getcwd()) + path
results = {}
try:
f = open(output_file, 'r+')
@ -38,16 +37,32 @@ def save_moveset(moveset):
open(output_file, 'a').close()
finally:
f = open(output_file, 'r+')
try:
results = json.load(f)
except:
pass
finally:
finally:
if "moveset" not in results:
results = { "moveset": [] }
results["moveset"].append(moveset)
f.seek(0)
json.dump(results, f, indent=1)
f.close()
f.close()
def parse_list(whole_result,current_x,current_y):
moves = []
print("x,y",current_x,current_y,"list",whole_result)
parser = {'[0,1]':"down",'[0,-1]':"up",'[1,0]':"right",'[-1,0]':"left"}
for x in range(len(whole_result)):
if whole_result[x]=="pick_garbage":
moves.append(whole_result[x])
else:
x_subtraction = whole_result[x][0] - current_x
y_subtraction = whole_result[x][1] - current_y
current_x = whole_result[x][0]
current_y = whole_result[x][1]
moves.append(parser[f"[{x_subtraction},{y_subtraction}]"])
print(moves)
return moves