Codes de la vidéo
from random import randint
def grilleVide():
grille = []
for i in range(6):
ligne = []
for j in range(6): ligne.append('⬛')
grille.append(ligne)
return grille
def rotate(m, n):
for _ in range(n): m = list(zip(*m))[::-1]
return m
def afficher(g):
for l in range(6): print(''.join(g[l]))
def creerGrille():
grille = grilleVide()
m = [[6 * l + c for c in range(6)] for l in range(6)]
for c in range(3):
for l in range(3):
m = rotate(m, randint(0, 3))
pos = m[l][c]
x, y = pos % 6, pos // 6
grille[y][x] = '⬜'
return grille
def completeEtoiles(phrase):
return phrase + "*" * (9 - len(phrase) % 9)
def listeTrous(g):
trous = []
for l in range(6):
for c in range(6):
if g[l][c] == '⬜': trous.append(6 * l + c)
return trous
def chiffrer(phrase, grille):
phrase = completeEtoiles(phrase)
chiffre = grilleVide()
trous = listeTrous(grille)
for i, v in enumerate(phrase):
pos = i % 9
if pos == 0 and i > 0:
grille = rotate(grille, 1)
trous = listeTrous(grille)
x, y = trous[pos] % 6, trous[pos] // 6
if v == "*": v = chr(randint(65, 90))
chiffre[y][x] = v
return chiffre
def dechiffrer(crypte, grille):
clair = ''
trous = listeTrous(grille)
for i in range(36):
pos = i % 9
if pos == 0 and i > 0:
grille = rotate(grille, 1)
trous = listeTrous(grille)
x, y = trous[pos] % 6, trous[pos] // 6
clair += crypte[y][x]
return clair
grille = creerGrille()
afficher(grille)
phrase='RENDEZVOUSAQUINZEHEURESPLACELECLERC'
crypte = chiffrer(phrase, grille)
afficher(crypte)
clair = dechiffrer(crypte, grille)
print(clair)