Merge remote-tracking branch 'origin/master'

This commit is contained in:
s452645 2021-04-11 19:04:37 +02:00
commit edca418fed
3 changed files with 69 additions and 32 deletions

View File

@ -3,6 +3,18 @@ import random
import project_constants as const 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: class JsonGenerator:
grid = dict() grid = dict()
@ -11,64 +23,84 @@ 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, mine_appearance_chance=0.25, predecessor_chance_decrease=0.25):
# clearing grid field # clearing grid field
self.clear_tile_dictionary() self.clear_tile_dictionary()
# getting grid dimensions # getting grid dimensions
num_of_rows, num_of_columns = dimensions num_of_rows, num_of_columns = dimensions
tile_pool = []
for i in range(num_of_rows): for i in range(num_of_rows):
for j in range(num_of_columns): for j in range(num_of_columns):
# picking random values for tiles # picking random values for tiles
random_tile_color = random.choice(const.STRUCT_TILE_COLORS) 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 # adding added tile's index to a pool
if random.random() < 0.75: tile_pool.append((i, j))
# creating a tile without a mine
# adding the random tile
self.add_tile((i, j), random_tile_color) self.add_tile((i, j), random_tile_color)
else: # deleting tile agent's starting tile from the set
# picking random mine attribute values 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 = [] random_attribute_values = []
for attr_type in const.STRUCT_MINE_ATTRIBUTE_TYPES[random_mine_type]: 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: # adding the mine
min_value, max_value = (num_of_columns + num_of_rows) / 2, 2 *(num_of_columns + num_of_rows) self.set_a_mine((i, j), random_mine_type, random_attribute_values)
random_value = random.randint(min_value, max_value)
random_attribute_values.append(random_value)
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: # create chained predecessors
rand_i = random.randint(0, (i - 1)) while random.random() < predecessor_appearance_chance and len(tile_pool) > 0:
rand_j = random.randint(0, (num_of_columns - 1)) predecessor_appearance_chance -= predecessor_chance_decrease
random_attribute_values.append(str(rand_i) + ',' + str(rand_j))
self.set_a_mine((rand_i, rand_j), "chained", [None])
elif j > 0: predecessor_position = random.choice(tile_pool)
rand_i = 0 pre_row, pre_column = predecessor_position
rand_j = random.randint(0, (j - 1)) predecessor = str(pre_row) + ',' + str(pre_column)
random_attribute_values.append(str(rand_i) + ',' + str(rand_j)) tile_pool.remove(predecessor_position)
self.set_a_mine((rand_i, rand_j), "chained", [None])
else: self.set_a_mine(predecessor_position, "chained", [])
random_attribute_values.append(None)
# creating a tile with a mine self.grid[current_tile]["mine"]["predecessor"] = predecessor
self.add_tile_with_a_mine( self.grid[predecessor]["mine"]["predecessor"] = None
(i, j),
random_tile_color, current_tile = predecessor
random_mine_type,
random_attribute_values)
# adds a new tile or edits an existing one in the grid field # adds a new tile or edits an existing one in the grid field
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 +159,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 +169,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 +196,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

View File

@ -73,7 +73,7 @@ STRUCT_MINE_ATTRIBUTES = {
# (int, int) - index "row,column" where row=int and column=int (used exclusively for chained mine) # (int, int) - index "row,column" where row=int and column=int (used exclusively for chained mine)
STRUCT_MINE_ATTRIBUTE_TYPES = { STRUCT_MINE_ATTRIBUTE_TYPES = {
"standard": [], "standard": [],
"chained": [(int, int)], "chained": [],
"time": [int] "time": [int]
} }