Advertisement
TadewosAbiye

Untitled

May 25th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.73 KB | None | 0 0
  1. import copy
  2. import numpy as np
  3. CONVENTION = {'Y':('Top',0),'R':('Front',1),'B':('Left',2),
  4.               'G':('Right',3),'W':('Bottom',4),'O':('Back',5)}
  5. CONVENTION_I = {CONVENTION[i][0]:i for i in CONVENTION}
  6.  
  7. CONVERT = { 'B':0,'R':1,'G':2,'O':3, 'Y':4 , 'W':5 }
  8. CONVENTION2 = {CONVERT[i]:CONVENTION[i] for i in CONVENTION}
  9. Array = [[['B','B','B'],['R','R','R'],['O','O','O']],
  10.          [['R','R','R'],['G','G','G'],['B','B','B']],
  11.          [['G','G','G'],['O','O','O'],['R','R','R']],
  12.          [['O','O','O'],['B','B','B'],['G','G','G']],
  13.          [['Y','Y','Y'],['Y','Y','Y'],['Y','Y','Y']],
  14.          [['W','W','W'],['W','W','W'],['W','W','W']]]
  15.  
  16. def Convert(Array):
  17.     New = []
  18.     for face in Array:
  19.         New_Face = []
  20.         for row in face:
  21.             New_Face.append(map(lambda x:CONVERT[x],row))
  22.         New.append(np.matrix(New_Face))
  23.     return New
  24.            
  25.        
  26.  
  27. class Cube:    
  28.     ''' A quick and dirty implementation of a Rubiks cube '''
  29.     def __init__(self , Cube_Array , Faces = None ):
  30.         '''Takes an array of faces and sorts them out and assigns a conventional reference point '''
  31.         self.History = []
  32.         if Faces == None:
  33.            
  34.             self.Faces  = {}
  35.             for face in Cube_Array:
  36.                 Key = CONVENTION[ face[1][1] ][0]
  37.                 self.Faces[ Key ] = face        
  38.    
  39.         else:
  40.             self.Faces = Faces
  41.  
  42.     def __repr__( self ):
  43.         '''Output for printing the cube object'''
  44.         for f in ['Front','Right','Back','Left','Top','Bottom']:
  45.             for i in self.Faces[f]:print i
  46.             print (f+'^=^')*12
  47.         return ''
  48.    
  49.     def __getitem__(self,indice):
  50.         return self.Faces[indice]
  51.    
  52.     def __iter__( self ):
  53.         return iter( self.Faces.values() )
  54.            
  55.     def get_face( self , position ):
  56.         '''Returns a face given the position of the'''
  57.         return self.Faces[ position ]
  58.    
  59.     def get_row( self , position , row , Input = None):
  60.         '''Returns a row of a given face'''
  61.         if Input != None:return Input[ position ][ row ]
  62.         return self.Faces[ position ][ row ]
  63.  
  64.     def get_column( self , position , column , Input = None):
  65.         '''Return a column of a given face'''
  66.         if Input != None:return [ Input[ position ][ row ][ column ] for row in range(3) ]
  67.         column = [ self.Faces[ position ][ row ][ column ] for row in range(3) ]
  68.         return column
  69.  
  70.     def is_solved(self):
  71.         ''' Checks wheather the cube has been solved or not '''
  72.         for face in self.Faces:
  73.             All=self.Faces[face]
  74.             if len(set(map(tuple,All))) != 1:
  75.                 return False
  76.             for i in All:
  77.                 if len(set(i))!=1:
  78.                     return False
  79.            
  80.         return True
  81.  
  82.     def F( self , clockwise = True ):
  83.         faces = {i:[] for i in self.Faces}
  84.         faces['Back'] = self.Faces['Back']
  85.         for i in range(3):
  86.             if clockwise:
  87.                 faces['Front'].append(self.get_column('Front',i)[::-1])
  88.                 faces['Right'].append([self.get_row('Top',2)[i]]+self.get_row('Right',i)[1::])
  89.                 faces['Left'].append(self.get_row('Left',i)[0:2]+[self.get_row('Bottom',0)[i]] )
  90.                 faces['Top'] = self.Faces['Top'][0:2] + [self.get_column('Left',2)[::-1]]
  91.                 faces['Bottom'] = [self.get_column('Right',0)[::-1]] + self.Faces['Bottom'][1::]
  92.             else:
  93.                 faces['Front'].append(self.get_column('Front',2-i))
  94.                 faces['Right'].append([self.get_row('Bottom',0)[2-i]]+self.get_row('Right',i)[1::])
  95.                 faces['Left'].append(self.get_row('Left',i)[0:2]+[self.get_row('Top',2)[2-i]])
  96.                 faces['Top']=self.Faces['Top'][0:2] + [self.get_column('Right',0)]
  97.                 faces['Bottom']=[self.get_column('Left',2)]+self.Faces['Bottom'][1::]
  98.      
  99.         self.Faces = faces
  100.  
  101.     def Rotate_Up(self):
  102.         ''' Rotate the whole cube 90 degrees up'''
  103.         Faces = {i:[] for i in self.Faces}
  104.         Faces['Top'] = self.Faces['Front']
  105.         Faces['Front'] = self.Faces['Bottom']
  106.         Faces['Back']=[ i[::-1] for i in self.Faces['Top'] ][::-1]
  107.         Faces['Bottom'] =[ i[::-1] for i in self.Faces['Back']][::-1]
  108.         for r in range(3):
  109.             Faces['Right'].append( self.get_column('Right',r)[::-1] )
  110.             Faces['Left'].append( self.get_column('Left',2-r))
  111.         self.Faces = Faces
  112.  
  113.     def Rotate_Down(self):
  114.         ''' Rotate the whole cube 90 degrees down'''
  115.         for i in range(3):self.Rotate_Up()
  116.        
  117.     def Rotate_Right(self):
  118.         ''' Rotate the whole cube 90 degrees to the right'''
  119.         Faces = {i:[] for i in self.Faces}
  120.         Faces['Right']=self.Faces['Front']
  121.         Faces['Back']=self.Faces['Right']
  122.         Faces['Left']=self.Faces['Back']
  123.         Faces['Front']=self.Faces['Left']
  124.         for r in range(3):
  125.             Faces['Top'].append(self.get_column('Top',2-r))
  126.             Faces['Bottom'].append(self.get_column('Bottom',r)[::-1])
  127.         self.Faces=Faces
  128.  
  129.     def Rotate_Left(self):
  130.         ''' Rotate the whole cube 90 degrees to the left'''
  131.         Faces = {i:[] for i in self.Faces}
  132.         Faces['Right'] = self.Faces['Back']
  133.         Faces['Front'] = self.Faces['Right']
  134.         Faces['Left'] = self.Faces['Front']
  135.         Faces['Back'] = self.Faces['Left']
  136.         for r in range(3):
  137.             Faces['Top'].append(self.get_column('Top',r)[::-1])
  138.             Faces['Bottom'].append(self.get_column('Bottom',2-r))
  139.         self.Faces = Faces
  140.    
  141.  
  142.     def U(self,clockwise=True):
  143.         ''' Rotate the upper face of the cube clockwise or anticlockwise'''
  144.         TempCube = Cube( self.Faces.values() )      
  145.         TempCube.Rotate_Down()
  146.         TempCube.F(clockwise)
  147.         TempCube.Rotate_Up()
  148.         self.Faces = TempCube.Faces
  149.  
  150.        
  151.     def D(self,clockwise=True):
  152.         '''Rotate the bottom face of the cube clock/anti wise'''
  153.         TempCube = Cube( self.Faces.values() )      
  154.         TempCube.Rotate_Up()
  155.         TempCube.F(clockwise)
  156.         TempCube.Rotate_Down()
  157.         self.Faces = copy.copy(TempCube.Faces)
  158.  
  159.        
  160.     def B(self,clockwise=True):
  161.         '''Rotate the back face of the cube clock/anti wise'''
  162.         TempCube = Cube( self.Faces.values() )      
  163.         TempCube.Rotate_Right()
  164.         TempCube.Rotate_Right()
  165.         TempCube.F(clockwise)
  166.         TempCube.Rotate_Right()
  167.         TempCube.Rotate_Right()
  168.         self.Faces = TempCube.Faces
  169.  
  170.        
  171.     def L(self,clockwise=True):
  172.         ''' Rotate the left face of the cube clockwise or anticlockwise'''
  173.         TempCube = Cube( self.Faces.values() )      
  174.         TempCube.Rotate_Right()
  175.         TempCube.F(clockwise)
  176.         TempCube.Rotate_Left()
  177.         self.Faces = TempCube.Faces
  178.  
  179.        
  180.     def R(self,clockwise=True):
  181.         ''' Rotate the right face of the cube clockwise or anticlockwise'''
  182.         TempCube = Cube(  self.Faces.values() )    
  183.         TempCube.Rotate_Left()
  184.         TempCube.F(clockwise)
  185.         TempCube.Rotate_Right()
  186.         self.Faces = TempCube.Faces
  187.      
  188.  
  189.     def eeval(self,Move):      
  190.         Move += ' '
  191.         Dir , Orientation = Move[0] , Move[1]!="'"
  192.         if Dir == 'F':
  193.             self.F(Orientation)
  194.         elif Dir == 'R':
  195.             self.R(Orientation)
  196.         elif Dir == 'L':
  197.             self.L(Orientation)
  198.         elif Dir == 'U':
  199.             self.U(Orientation)
  200.         elif Dir == 'D':
  201.             self.D(Orientation)
  202.         elif Dir == 'B':
  203.             self.B(Orientation)
  204.         else:
  205.             raise ValueError #Wrong Input!
  206.     def Eval(self,moves):
  207.         for move in moves:
  208.             self.eeval(move)
  209. A=Cube(Array)
  210. A.D()
  211. A.D()
  212. A.U()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement