rubik222.py

Created by schraf

Created on June 05, 2021

1.41 KB

Amélioration du programme Rubik avec visualisation éclatée du cube 2x2x2

Vous pouvez tester par exemple avec >> ordre("FR")


from kandinsky import *

U=[(8,9,11,10),(2,12,17,7),(3,14,16,5)] 
R=[(12,13,15,14),(23,19,11,3),(21,17,9,1)] 
L=[(4,5,7,6),(0,8,16,20),(2,10,18,22)] 
F=[(16,17,19,18),(6,10,14,21),(7,11,15,20)] 
D=[(20,21,23,22),(18,15,1,4),(19,13,0,6)] 
B=[(3,2,0,1),(13,9,5,22),(12,8,4,23)] 
CUBES = 4 
COUL="RBWVOJ"
# Couleurs et positions des faces
RVB={"R":(255,0,0), "B":(0,0,255), "W":(255,255,255), "V":(0,255,0),"O":(255,190,0),"J":(255,255,0)}
COINS=[[136,10],[88,58],[136,58],[184,58],[136,106], [136,154]]

def inv(l):
  return [tuple(reversed(t)) for t in reversed(l)]

# Affichage d une facette en couleur
def aff(v, c):
  xy=COINS[v//4]
  for i in range(24*24):
    coul = RVB[c] if 0<i%24<23 and 0<i//24<23 else (0,)*3
    set_pixel(xy[0]+24*(v%2)+i%24,xy[1]+24*((v%4)//2)+i//24,coul)

def permu(c, pos):
  suiv = list(pos)
  mvt = inv(eval(c.upper())) if c==c.lower() else eval(c) 
  for t in mvt:
    u = (t[-1],)+t
    for i,v in enumerate(t):
      suiv[v] = pos[u[i]]
      aff(v, suiv[v])
  return suiv
  
def fin(pos):
  c, nb = pos[0], 1
  for v in pos:
    if v != c: 
      c = v
      nb += 1
  return nb == len(COUL)
   
def ordre(m):
  cube = ""
  for c in COUL: cube += c*CUBES
  pos=list(cube)
  for i,v in enumerate(pos):
    aff(i,v)
  nb = 1
  while True:
    p = input()
    for k,c in enumerate(m):
      pos = permu(c,pos)
    if fin(pos): 
      draw_string(str(nb),10,10)
      return True
    nb += 1