-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants_and_imports.py
104 lines (84 loc) · 2.62 KB
/
constants_and_imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# We keep all of our external imports here. We should only have internal imports within the other files.
import random
from itertools import product
import tkinter as tk
from tkinter import messagebox
import json
# Defaults and constants:
MINE_SYMBOL = "M"
FLAG_SYMBOL = "F"
DEFAULT_CONFIG = {
"colors": {
0: "white", 1: "blue", 2: "green",
3: "red", 4: "purple", 5: "orange",
6: "grey", 7: "maroon", 8: "turquoise",
MINE_SYMBOL: "black"
},
"difficulty_settings": {
"Easy": {
"height": 10,
"width": 10,
"mines": 15
},
"Medium": {
"height": 15,
"width": 15,
"mines": 40
},
"Hard": {
"height": 20,
"width": 20,
"mines": 99
},
"Custom": {
"height": None,
"width": None,
"mines": None
}
}
}
CUSTOM_ENTRY_FIELDS = ["Height: ", "Width: ", "Mines: "]
# Wrapper function to help pack the function for calling on-demand.
def on_click(f, *args):
def g(_):
print(f"The wrapper function on_click was called with arguments {f.__name__} {args}")
# try:
f(*args)
# except TypeError:
# print(f"on_click wrapper function reported argument errors! {f.__name__} {args}")
return g
class ConfigEditor:
# This object interacts with the config.txt of the project.
# Needless to say, if there is no config file, create it and populate it with the default config settings.
def create_cfg(self):
with open(self.config_file, "w") as f:
f.write(json.dumps(DEFAULT_CONFIG))
def read_cfg(self):
try:
with open(self.config_file, "r") as f:
cfg = json.loads(f.read())
return cfg
except Exception:
self.create_cfg()
return DEFAULT_CONFIG
def find_cfg(self, query_string: str):
# This gives me an idea for a class that traverses through generic JSON dictionaries.
query_result = self.options_dict
layer_list = query_string.split(".")
for layer in layer_list:
query_result = query_result[layer]
return query_result
def edit_cfg(self, query_string: str, edit_dict: dict):
# TODO: Find out how to filter out unwanted entries.
# For now we will simply hack together a clumsy json dict.
for edit_key, edit_value in edit_dict.items():
try:
self.find_cfg(query_string)[edit_key] = edit_value
except KeyError:
pass
with open(self.config_file, "w") as f:
f.write(json.dumps(self.options_dict))
# The class takes in a file name which contains your game configuration.
def __init__(self, config_file = "config.txt"):
self.config_file = config_file
self.options_dict = self.read_cfg()