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
# 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()
@ -11,64 +23,84 @@ 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):
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):
# 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 +159,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 +169,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 +196,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

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)
STRUCT_MINE_ATTRIBUTE_TYPES = {
"standard": [],
"chained": [(int, int)],
"chained": [],
"time": [int]
}