added JsonGenerator method for setting agent's starting position, also minor code refactoring

This commit is contained in:
JakubR 2021-03-31 21:56:53 +02:00
parent a90cc61586
commit acb231f049

View File

@ -11,6 +11,14 @@ class JsonGenerator:
starting_row, starting_column = agent_starting_position
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
# sets agent's starting position
def set_agent_starting_position(self, position):
# getting position coordinates
starting_row, starting_column = position
# setting new agent's starting position
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
# overwrites grid field with a new grid with randomized colors and mines
def generate_randomized_grid(self, dimensions):
# clearing grid field
@ -69,6 +77,7 @@ class JsonGenerator:
def add_tile(self, position, color):
# getting added/edited tile's index values
row, column = position
# creating new tile without a mine
self.grid[str(row) + ',' + str(column)] = {
"color": color,
@ -127,6 +136,7 @@ class JsonGenerator:
def delete_a_mine(self, position):
# getting edited tile's index values
row, column = position
# removing mine from the edited tile
self.grid[str(row) + ',' + str(column)]["mine"] = None
@ -136,7 +146,9 @@ class JsonGenerator:
# clears the grid field
def clear_tile_dictionary(self):
# clearing grid dict
self.grid.clear()
# resetting agents starting position
starting_row, starting_column = self.agent_starting_position
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
@ -161,9 +173,11 @@ class JsonGenerator:
with open(file_path, "r") as input_file:
# loading data that was stored in the file previously
previous_data = json.load(input_file)
# creating and updating a new grid using it's own grid field
new_grid = previous_data
new_grid.update(self.grid)
# opening the file for writing
with open(file_path, "w") as output_file:
# saving the newly created grid