189 lines
5.1 KiB
Python
189 lines
5.1 KiB
Python
### import nécéssaires pour le TP
|
|
import time
|
|
from tkinter import *
|
|
|
|
####################################################################################
|
|
# Labyrinthe
|
|
####################################################################################
|
|
def find_e(t):
|
|
"""Permet de trouver l'entrée du labyrinthe"""
|
|
for ligne in range(len(maze)):
|
|
col = t[ligne].find("e")
|
|
if col != -1:
|
|
return ligne, col
|
|
|
|
|
|
def aff(t):
|
|
"""Affiche le labyrinth t"""
|
|
for ligne in range(len(t)):
|
|
print(t[ligne])
|
|
|
|
|
|
def avance(maze, p):
|
|
"""Avance dans le labyrinthe"""
|
|
# on va sur la position du haut de la pile
|
|
l, c = p.pop()
|
|
|
|
# on marque la position comme "exploré"
|
|
ligne = list(maze[l])
|
|
ligne[c] = "x"
|
|
maze[l] = "".join(ligne)
|
|
|
|
# pour toutes les cases adjacentes ...
|
|
for dl, dc in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
|
|
# on empile les positions adjacentes libres
|
|
case = maze[l + dl][c + dc]
|
|
if case == " ":
|
|
p.append((l + dl, c + dc))
|
|
# si on a trouver la sortie, on vide la pile
|
|
if case == "s":
|
|
p.empty()
|
|
return l, c
|
|
|
|
|
|
# Notre labyrinthe
|
|
maze_str = """
|
|
######################################
|
|
#e # # # # #
|
|
###### ## # # # # # # ##############
|
|
# ## # # # # # ##s #
|
|
###### ## # # #### # # ## # # ## #
|
|
# # # # ## # #
|
|
# ############## # # ######## # # ##
|
|
# # # # # # # ## #
|
|
# ##### ############ # # # ### ##
|
|
# # # # # ## # ##
|
|
######################################
|
|
"""
|
|
|
|
# On transforme le labyrinthe en listes de chaînes de caractères
|
|
maze = maze_str.split("\n")
|
|
|
|
# pile des positions inexplorées
|
|
p = Pile()
|
|
p.append(find_e(maze))
|
|
|
|
# Tant que la pile n'est pas vide ... on avance
|
|
AFFICHAGE_CONSOLE = False
|
|
while not p.is_empty() and AFFICHAGE_CONSOLE:
|
|
avance(maze, p)
|
|
aff(maze)
|
|
time.sleep(0.5)
|
|
|
|
########################
|
|
# Interface avec tkinter
|
|
########################
|
|
|
|
# Réinitialisation du labyrinthe
|
|
maze = maze_str.split("\n")
|
|
p.append(find_e(maze))
|
|
|
|
# Taille des cellules et du labyrinthe
|
|
cell_size = 16
|
|
xmax = max([len(t) for t in maze]) * cell_size
|
|
ymax = len(maze) * cell_size
|
|
|
|
# Affichage
|
|
root = Tk()
|
|
root.title = "Labyrinthe"
|
|
|
|
canevas = Canvas(root, width=xmax, height=ymax, background="#FFFFFF")
|
|
canevas.pack()
|
|
|
|
for y in range(len(maze)):
|
|
for x in range(len(maze[y])):
|
|
if maze[y][x] != " ":
|
|
canevas.create_rectangle(
|
|
cell_size * x,
|
|
cell_size * y,
|
|
cell_size * (x + 1),
|
|
cell_size * (y + 1),
|
|
fill="#111111",
|
|
)
|
|
if maze[y][x] == "e" or maze[y][x] == "s":
|
|
canevas.create_rectangle(
|
|
cell_size * x,
|
|
cell_size * y,
|
|
cell_size * (x + 1),
|
|
cell_size * (y + 1),
|
|
fill="#005533",
|
|
)
|
|
|
|
# Boucle principale d'affichage
|
|
def loop():
|
|
if not p.is_empty():
|
|
l, c = avance(maze, p)
|
|
canevas.create_rectangle(
|
|
cell_size * c,
|
|
cell_size * l,
|
|
cell_size * (c + 1),
|
|
cell_size * (l + 1),
|
|
fill="#00FF00",
|
|
)
|
|
root.after(50, loop)
|
|
|
|
|
|
root.after(50, loop)
|
|
root.mainloop()
|
|
|
|
####################################################################################
|
|
# Labyrinthes à tester :)
|
|
####################################################################################
|
|
|
|
maze_str = """
|
|
######################################
|
|
#e # # # # #
|
|
###### ## # # # # # # ##############
|
|
# ## # # # # # ##s #
|
|
###### ## # # #### # # ## # # ## #
|
|
# # # # ## # #
|
|
# ############## # # ######## # # ##
|
|
# # # # # # # ## #
|
|
# ##### ############ # # # ### ##
|
|
# # # # # ## # ##
|
|
######################################
|
|
"""
|
|
|
|
maze_str = """
|
|
######### ####
|
|
## ## ##### s#
|
|
# # ## # ##
|
|
# # ## # # ####
|
|
## # # #
|
|
#### # # # # #
|
|
##### #### ### #
|
|
# # # # # #### # # #
|
|
# # # # ## # #
|
|
###### # # ### # #
|
|
# ## # # #
|
|
#### # #####
|
|
## # ## # #
|
|
# # # ### ###
|
|
# # # e####
|
|
##### # #
|
|
########
|
|
"""
|
|
|
|
maze_str = """
|
|
####
|
|
#e #
|
|
## ########
|
|
####### # #
|
|
# # # # # # # ##
|
|
# # ## # #
|
|
### ## # # # #########
|
|
# # ## # ## # # #
|
|
## # # ## # # # #
|
|
# ## # ### # # ### # #
|
|
# ### # # ## ## # ##
|
|
##### # # ## # # #
|
|
# # ### # # # ### ### ##
|
|
# ## # # # ## #
|
|
# ## ## # ## ## # #
|
|
## # # # # ## ## # ###
|
|
## ## # # # # # # ###
|
|
# ##### # # # ###
|
|
## # # # # #s###
|
|
######## ###########
|
|
"""
|