From 7181bb133ac58277450dab6faa77f4b2b82ec04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Starski?= Date: Tue, 21 May 2019 11:53:29 +0200 Subject: [PATCH] implemented dumping moveset to the file --- .gitignore | 1 + DataModels/GC.py | 3 ++- utilities.py | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 169ffb9..41bb08d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ env .vscode *.swp linux_env +Results/* diff --git a/DataModels/GC.py b/DataModels/GC.py index 6e60278..c934e16 100644 --- a/DataModels/GC.py +++ b/DataModels/GC.py @@ -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 -from utilities import movement, check_moves +from utilities import movement, check_moves, save_moveset from Traversal.DFS import DFS from Traversal.BestFS import BestFS from Traversal.BFS import BFS @@ -66,6 +66,7 @@ class GC(Cell): for x in element_list: x.unvisited = True self.moves.reverse() + save_moveset(self.moves) def find_houses_BestFS(self, environment): diff --git a/utilities.py b/utilities.py index 410f34c..d55f205 100644 --- a/utilities.py +++ b/utilities.py @@ -1,5 +1,7 @@ from config import GRID_WIDTH, GRID_HEIGHT from DataModels.Road import Road +import json, os + 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), @@ -21,3 +23,18 @@ def check_moves(environment, x,y,direction=None): if direction == None: return ([dir for dir in movement(environment, x, y)[0] if movement(environment, x,y)[0][dir] != (x,y)]) return ([dir for dir in movement(environment, x, y)[0] if movement(environment, x,y)[0][dir] != (x,y) and dir != movement(environment,x,y)[1][direction]]) + +def save_moveset(moveset): + output_file = os.path.normpath(os.getcwd()) + '/Results/moveset_data.json' + with open(output_file, 'w') as f: + try: + results = json.loads(f) + except: + results = { + "moveset": [] + } + finally: + results["moveset"].append(moveset) + json.dump(results, f, indent=2) + print('Moveset saved to the file /Results/moveset_data.json') +