edited some constant values and did some minor code refactoring

This commit is contained in:
JakubR 2021-03-14 00:59:00 +01:00
parent 54d9133751
commit 913d58c517
2 changed files with 20 additions and 28 deletions

View File

@ -17,10 +17,10 @@ class JsonGenerator:
self.clear_tile_dictionary() self.clear_tile_dictionary()
# getting grid dimensions # getting grid dimensions
row, column = dimensions num_of_rows, num_of_columns = dimensions
for i in range(row): for i in range(num_of_rows):
for j in range(column): 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) random_mine_type = random.choice(const.STRUCT_MINE_TYPES)
@ -28,20 +28,12 @@ class JsonGenerator:
# randomly checking if a mine should appear # randomly checking if a mine should appear
if random.random() < 0.75: if random.random() < 0.75:
# creating a tile without a mine # creating a tile without a mine
self.grid[str(i) + ',' + str(j)] = { self.add_tile((i, j), random_tile_color)
"color": random_tile_color,
"mine": None
}
else: else:
# creating a tile with a mine # creating a tile with a mine
self.grid[str(i) + ',' + str(j)] = { self.add_tile_with_a_mine((i, j), random_tile_color, random_mine_type)
"color": random_tile_color,
"mine": {
"mine_type": random_mine_type
}
}
# adds a new tile or edits an existing one to 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):
# getting added/edited tile's index values # getting added/edited tile's index values
row, column = position row, column = position
@ -51,7 +43,7 @@ class JsonGenerator:
"mine": None "mine": None
} }
# adds a new tile with a mine or edits an existing one to the grid field # 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):
# getting added/edited tile's index values # getting added/edited tile's index values
row, column = position row, column = position
@ -70,7 +62,7 @@ class JsonGenerator:
# deleting a tile with given key # deleting a tile with given key
self.grid.pop(str(row) + ',' + str(column)) self.grid.pop(str(row) + ',' + str(column))
# adds a mine to a tile in the grid field # adds a mine to a tile stored in the grid field
def add_a_mine(self, position, mine_type): def add_a_mine(self, position, mine_type):
# getting edited tile's index values # getting edited tile's index values
row, column = position row, column = position
@ -93,34 +85,34 @@ class JsonGenerator:
# clears the grid field # clears the grid field
def clear_tile_dictionary(self): def clear_tile_dictionary(self):
self.grid.clear() self.grid.clear()
# resets agents starting position # resetting agents starting position
starting_row, starting_column = self.agent_starting_position starting_row, starting_column = self.agent_starting_position
self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column) self.grid["agent_starting_position"] = str(starting_row) + ',' + str(starting_column)
# loads a grid from a file and overwrites the grid field # loads a grid from a file and overwrites the grid field
def load_from_a_file(self, file_path): def load_from_a_file(self, file_path):
# opens file for reading # opening a file for reading
with open(file_path, 'r') as input_file: with open(file_path, 'r') as input_file:
# overwrites the grid field for the grid stored in a file # overwriting the grid field with the grid stored in a file
self.grid = json.load(input_file) self.grid = json.load(input_file)
# saves the current grid field to a file # saves the current grid field to a file
def save_to_a_file(self, file_path, access_mode): def save_to_a_file(self, file_path, access_mode):
# opens file with a given access mode (w - write / a - append) # opening a file with a given access mode (w - write / a - append)
with open(file_path, access_mode) as output_file: with open(file_path, access_mode) as output_file:
# saves the grid to a file # saving the grid to the file
json.dump(self.grid, output_file, indent=2, sort_keys=True) 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 # edits a grid in a file. doesn't delete data, only overwrites and adds new entries
def edit_a_file(self, file_path): def edit_a_file(self, file_path):
# open a file for reading # opening a file for reading
with open(file_path, "r") as input_file: with open(file_path, "r") as input_file:
# loads data that was stored in the file previously # loading data that was stored in the file previously
previous_data = json.load(input_file) previous_data = json.load(input_file)
# creates and updates a new grid using it's own grid field # creating and updating a new grid using it's own grid field
new_grid = previous_data new_grid = previous_data
new_grid.update(self.grid) new_grid.update(self.grid)
# opens the file for writing # opening the file for writing
with open(file_path, "w") as output_file: with open(file_path, "w") as output_file:
# saves the newly created grid # saving the newly created grid
json.dump(new_grid, output_file, indent=2, sort_keys=True) json.dump(new_grid, output_file, indent=2, sort_keys=True)

View File

@ -36,8 +36,8 @@ SCREEN = pygame.display.set_mode\
# === STRUCTS === # # === STRUCTS === #
# =============== # # =============== #
STRUCT_TILE_COLORS = ["blue", "green", "orange", "purple", "red", "white", "yellow"] STRUCT_TILE_COLORS = ["BLUE", "GREEN", "ORANGE", "PURPLE", "RED", "WHITE", "YELLOW"]
STRUCT_MINE_TYPES = ['a', 'b', 'f', 'k'] STRUCT_MINE_TYPES = ['A', 'B', 'F', 'K']
# ============== # # ============== #
# ==== MAPS ==== # # ==== MAPS ==== #