SZI2019SmieciarzWmi/utilities.py
2019-05-21 11:53:29 +02:00

41 lines
1.5 KiB
Python

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),
"left": (x - 1, y) if x - 1 >= 0 and type(environment[x - 1][y]) == Road else (x, y),
"down": (x, y + 1) if y + 1 < GRID_HEIGHT and type(environment[x][y + 1]) == Road else (x, y),
"up": (x, y - 1) if y - 1 >= 0 and type(environment[x][y - 1]) == Road else (x, y)
}
forbidden_movement = {
"right": "left",
"left": "right",
"up": "down",
"down": "up"
}
return (movement, forbidden_movement)
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')