-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
executable file
·170 lines (148 loc) · 6.96 KB
/
cli.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!./venv/bin/python
from generaterb import GenerateRB
from generateprims import GeneratePrims
from generatekruskal import GenerateKruskal
from solveastar import SolveAStar
from solverb import SolveRB
from os import listdir
from os.path import isfile, join
def main():
cli = CLI()
cli.main_menu()
class CLI:
def __init__(self):
self.decision = ''
self.operation = ''
def main_menu(self): # print main menu
while True:
options = ["Generate a new maze", "Solve an existing maze", "Quit"]
print("--------------------------------------------------")
print(" Maze Machine ")
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print(" Please select an operation: ")
for index in range(len(options)):
print(" (%s) %s" % (str(index + 1), options[index]))
print("--------------------------------------------------")
operation = input(">>")
if int(operation) == len(options):
break
if int(operation) >= len(options):
print("Please choose one of the specified options")
elif options[int(operation) - 1] == "Generate a new maze":
self.generate_maze()
elif options[int(operation) - 1] == "Solve an existing maze":
self.solve_maze()
def generate_maze(self):
while True:
options = ["Recursive Backtracking", "Primm's", "Kruskal's", "Back"]
print("--------------------------------------------------")
print(" Generate a Maze ")
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print(" Please select an algorithm: ")
for index in range(len(options)):
print(" (%s) %s" % (str(index + 1), options[index]))
print("--------------------------------------------------")
generator = None
operation = input(">>")
if int(operation) == len(options):
break
if int(operation) > len(options):
print("Please choose one of the specified options")
elif options[int(operation) - 1] == "Recursive Backtracking":
size = self.generation_settings("Recursive Backtracking")
generator = GenerateRB(size)
elif options[int(operation) - 1] == "Primm's":
size = self.generation_settings("Primm's")
generator = GeneratePrims(size)
elif options[int(operation) - 1] == "Kruskal's":
size = self.generation_settings("Kruskal's")
generator = GenerateKruskal(size)
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print("Would you like to save the process as well as the solution? (Y/N)")
answer = None
while answer is None:
answer = input(">>")
if answer.upper() != "Y" and answer.upper() != "N":
print("Please answer with yes (Y) or no (N)")
answer = None
if answer.upper() == "Y":
print("Saving solution and history.")
generator.generate([])
else:
print("Saving solution.")
generator.generate()
generator.print_results()
print("To compile a gif of the solution, use")
print("gifski --fps 60 -o out.gif *.png")
@staticmethod
def generation_settings(title):
width = 50
header = " " * ((width - len(title)) // 2) + title + " " * ((width - len(title)) // 2)
print("--------------------------------------------------")
print(header)
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print(" Please enter the maze size (N x N): ")
response = int(input(">>"))
if response > 10000:
print("That's way to big. Exiting.")
return None
else:
return response
@staticmethod
def solve_maze():
while True:
options = ["Recursive Backtracking", "A-Star", "Back"]
print("--------------------------------------------------")
print(" Solve a Maze ")
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print(" Please select an algorithm: ")
for index in range(len(options)):
print(" (%s) %s" % (str(index + 1), options[index]))
print("--------------------------------------------------")
operation = input(">>")
if int(operation) == len(options):
break
if int(operation) >= len(options):
print("Please choose one of the specified options")
elif options[int(operation) - 1] == "Recursive Backtracking":
CLI.solve_generic("Recursive Backtracking", SolveRB())
elif options[int(operation) - 1] == "A-Star":
CLI.solve_generic("A-Star", SolveAStar())
@staticmethod
def solve_generic(title, solver):
while True:
path = "./mazes/"
options = [f for f in listdir(path) if isfile(join(path, f))] + ["Back"]
width = 50
header = " " * ((width - len(title)) // 2) + title + " " * ((width - len(title)) // 2)
print("--------------------------------------------------")
print(header)
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
print(" Please select a PNG to import ")
for index in range(len(options)):
print(" (%s) %s" % (str(index + 1), options[index]))
print("- - - - - - - - - - - - - - - - - - - - - - - - - ")
user_input = input(">>")
if int(user_input) == len(options):
break
solver.import_png(path + options[int(user_input) - 1])
print("PNG imported")
print("Would you like to save the process as well as the solution? (Y/N)")
answer = None
history = None
while answer is None:
answer = input(">>")
if answer.upper() != "Y" and answer.upper() != "N":
print("Please answer with yes (Y) or no (N)")
answer = None
if answer.upper() == "Y":
print("Saving solution and history.")
history = []
else:
print("Saving solution.")
solution = solver.solve((1, 1), (solver.maze.size - 2, solver.maze.size - 2), history)
solver.save_solution(solution, history)
print("To compile a gif of the solution, use")
print("gifski --fps 60 -o out.gif *.png")
if __name__ == "__main__":
main()