From 913d58c5178efb0c2c9ef1a0a3c5dffb9b9af389 Mon Sep 17 00:00:00 2001 From: JakubR Date: Sun, 14 Mar 2021 00:59:00 +0100 Subject: [PATCH] edited some constant values and did some minor code refactoring --- json_generator.py | 44 ++++++++++++++++++-------------------------- project_constants.py | 4 ++-- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/json_generator.py b/json_generator.py index 9ae882b..09a1f4a 100644 --- a/json_generator.py +++ b/json_generator.py @@ -17,10 +17,10 @@ class JsonGenerator: self.clear_tile_dictionary() # getting grid dimensions - row, column = dimensions + num_of_rows, num_of_columns = dimensions - for i in range(row): - for j in range(column): + for i in range(num_of_rows): + for j in range(num_of_columns): # picking random values for tiles random_tile_color = random.choice(const.STRUCT_TILE_COLORS) random_mine_type = random.choice(const.STRUCT_MINE_TYPES) @@ -28,20 +28,12 @@ class JsonGenerator: # randomly checking if a mine should appear if random.random() < 0.75: # creating a tile without a mine - self.grid[str(i) + ',' + str(j)] = { - "color": random_tile_color, - "mine": None - } + self.add_tile((i, j), random_tile_color) else: # creating a tile with a mine - self.grid[str(i) + ',' + str(j)] = { - "color": random_tile_color, - "mine": { - "mine_type": random_mine_type - } - } + self.add_tile_with_a_mine((i, j), random_tile_color, random_mine_type) - # adds a new tile or edits an existing one to the grid field + # adds a new tile or edits an existing one in the grid field def add_tile(self, position, color): # getting added/edited tile's index values row, column = position @@ -51,7 +43,7 @@ class JsonGenerator: "mine": None } - # adds a new tile with a mine or edits an existing one to the grid field + # adds a new tile with a mine or edits an existing one in the grid field def add_tile_with_a_mine(self, position, color, mine_type): # getting added/edited tile's index values row, column = position @@ -70,7 +62,7 @@ class JsonGenerator: # deleting a tile with given key self.grid.pop(str(row) + ',' + str(column)) - # adds a mine to a tile in the grid field + # adds a mine to a tile stored in the grid field def add_a_mine(self, position, mine_type): # getting edited tile's index values row, column = position @@ -93,34 +85,34 @@ class JsonGenerator: # clears the grid field def clear_tile_dictionary(self): self.grid.clear() - # resets agents starting position + # resetting agents starting position starting_row, starting_column = self.agent_starting_position self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column) # loads a grid from a file and overwrites the grid field def load_from_a_file(self, file_path): - # opens file for reading + # opening a file for reading with open(file_path, 'r') as input_file: - # overwrites the grid field for the grid stored in a file + # overwriting the grid field with the grid stored in a file self.grid = json.load(input_file) # saves the current grid field to a file def save_to_a_file(self, file_path, access_mode): - # opens file with a given access mode (w - write / a - append) + # opening a file with a given access mode (w - write / a - append) with open(file_path, access_mode) as output_file: - # saves the grid to a file + # saving the grid to the file json.dump(self.grid, output_file, indent=2, sort_keys=True) # edits a grid in a file. doesn't delete data, only overwrites and adds new entries def edit_a_file(self, file_path): - # open a file for reading + # opening a file for reading with open(file_path, "r") as input_file: - # loads data that was stored in the file previously + # loading data that was stored in the file previously previous_data = json.load(input_file) - # creates and updates 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.update(self.grid) - # opens the file for writing + # opening the file for writing with open(file_path, "w") as output_file: - # saves the newly created grid + # saving the newly created grid json.dump(new_grid, output_file, indent=2, sort_keys=True) diff --git a/project_constants.py b/project_constants.py index daaf234..072743a 100644 --- a/project_constants.py +++ b/project_constants.py @@ -36,8 +36,8 @@ SCREEN = pygame.display.set_mode\ # === STRUCTS === # # =============== # -STRUCT_TILE_COLORS = ["blue", "green", "orange", "purple", "red", "white", "yellow"] -STRUCT_MINE_TYPES = ['a', 'b', 'f', 'k'] +STRUCT_TILE_COLORS = ["BLUE", "GREEN", "ORANGE", "PURPLE", "RED", "WHITE", "YELLOW"] +STRUCT_MINE_TYPES = ['A', 'B', 'F', 'K'] # ============== # # ==== MAPS ==== #