rubik.py

Created by schraf

Created on November 01, 2022

1.1 KB

Vidéo d’explication

J’ai également fait une version visuelle dans le cas du cube 2x2x2

Le programme calcule l’ordre d’un mouvement, c’est-à-dire combien de fois il faut le répéter pour revenir au point de départ. Il est suffisamment général pour pouvoir s’appliquer au Rubik’s cube classique, au pocket ou à d’autres jeux comme les anneaux Hongrois.

>> ordre("FR") donnera 15 (Le mouvement est face avant + droite)
Utilisez des minuscules pour tourner une face en sens inverse.

Pour passer d’un objet à un autre, faire un copier-coller des constantes ci-dessous.

Rubik 2x2x2

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"

Rubik 3x3x3

F=[(0,6,8,2),(1,3,7,5),(9,45,44,35),(29,11,51,38),(10,48,41,32)] 
B=[(18,24,26,20),(19,21,25,23),(15,33,42,47),(27,36,53,17),(16,30,39,50)] 
U=[(9,15,17,11),(10,12,16,14),(6,27,20,45),(29,18,47,8),(28,19,46,7)] 
D=[(42,36,38,44),(43,39,37,41),(0,51,20,33),(35,2,53,24),(1,52,25,34)] 
R=[(45,47,53,51),(46,50,52,48),(2,11,20,42),(44,8,17,26),(5,14,23,43)] 
L=[(27,29,35,33),(28,32,34,30),(0,36,18,9),(38,24,15,6),(12,3,37,21)] 
CUBES=9 
COUL="RBWVOJ"

Anneaux Hongrois

L=[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 23)] 
R=[(19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 4)]
COUL="RBNJ" 
cube="R"*10+"B"*9+"N"*10+"J"*9


F=[(0,6,8,2),(1,3,7,5),(9,45,44,35),(29,11,51,38),(10,48,41,32)] 
B=[(18,24,26,20),(19,21,25,23),(15,33,42,47),(27,36,53,17),(16,30,39,50)] 
U=[(9,15,17,11),(10,12,16,14),(6,27,20,45),(29,18,47,8),(28,19,46,7)] 
D=[(42,36,38,44),(43,39,37,41),(0,51,20,33),(35,2,53,24),(1,52,25,34)] 
R=[(45,47,53,51),(46,50,52,48),(2,11,20,42),(44,8,17,26),(5,14,23,43)] 
L=[(27,29,35,33),(28,32,34,30),(0,36,18,9),(38,24,15,6),(12,3,37,21)] 
CUBES=9 
COUL="RBWVOJ"


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

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]]
  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 rubik():
  cube = ""
  for c in COUL: cube += c*CUBES
  return cube
  
def ano():
  return "R"*10+"B"*9+"N"*10+"J"*9

def ordre(m, obj=rubik):
  pos = obj()
  nb = 1
  while True:
    for k,c in enumerate(m):
      pos = permu(c,pos)
    if fin(pos): return nb
    nb += 1

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.