-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (104 loc) · 5.37 KB
/
main.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
import tkinter as tk
from gen_lab import GenLab
import res_lab
# Création de la fenêtre tkinter
app = tk.Tk()
app.title("Labyrinthe")
app.geometry("800x600")
app.resizable(False, False)
# Création du canevas
canvas = tk.Canvas(app, width=500, height=500) # initialiser le canevas avec une taille arbitraire
canvas.pack()
labyrinthe_global = None
dim_global = tk.IntVar(value=25) # Valeur de la dimension de base : 25
longueur_var = tk.StringVar() # Variable longeur du labyrinthe
temps_var = tk.StringVar() # Variable temps du labyrinthe
seed_global = tk.StringVar(value="") # Variable seed du labyrinthe : par défaut vide
def affiche_lab():
global labyrinthe_global
'''
Affiche le labyrinthe sur l'application tkinter
'''
dim = dim_global.get()
seed_value = seed_global.get() # Récupère la seed entrée par l'utilisateur
if seed_value:
seed_value = int(seed_value) # Convertir la seed en entier si écrite
else:
seed_value = None # Pas de seed alors génération aléatoire
dimcanva = 500 / dim
gen = GenLab(dim, seed_value=seed_value) # Donner la seed pour générer le labyrinthe
labyrinthe_global, temps_generation = gen.dessine_lab()
temps_generation = round(temps_generation * 10000) / 10000
longueur_var.set(f"Longueur: N/A")
temps_var.set(f"Temps de génération du labyrinthe : {temps_generation} secondes")
# Afficher la seed générée
if not seed_value:
seed_global.set(str(gen.generated_seed)) # Afficher la seed générée
# Mise à jour de l'affichage du labyrinthe sur le canvas
canvas.config(width=len(labyrinthe_global[0]) * dimcanva, height=len(labyrinthe_global) * dimcanva)
canvas.delete("all") # Effacer tout ce qui est dessiné précédemment sur le canvas
# Parcours du labyrinthe pour dessiner les murs et les espaces
for i, ligne in enumerate(labyrinthe_global):
for j, case in enumerate(ligne):
if case == 1:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="black")
elif case == 2:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="green")
elif case == 3:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="red")
else:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="white")
def resoudre_lab():
global labyrinthe_global
dim = dim_global.get()
dimcanva = 500 / dim
# Récupérer le labyrinthe et la dimension depuis la fonction affiche_lab
lab_resolu, success, longueur,temps_resolution = res_lab.res_lab(labyrinthe_global, dim)
temps_resolution=round(temps_resolution*10000)
temps_resolution=temps_resolution/10000
if temps_resolution!=0:
temps_var.set(f"Temps de résolution du labyrinthe: {temps_resolution} secondes")
# Mettre à jour la longueur du labyrinthe résolu
if longueur is not None:
longueur_var.set(f"Longueur: {longueur} cases")
elif not lab_resolu[dim-3][dim-2]==5:
if not lab_resolu[dim-2][dim-3]==5:
longueur_var.set("Longueur: N/A")
# Effacer tout ce qui est dessiné précédemment sur le canvas
canvas.delete("all")
# Parcours du labyrinthe pour dessiner les murs et les espaces
for i, ligne in enumerate(lab_resolu):
for j, case in enumerate(ligne):
if case == 1:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="black")
elif case == 2:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="green")
elif case == 3:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="red")
elif case == 5:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="lime")
else:
canvas.create_rectangle(j * dimcanva, i * dimcanva, (j + 1) * dimcanva, (i + 1) * dimcanva, fill="white")
# Zone des Boutons et de l'étiquette pour afficher la longueur et la seed
button_frame = tk.Frame(app)
button_frame.pack(side=tk.TOP, pady=10)
dimensions = [15, 25, 35, 45, 55, 65, 75, 85, 95, 105]
dim_menu = tk.OptionMenu(button_frame, dim_global, *dimensions)
dim_menu.pack(side=tk.LEFT, padx=5)
# Champ d'entrée pour la seed
seed_label = tk.Label(button_frame, text="Seed :")
seed_label.pack(side=tk.LEFT, padx=5)
seed_entry = tk.Entry(button_frame, textvariable=seed_global)
seed_entry.pack(side=tk.LEFT, padx=5)
gen_button = tk.Button(button_frame, text="Générer Labyrinthe", command=affiche_lab)
gen_button.pack(side=tk.LEFT, padx=5)
res_button = tk.Button(button_frame, text="Résoudre Labyrinthe", command=resoudre_lab)
res_button.pack(side=tk.LEFT, padx=5)
texte_frame = tk.Frame(app)
texte_frame.pack(side=tk.TOP)
longueur_label = tk.Label(texte_frame, textvariable=longueur_var)
longueur_label.pack()
temps_label = tk.Label(texte_frame, textvariable=temps_var)
temps_label.pack()
# Lancer la boucle principale de tkinter
app.mainloop()