# hongrois.py

Created by schraf

Created on June 09, 2021

1.85 KB

## Les anneaux hongrois

Explications en vidéos

```from kandinsky import *
from math import *
from random import *
from ion import *
from time import sleep

DIV = 0.314
R = tuple(range(20))
L = (20,21,7,)+ tuple(range(22,36)) + (12,36,37)
I = list(range(29,36))+[12,36,37,20,21,7]+list(range(22,29))
COUL = {"J":(250,230,20),"N":(0,0,0),"R":(255,50,50),"B":(40,135,250),"F":(250,250,240)}

depart = "JJ" + "N"*10 + "R" + "J"*7 + "RR" + "B"*9 + "R"*7

def permu(c, pos, aff = True):
suiv = list(pos)
mvt = tuple(reversed(eval(c.upper()))) if c == c.lower() else eval(c)
u = (mvt[-1],) + mvt
for i,v in enumerate(mvt): suiv[v] = pos[u[i]]
suiv = "".join(suiv)
if aff:
anneaux(suiv)
sleep(0.2)
return suiv

def carre(x,y,c,t):
fill_rect(int(x - t),int(y - t),2 * t,2 * t,c)

def anneaux(jeu,x0 = 210,y0 = 130,r = 70,t = 7):
for i in range(20):
x = x0 + r * cos(DIV/2 + DIV * i)
y = y0 - r * sin(DIV/2 + DIV * i)
carre(x,y,COUL[jeu[R[i]]],t)
x = x0 - 1.414 * r + r * cos(DIV/2 + DIV * i)
carre(x,y,COUL[jeu[L[i]]],t)

def melange(pos, n):
for i in range(n): pos = permu(choice("LlRr"),pos,False)
return pos

def inverser(pos):
suiv = list(pos)
for k,v in enumerate(I):
suiv[k] = pos[v]
suiv[v] = pos[k]
suiv = "".join(suiv)
anneaux(suiv)
sleep(0.2)
return suiv

def fond():
f = COUL["F"]
n = COUL["N"]
fill_rect(0,0,320,222,f)
draw_string("1 >",20,40,n,f)
draw_string("0 >",20,200,n,f)
draw_string("> -",260,40,n,f)
draw_string("> EXE",260,200,n,f)

def go(n = 200):
fond()
pos = melange(list(depart), n)
anneaux(pos)
anneaux(depart,171,25,15,3)
while True:
if keydown(KEY_ZERO): pos = permu("L",pos)
if keydown(KEY_ONE): pos = permu("l",pos)
if keydown(KEY_EXE): pos = permu("R",pos)
if keydown(KEY_MINUS): pos = permu("r",pos)
if keydown(KEY_DIVISION): pos = inverser(pos)
if keydown(KEY_BACK): break
go()```