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):