changed JsonGenerator definition to support new mine types

This commit is contained in:
JakubR 2021-03-27 18:25:26 +01:00
parent c3a26d02e6
commit c020d2277a

View File

@ -29,9 +29,41 @@ class JsonGenerator:
if random.random() < 0.75:
# creating a tile without a mine
self.add_tile((i, j), random_tile_color)
else:
# picking random mine attribute values
random_attribute_values = []
for attr_type in const.STRUCT_MINE_ATTRIBUTE_TYPES[random_mine_type]:
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)
elif attr_type == (int, int):
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])
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])
else:
random_attribute_values.append(None)
# creating a tile with a mine
self.add_tile_with_a_mine((i, j), random_tile_color, random_mine_type)
self.add_tile_with_a_mine(
(i, j),
random_tile_color,
random_mine_type,
random_attribute_values)
# adds a new tile or edits an existing one in the grid field
def add_tile(self, position, color):
@ -44,32 +76,52 @@ class JsonGenerator:
}
# 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):
def add_tile_with_a_mine(self, position, color, mine_type, attribute_values):
# getting added/edited tile's index values
row, column = position
# creating a new tile with a mine
# setting mine data using attribute_values
mine_values = const.STRUCT_MINE_ATTRIBUTES[mine_type]
for key in mine_values.keys():
if key not in const.HARDCODED_VALUES and len(attribute_values) > 0:
mine_values[key] = attribute_values.pop(0)
# creating a new tile
self.grid[str(row) + ',' + str(column)] = {
"color": color,
"mine": {
"mine_type": mine_type
}
"color": color
}
# updating the tile with a mine field
self.grid[str(row) + ',' + str(column)]["mine"] = {}
for key in mine_values.keys():
self.grid[str(row) + ',' + str(column)]["mine"][key] = mine_values[key]
# deletes a mine with a given position from the grid field
def delete_a_tile(self, position):
# getting tile's index values
row, column = position
# deleting a tile with given key
self.grid.pop(str(row) + ',' + str(column))
# adds a mine to a tile stored in the grid field
def add_a_mine(self, position, mine_type):
def set_a_mine(self, position, mine_type, attribute_values):
# getting edited tile's index values
row, column = position
# setting mine data using attribute_values
mine_values = const.STRUCT_MINE_ATTRIBUTES[mine_type]
for key in mine_values.keys():
if key not in const.HARDCODED_VALUES and len(attribute_values) > 0:
mine_values[key] = attribute_values.pop(0)
# adding a mine to the edited tile
self.grid[str(row) + ',' + str(column)]["mine"] = {
"mine_type": mine_type
}
self.grid[str(row) + ',' + str(column)]["mine"] = {}
for key in mine_values.keys():
self.grid[str(row) + ',' + str(column)]["mine"][key] = mine_values[key]
# deletes a mine from a tile stored in the grid field
def delete_a_mine(self, position):