277 lines
7.6 KiB
Python
277 lines
7.6 KiB
Python
########################################################################################
|
|
#
|
|
# TP - Structure de données linéaire : La pile
|
|
#
|
|
########################################################################################
|
|
|
|
### import nécéssaires pour le TP
|
|
import random
|
|
import time
|
|
from tkinter import *
|
|
|
|
########################################################################################
|
|
# Classes Node() et Pile()
|
|
########################################################################################
|
|
class Node:
|
|
def __init__(self, a=None, nxt=None) -> None:
|
|
self.value = a
|
|
self.nxt = nxt
|
|
|
|
|
|
class Pile:
|
|
def __init__(self) -> None:
|
|
self.current = None
|
|
self.n = 0
|
|
|
|
def is_empty(self):
|
|
return self.current == None
|
|
|
|
def add(self, valeur):
|
|
self.current = Node(valeur, nxt=self.current)
|
|
self.n += 1
|
|
|
|
def depile(self):
|
|
if not self.is_empty():
|
|
a = self.current.value
|
|
self.current = self.current.nxt
|
|
self.n -= 1
|
|
return a
|
|
else:
|
|
return None
|
|
|
|
def get_top(self):
|
|
return self.current.value if not self.is_empty() else None
|
|
|
|
def vide_la_pile(self):
|
|
while not self.is_empty():
|
|
self.depile()
|
|
|
|
|
|
####################################################################################
|
|
# Jeu de cartes
|
|
####################################################################################
|
|
def DeckOfCards():
|
|
jeu_trie = []
|
|
for valeur in [2, 3, 4, 5, 6, 7, 8, 9, 10, "Valet", "Dame", "Roi", "As"]:
|
|
for couleur in ["Pique", "Coeur", "Trèfle", "Carreau"]:
|
|
jeu_trie.append((valeur, couleur))
|
|
jeu = Pile()
|
|
while jeu_trie != []:
|
|
# Un index du jeu trié au hasard
|
|
i = random.randint(0, len(jeu_trie) - 1)
|
|
jeu.add(jeu_trie[i]) # On ajoute la carte à la pile
|
|
jeu_trie.pop(i) # On retire la carte du jeu trié
|
|
return jeu
|
|
|
|
|
|
def main_bjack(jeu):
|
|
result = 0
|
|
if jeu.n < 2:
|
|
return result
|
|
c1, c2 = jeu.depile(), jeu.depile()
|
|
for c in [c1, c2]:
|
|
if (c[0] == "Valet") or (c[0] == "Dame") or (c[0] == "Roi"):
|
|
result += 10
|
|
elif c[0] == "As":
|
|
result += 11
|
|
else:
|
|
result += c[0]
|
|
print(c1, c2, result)
|
|
return result
|
|
|
|
|
|
# Test : Distribution de toutes les cartes
|
|
j = DeckOfCards()
|
|
while not j.is_empty():
|
|
print(j.depile())
|
|
|
|
# Test : Calcul de mains de blackjack
|
|
j = DeckOfCards()
|
|
while not j.is_empty():
|
|
main_bjack(j)
|
|
|
|
####################################################################################
|
|
# 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.depile()
|
|
|
|
# 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.add((l + dl, c + dc))
|
|
# si on a trouver la sortie, on vide la pile
|
|
if case == "s":
|
|
p.vide_la_pile()
|
|
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.add(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.add(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###
|
|
######## ###########
|
|
"""
|