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