From f73dd1f68fc1371b496ed6eeef5bf3a41a625b47 Mon Sep 17 00:00:00 2001 From: JakubR Date: Fri, 12 Mar 2021 20:48:16 +0100 Subject: [PATCH] added first version of json generating class responsible for data serialization, also added some new constants and a new constant type (STRUCT) --- json_generator.py | 126 +++++++++++++++++++++++++++++++++++++++++++ project_constants.py | 8 +++ 2 files changed, 134 insertions(+) create mode 100644 json_generator.py diff --git a/json_generator.py b/json_generator.py new file mode 100644 index 0000000..9ae882b --- /dev/null +++ b/json_generator.py @@ -0,0 +1,126 @@ +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 + row, column = dimensions + + for i in range(row): + for j in range(column): + # 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.grid[str(i) + ',' + str(j)] = { + "color": random_tile_color, + "mine": None + } + else: + # creating a tile with a mine + self.grid[str(i) + ',' + str(j)] = { + "color": random_tile_color, + "mine": { + "mine_type": random_mine_type + } + } + + # adds a new tile or edits an existing one to 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, + "mine": None + } + + # adds a new tile with a mine or edits an existing one to the grid field + 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)) + + # adds a mine to a tile in the grid field + 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() + # resets agents starting position + 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): + # opens file for reading + with open(file_path, 'r') as input_file: + # overwrites the grid field for the grid stored in a file + self.grid = json.load(input_file) + + # saves the current grid field to a file + def save_to_a_file(self, file_path, access_mode): + # opens file with a given access mode (w - write / a - append) + with open(file_path, access_mode) as output_file: + # saves the grid to a file + 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): + # open a file for reading + with open(file_path, "r") as input_file: + # loads data that was stored in the file previously + previous_data = json.load(input_file) + # creates and updates a new grid using it's own grid field + new_grid = previous_data + new_grid.update(self.grid) + # opens the file for writing + with open(file_path, "w") as output_file: + # saves the newly created grid + json.dump(new_grid, output_file, indent=2, sort_keys=True) diff --git a/project_constants.py b/project_constants.py index af617e6..032af30 100644 --- a/project_constants.py +++ b/project_constants.py @@ -4,6 +4,7 @@ import os # VARIABLE STARTS WITH ... IF IT'S # V a value like a string or an int +# STRUCT a list or other structure of values # ASSET a png file (or other graphic format) @@ -30,6 +31,13 @@ SCREEN = pygame.display.set_mode\ ) +# =============== # +# === STRUCTS === # +# =============== # + +STRUCT_TILE_COLORS = ["blue", "green", "orange", "purple", "red", "white", "yellow"] +STRUCT_MINE_TYPES = ['a', 'b', 'f', 'k'] + # ============== # # === ASSETS === #