From 502dc62700913dab2f36d773610f4e53e55b006b Mon Sep 17 00:00:00 2001 From: Jakub Radowicz Date: Sun, 28 Mar 2021 19:51:53 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Usu=C5=84=20'resources/minefields/there's?= =?UTF-8?q?=5Fa=5Ffile=5Fhere=5F'cause=5FI=5Fcannot=5Fcommit=5Fan=5Fempty?= =?UTF-8?q?=5Fdirectory'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../there's_a_file_here_'cause_I_cannot_commit_an_empty_directory | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 resources/minefields/there's_a_file_here_'cause_I_cannot_commit_an_empty_directory diff --git a/resources/minefields/there's_a_file_here_'cause_I_cannot_commit_an_empty_directory b/resources/minefields/there's_a_file_here_'cause_I_cannot_commit_an_empty_directory deleted file mode 100644 index e69de29..0000000 From acb231f049d9db3e316831c1a88fb867b3e3e5d3 Mon Sep 17 00:00:00 2001 From: JakubR Date: Wed, 31 Mar 2021 21:56:53 +0200 Subject: [PATCH 2/4] added JsonGenerator method for setting agent's starting position, also minor code refactoring --- json_generator.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/json_generator.py b/json_generator.py index 0c15d62..7904030 100644 --- a/json_generator.py +++ b/json_generator.py @@ -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 From b08ecef061034d7d72e70d4e429be5aa4fde8726 Mon Sep 17 00:00:00 2001 From: JakubR Date: Sat, 3 Apr 2021 23:00:46 +0200 Subject: [PATCH 3/4] fixed bug related to agent's starting position. reworked map generating method --- json_generator.py | 85 ++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 31 deletions(-) diff --git a/json_generator.py b/json_generator.py index 7904030..5779a8e 100644 --- a/json_generator.py +++ b/json_generator.py @@ -3,6 +3,18 @@ import random import project_constants as const +# auxiliary function that returns random attribute values based on their type +def _get_random_attribute_values(self, attr_type, grid_dimensions): + num_of_rows, num_of_columns = grid_dimensions + + if attr_type == int: + # temporary solution + return random.randint(num_of_rows + num_of_columns, (2 * num_of_rows + num_of_columns)) + + else: + pass + + class JsonGenerator: grid = dict() @@ -20,58 +32,69 @@ class JsonGenerator: 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): + def generate_randomized_grid(self, dimensions, mine_appearance_chance=0.25, predecessor_chance_decrease=0.25): # clearing grid field self.clear_tile_dictionary() # getting grid dimensions num_of_rows, num_of_columns = dimensions + tile_pool = [] + 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) - # randomly checking if a mine should appear - if random.random() < 0.75: - # creating a tile without a mine - self.add_tile((i, j), random_tile_color) + # adding added tile's index to a pool + tile_pool.append((i, j)) - else: - # picking random mine attribute values + # adding the random tile + self.add_tile((i, j), random_tile_color) + + # deleting tile agent's starting tile from the set + deleted_row, deleted_column = self.grid["agent_starting_position"].split(',') + tile_pool.remove((int(deleted_row), int(deleted_column))) + + for i in range(num_of_rows): + for j in range(num_of_columns): + + # checking if a mine will appear + if random.random() < mine_appearance_chance and len(tile_pool) > 0 and tile_pool.__contains__((i, j)): + # removing current tile from the set + tile_pool.remove((i, j)) + + # choosing random mine parameters + random_mine_type = random.choice(const.STRUCT_MINE_TYPES) random_attribute_values = [] for attr_type in const.STRUCT_MINE_ATTRIBUTE_TYPES[random_mine_type]: + random_attribute_values.append(_get_random_attribute_values(self, attr_type, dimensions)) - if attr_type == int: - min_value, max_value = (num_of_columns + num_of_rows) / 2, 2 *(num_of_columns + num_of_rows) - random_value = random.randint(min_value, max_value) - random_attribute_values.append(random_value) + # adding the mine + self.set_a_mine((i, j), random_mine_type, random_attribute_values) - elif attr_type == (int, int): + # if is ChainedMine create predecessors + if random_mine_type == "chained": + predecessor_appearance_chance = 1.0 + current_tile = str(i) + ',' + str(j) - if i > 0: - rand_i = random.randint(0, (i - 1)) - rand_j = random.randint(0, (num_of_columns - 1)) - random_attribute_values.append(str(rand_i) + ',' + str(rand_j)) - self.set_a_mine((rand_i, rand_j), "chained", [None]) + # create chained predecessors + while random.random() < predecessor_appearance_chance and len(tile_pool) > 0: + predecessor_appearance_chance -= predecessor_chance_decrease - elif j > 0: - rand_i = 0 - rand_j = random.randint(0, (j - 1)) - random_attribute_values.append(str(rand_i) + ',' + str(rand_j)) - self.set_a_mine((rand_i, rand_j), "chained", [None]) + predecessor_position = random.choice(tile_pool) + pre_row, pre_column = predecessor_position + predecessor = str(pre_row) + ',' + str(pre_column) + tile_pool.remove(predecessor_position) - else: - random_attribute_values.append(None) + self.set_a_mine(predecessor_position, "chained", []) - # creating a tile with a mine - self.add_tile_with_a_mine( - (i, j), - random_tile_color, - random_mine_type, - random_attribute_values) + self.grid[current_tile]["mine"]["predecessor"] = predecessor + self.grid[predecessor]["mine"]["predecessor"] = None + + current_tile = predecessor # adds a new tile or edits an existing one in the grid field def add_tile(self, position, color): From 0af9fa4b30e4fe2d246e027a7bd5aaef6ec4ae8a Mon Sep 17 00:00:00 2001 From: JakubR Date: Sat, 3 Apr 2021 23:10:13 +0200 Subject: [PATCH 4/4] deleted invalid field in STRUCT_MINE_ATTRIBUTE_TYPES from project constants --- project_constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_constants.py b/project_constants.py index 5cb202a..c624a83 100644 --- a/project_constants.py +++ b/project_constants.py @@ -73,7 +73,7 @@ STRUCT_MINE_ATTRIBUTES = { # (int, int) - index "row,column" where row=int and column=int (used exclusively for chained mine) STRUCT_MINE_ATTRIBUTE_TYPES = { "standard": [], - "chained": [(int, int)], + "chained": [], "time": [int] }