adjusted JsonGenerator class; added newly generated map; minor code refactoring

This commit is contained in:
JakubR 2021-04-17 23:22:27 +02:00
parent 1d9e45c631
commit be43e8aeb8
4 changed files with 507 additions and 36 deletions

View File

@ -12,29 +12,44 @@ def _get_random_attribute_values(self, attr_type, grid_dimensions):
return random.randint(num_of_rows + num_of_columns, (2 * num_of_rows + num_of_columns))
else:
pass
return None
class JsonGenerator:
grid = dict()
def __init__(self, agent_starting_position, agent_direction):
self.agent_starting_position = agent_starting_position
starting_row, starting_column = agent_starting_position
self.agent_direction = agent_direction
starting_direction = agent_direction
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
# constructor that can be used to set agent's initial state
def __init__(self, agents_initial_position=(0, 0), agents_initial_direction=const.Direction.UP.value):
# saving agent's initial state (position & direction)
self.agents_initial_position = agents_initial_position
self.agents_initial_direction = agents_initial_direction
# getting position coordinates
starting_row, starting_column = agents_initial_position
# saving data to the grid dictionary
self.grid["agents_initial_state"] = {
"position": str(starting_row) + ',' + str(starting_column),
"direction": agents_initial_direction
}
# sets agent's initial state
def set_agents_initial_state(self, position=(0, 0), direction=const.Direction.UP.value):
# setting fields in the instance
self.agents_initial_position = position
self.agents_initial_direction = direction
# 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)
# setting new agent's initial state
self.grid["agents_initial_state"] = {
"position": str(starting_row) + ',' + str(starting_column),
"direction": direction
}
# overwrites grid field with a new grid with randomized colors and mines
def generate_randomized_grid(self, dimensions, mine_appearance_chance=0.25, predecessor_chance_decrease=0.25):
def generate_randomized_grid(self, dimensions, mine_appearance_chance=0.15, predecessor_chance_decrease=0.25):
# clearing grid field
self.clear_tile_dictionary()
@ -45,18 +60,17 @@ class JsonGenerator:
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)
# adding added tile's index to a pool
# adding added tile's indexes to a pool
tile_pool.append((i, j))
# adding the random tile
# creating 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(',')
# deleting agent's starting tile from the pool
deleted_row, deleted_column = self.agents_initial_position
tile_pool.remove((int(deleted_row), int(deleted_column)))
for i in range(num_of_rows):
@ -64,7 +78,7 @@ class JsonGenerator:
# 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
# removing current tile from the pool
tile_pool.remove((i, j))
# choosing random mine parameters
@ -174,9 +188,14 @@ class JsonGenerator:
# 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)
# getting agent's starting position coordinates
starting_row, starting_column = self.agents_initial_position
# re-setting the agent's initial state
self.grid["agents_initial_state"] = {
"position": str(starting_row) + ',' + str(starting_column),
"direction": self.agents_initial_direction
}
# loads a grid from a file and overwrites the grid field
def load_from_a_file(self, file_path):

View File

@ -66,9 +66,5 @@ def main():
# Assigning all input from keyboard as variables into an array
if __name__ == "__main__":
main()

View File

@ -45,6 +45,7 @@ class Direction(Enum):
def next(self):
v = (self.value + 1) % 4
return Direction(v)
def previous(self):
v = (self.value - 1) % 4
return Direction(v)
@ -69,26 +70,22 @@ STRUCT_TILE_COLORS = ["BLUE", "GREEN", "ORANGE", "PURPLE", "RED", "WHITE", "YELL
# used to generate random mines and create not random mines
STRUCT_MINE_TYPES = ["standard", "chained", "time"]
STRUCT_MINE_ASSET_TYPES = ['A', 'B', 'F', 'K']
# values that are predefined for certain mine types and can't be changed by changing any parameters
# put here all dict keys that have hardcoded values and are not supposed to be editable
HARDCODED_VALUES = ["asset", "mine_type"]
HARDCODED_VALUES = ["mine_type"]
# default values and key-value pairs for JsonGenerator class
# when defining a new mine it's dict template must be put here
STRUCT_MINE_ATTRIBUTES = {
"standard": {
"asset": STRUCT_MINE_ASSET_TYPES.__getitem__(0),
"mine_type": "standard"
},
"chained": {
"asset": STRUCT_MINE_ASSET_TYPES.__getitem__(1),
"mine_type": "chained",
"predecessor": None
},
"time": {
"asset": STRUCT_MINE_ASSET_TYPES.__getitem__(2),
"mine_type": "time",
"timer": None
}
@ -97,8 +94,6 @@ STRUCT_MINE_ATTRIBUTES = {
# types of attributes the mines have
# used for random mine generation
# int - integral number
# (int, int) - index "row,column" where row=int and column=int (used exclusively for chained mine)
STRUCT_MINE_ATTRIBUTE_TYPES = {
"standard": [],
"chained": [],
@ -111,7 +106,6 @@ STRUCT_MINE_ATTRIBUTE_TYPES = {
MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "secondmap.json")
# ============== #
# === ASSETS === #
# ============== #

View File

@ -0,0 +1,462 @@
{
"0,0": {
"color": "GREEN",
"mine": null
},
"0,1": {
"color": "RED",
"mine": {
"mine_type": "standard"
}
},
"0,2": {
"color": "RED",
"mine": null
},
"0,3": {
"color": "YELLOW",
"mine": null
},
"0,4": {
"color": "ORANGE",
"mine": null
},
"0,5": {
"color": "GREEN",
"mine": null
},
"0,6": {
"color": "GREEN",
"mine": null
},
"0,7": {
"color": "BLUE",
"mine": null
},
"0,8": {
"color": "ORANGE",
"mine": null
},
"0,9": {
"color": "ORANGE",
"mine": null
},
"1,0": {
"color": "RED",
"mine": null
},
"1,1": {
"color": "YELLOW",
"mine": null
},
"1,2": {
"color": "PURPLE",
"mine": null
},
"1,3": {
"color": "GREEN",
"mine": null
},
"1,4": {
"color": "ORANGE",
"mine": {
"mine_type": "time",
"timer": 30
}
},
"1,5": {
"color": "ORANGE",
"mine": null
},
"1,6": {
"color": "GREEN",
"mine": null
},
"1,7": {
"color": "GREEN",
"mine": null
},
"1,8": {
"color": "BLUE",
"mine": null
},
"1,9": {
"color": "YELLOW",
"mine": null
},
"2,0": {
"color": "GREEN",
"mine": null
},
"2,1": {
"color": "PURPLE",
"mine": null
},
"2,2": {
"color": "BLUE",
"mine": null
},
"2,3": {
"color": "BLUE",
"mine": null
},
"2,4": {
"color": "BLUE",
"mine": {
"mine_type": "chained",
"predecessor": "8,1"
}
},
"2,5": {
"color": "WHITE",
"mine": null
},
"2,6": {
"color": "GREEN",
"mine": null
},
"2,7": {
"color": "GREEN",
"mine": null
},
"2,8": {
"color": "ORANGE",
"mine": null
},
"2,9": {
"color": "RED",
"mine": null
},
"3,0": {
"color": "RED",
"mine": null
},
"3,1": {
"color": "RED",
"mine": null
},
"3,2": {
"color": "WHITE",
"mine": null
},
"3,3": {
"color": "BLUE",
"mine": null
},
"3,4": {
"color": "WHITE",
"mine": null
},
"3,5": {
"color": "YELLOW",
"mine": null
},
"3,6": {
"color": "GREEN",
"mine": null
},
"3,7": {
"color": "RED",
"mine": null
},
"3,8": {
"color": "RED",
"mine": null
},
"3,9": {
"color": "GREEN",
"mine": {
"mine_type": "time",
"timer": 23
}
},
"4,0": {
"color": "PURPLE",
"mine": null
},
"4,1": {
"color": "RED",
"mine": {
"mine_type": "chained",
"predecessor": "5,6"
}
},
"4,2": {
"color": "PURPLE",
"mine": null
},
"4,3": {
"color": "RED",
"mine": null
},
"4,4": {
"color": "RED",
"mine": null
},
"4,5": {
"color": "ORANGE",
"mine": null
},
"4,6": {
"color": "ORANGE",
"mine": null
},
"4,7": {
"color": "RED",
"mine": null
},
"4,8": {
"color": "GREEN",
"mine": {
"mine_type": "time",
"timer": 25
}
},
"4,9": {
"color": "YELLOW",
"mine": null
},
"5,0": {
"color": "YELLOW",
"mine": null
},
"5,1": {
"color": "ORANGE",
"mine": null
},
"5,2": {
"color": "WHITE",
"mine": null
},
"5,3": {
"color": "GREEN",
"mine": null
},
"5,4": {
"color": "GREEN",
"mine": {
"mine_type": "time",
"timer": 21
}
},
"5,5": {
"color": "ORANGE",
"mine": null
},
"5,6": {
"color": "ORANGE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"5,7": {
"color": "YELLOW",
"mine": {
"mine_type": "standard"
}
},
"5,8": {
"color": "YELLOW",
"mine": null
},
"5,9": {
"color": "YELLOW",
"mine": null
},
"6,0": {
"color": "BLUE",
"mine": {
"mine_type": "chained",
"predecessor": "8,2"
}
},
"6,1": {
"color": "YELLOW",
"mine": {
"mine_type": "chained",
"predecessor": "8,7"
}
},
"6,2": {
"color": "ORANGE",
"mine": null
},
"6,3": {
"color": "ORANGE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"6,4": {
"color": "YELLOW",
"mine": null
},
"6,5": {
"color": "RED",
"mine": {
"mine_type": "time",
"timer": 25
}
},
"6,6": {
"color": "BLUE",
"mine": null
},
"6,7": {
"color": "WHITE",
"mine": null
},
"6,8": {
"color": "GREEN",
"mine": {
"mine_type": "time",
"timer": 25
}
},
"6,9": {
"color": "RED",
"mine": null
},
"7,0": {
"color": "PURPLE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"7,1": {
"color": "YELLOW",
"mine": null
},
"7,2": {
"color": "RED",
"mine": null
},
"7,3": {
"color": "WHITE",
"mine": null
},
"7,4": {
"color": "BLUE",
"mine": null
},
"7,5": {
"color": "WHITE",
"mine": null
},
"7,6": {
"color": "YELLOW",
"mine": null
},
"7,7": {
"color": "PURPLE",
"mine": {
"mine_type": "standard"
}
},
"7,8": {
"color": "PURPLE",
"mine": null
},
"7,9": {
"color": "BLUE",
"mine": null
},
"8,0": {
"color": "PURPLE",
"mine": null
},
"8,1": {
"color": "PURPLE",
"mine": {
"mine_type": "chained",
"predecessor": "7,0"
}
},
"8,2": {
"color": "GREEN",
"mine": {
"mine_type": "chained",
"predecessor": "6,3"
}
},
"8,3": {
"color": "RED",
"mine": null
},
"8,4": {
"color": "YELLOW",
"mine": null
},
"8,5": {
"color": "GREEN",
"mine": null
},
"8,6": {
"color": "RED",
"mine": {
"mine_type": "standard"
}
},
"8,7": {
"color": "WHITE",
"mine": {
"mine_type": "chained",
"predecessor": null
}
},
"8,8": {
"color": "RED",
"mine": null
},
"8,9": {
"color": "GREEN",
"mine": null
},
"9,0": {
"color": "ORANGE",
"mine": null
},
"9,1": {
"color": "PURPLE",
"mine": null
},
"9,2": {
"color": "BLUE",
"mine": null
},
"9,3": {
"color": "ORANGE",
"mine": null
},
"9,4": {
"color": "GREEN",
"mine": null
},
"9,5": {
"color": "RED",
"mine": null
},
"9,6": {
"color": "GREEN",
"mine": null
},
"9,7": {
"color": "YELLOW",
"mine": null
},
"9,8": {
"color": "BLUE",
"mine": null
},
"9,9": {
"color": "GREEN",
"mine": null
},
"agents_initial_state": {
"direction": 3,
"position": "1,1"
}
}