2021-03-12 20:48:16 +01:00
|
|
|
import json
|
|
|
|
import random
|
|
|
|
import project_constants as const
|
|
|
|
|
|
|
|
|
|
|
|
class JsonGenerator:
|
|
|
|
grid = dict()
|
|
|
|
|
|
|
|
def __init__(self, agent_starting_position):
|
|
|
|
self.agent_starting_position = agent_starting_position
|
|
|
|
starting_row, starting_column = agent_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):
|
|
|
|
# clearing grid field
|
|
|
|
self.clear_tile_dictionary()
|
|
|
|
|
|
|
|
# getting grid dimensions
|
2021-03-14 00:59:00 +01:00
|
|
|
num_of_rows, num_of_columns = dimensions
|
2021-03-12 20:48:16 +01:00
|
|
|
|
2021-03-14 00:59:00 +01:00
|
|
|
for i in range(num_of_rows):
|
|
|
|
for j in range(num_of_columns):
|
2021-03-12 20:48:16 +01:00
|
|
|
# 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
|
2021-03-14 00:59:00 +01:00
|
|
|
self.add_tile((i, j), random_tile_color)
|
2021-03-12 20:48:16 +01:00
|
|
|
else:
|
|
|
|
# creating a tile with a mine
|
2021-03-14 00:59:00 +01:00
|
|
|
self.add_tile_with_a_mine((i, j), random_tile_color, random_mine_type)
|
|
|
|
|
|
|
|
# adds a new tile or edits an existing one in the grid field
|
2021-03-12 20:48:16 +01:00
|
|
|
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,
|
|
|
|
"mine": None
|
|
|
|
}
|
|
|
|
|
2021-03-14 00:59:00 +01:00
|
|
|
# adds a new tile with a mine or edits an existing one in the grid field
|
2021-03-12 20:48:16 +01:00
|
|
|
def add_tile_with_a_mine(self, position, color, mine_type):
|
|
|
|
# getting added/edited tile's index values
|
|
|
|
row, column = position
|
|
|
|
# creating a new tile with a mine
|
|
|
|
self.grid[str(row) + ',' + str(column)] = {
|
|
|
|
"color": color,
|
|
|
|
"mine": {
|
|
|
|
"mine_type": mine_type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
2021-03-14 00:59:00 +01:00
|
|
|
# adds a mine to a tile stored in the grid field
|
2021-03-12 20:48:16 +01:00
|
|
|
def add_a_mine(self, position, mine_type):
|
|
|
|
# getting edited tile's index values
|
|
|
|
row, column = position
|
|
|
|
# adding a mine to the edited tile
|
|
|
|
self.grid[str(row) + ',' + str(column)]["mine"] = {
|
|
|
|
"mine_type": mine_type
|
|
|
|
}
|
|
|
|
|
|
|
|
# deletes a mine from a tile stored in the grid field
|
|
|
|
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
|
|
|
|
|
|
|
|
# returns the grid field
|
|
|
|
def get_tile_dictionary(self):
|
|
|
|
return self.grid
|
|
|
|
|
|
|
|
# clears the grid field
|
|
|
|
def clear_tile_dictionary(self):
|
|
|
|
self.grid.clear()
|
2021-03-14 00:59:00 +01:00
|
|
|
# resetting agents starting position
|
2021-03-12 20:48:16 +01:00
|
|
|
starting_row, starting_column = self.agent_starting_position
|
|
|
|
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
|
|
|
|
|
|
|
|
# loads a grid from a file and overwrites the grid field
|
|
|
|
def load_from_a_file(self, file_path):
|
2021-03-14 00:59:00 +01:00
|
|
|
# opening a file for reading
|
2021-03-12 20:48:16 +01:00
|
|
|
with open(file_path, 'r') as input_file:
|
2021-03-14 00:59:00 +01:00
|
|
|
# overwriting the grid field with the grid stored in a file
|
2021-03-12 20:48:16 +01:00
|
|
|
self.grid = json.load(input_file)
|
|
|
|
|
|
|
|
# saves the current grid field to a file
|
|
|
|
def save_to_a_file(self, file_path, access_mode):
|
2021-03-14 00:59:00 +01:00
|
|
|
# opening a file with a given access mode (w - write / a - append)
|
2021-03-12 20:48:16 +01:00
|
|
|
with open(file_path, access_mode) as output_file:
|
2021-03-14 00:59:00 +01:00
|
|
|
# saving the grid to the file
|
2021-03-12 20:48:16 +01:00
|
|
|
json.dump(self.grid, output_file, indent=2, sort_keys=True)
|
|
|
|
|
|
|
|
# edits a grid in a file. doesn't delete data, only overwrites and adds new entries
|
|
|
|
def edit_a_file(self, file_path):
|
2021-03-14 00:59:00 +01:00
|
|
|
# opening a file for reading
|
2021-03-12 20:48:16 +01:00
|
|
|
with open(file_path, "r") as input_file:
|
2021-03-14 00:59:00 +01:00
|
|
|
# loading data that was stored in the file previously
|
2021-03-12 20:48:16 +01:00
|
|
|
previous_data = json.load(input_file)
|
2021-03-14 00:59:00 +01:00
|
|
|
# creating and updating a new grid using it's own grid field
|
2021-03-12 20:48:16 +01:00
|
|
|
new_grid = previous_data
|
|
|
|
new_grid.update(self.grid)
|
2021-03-14 00:59:00 +01:00
|
|
|
# opening the file for writing
|
2021-03-12 20:48:16 +01:00
|
|
|
with open(file_path, "w") as output_file:
|
2021-03-14 00:59:00 +01:00
|
|
|
# saving the newly created grid
|
2021-03-12 20:48:16 +01:00
|
|
|
json.dump(new_grid, output_file, indent=2, sort_keys=True)
|