fixed bug related to agent's starting position. reworked map generating method
This commit is contained in:
parent
bf5e88a667
commit
b08ecef061
@ -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()
|
||||||
|
|
||||||
@ -20,58 +32,69 @@ class JsonGenerator:
|
|||||||
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
|
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):
|
||||||
|
Loading…
Reference in New Issue
Block a user