Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- import numpy as np
- CONVENTION = {'Y':('Top',0),'R':('Front',1),'B':('Left',2),
- 'G':('Right',3),'W':('Bottom',4),'O':('Back',5)}
- CONVENTION_I = {CONVENTION[i][0]:i for i in CONVENTION}
- CONVERT = { 'B':0,'R':1,'G':2,'O':3, 'Y':4 , 'W':5 }
- CONVENTION2 = {CONVERT[i]:CONVENTION[i] for i in CONVENTION}
- Array = [[['B','B','B'],['R','R','R'],['O','O','O']],
- [['R','R','R'],['G','G','G'],['B','B','B']],
- [['G','G','G'],['O','O','O'],['R','R','R']],
- [['O','O','O'],['B','B','B'],['G','G','G']],
- [['Y','Y','Y'],['Y','Y','Y'],['Y','Y','Y']],
- [['W','W','W'],['W','W','W'],['W','W','W']]]
- def Convert(Array):
- New = []
- for face in Array:
- New_Face = []
- for row in face:
- New_Face.append(map(lambda x:CONVERT[x],row))
- New.append(np.matrix(New_Face))
- return New
- class Cube:
- ''' A quick and dirty implementation of a Rubiks cube '''
- def __init__(self , Cube_Array , Faces = None ):
- '''Takes an array of faces and sorts them out and assigns a conventional reference point '''
- self.History = []
- if Faces == None:
- self.Faces = {}
- for face in Cube_Array:
- Key = CONVENTION[ face[1][1] ][0]
- self.Faces[ Key ] = face
- else:
- self.Faces = Faces
- def __repr__( self ):
- '''Output for printing the cube object'''
- for f in ['Front','Right','Back','Left','Top','Bottom']:
- for i in self.Faces[f]:print i
- print (f+'^=^')*12
- return ''
- def __getitem__(self,indice):
- return self.Faces[indice]
- def __iter__( self ):
- return iter( self.Faces.values() )
- def get_face( self , position ):
- '''Returns a face given the position of the'''
- return self.Faces[ position ]
- def get_row( self , position , row , Input = None):
- '''Returns a row of a given face'''
- if Input != None:return Input[ position ][ row ]
- return self.Faces[ position ][ row ]
- def get_column( self , position , column , Input = None):
- '''Return a column of a given face'''
- if Input != None:return [ Input[ position ][ row ][ column ] for row in range(3) ]
- column = [ self.Faces[ position ][ row ][ column ] for row in range(3) ]
- return column
- def is_solved(self):
- ''' Checks wheather the cube has been solved or not '''
- for face in self.Faces:
- All=self.Faces[face]
- if len(set(map(tuple,All))) != 1:
- return False
- for i in All:
- if len(set(i))!=1:
- return False
- return True
- def F( self , clockwise = True ):
- faces = {i:[] for i in self.Faces}
- faces['Back'] = self.Faces['Back']
- for i in range(3):
- if clockwise:
- faces['Front'].append(self.get_column('Front',i)[::-1])
- faces['Right'].append([self.get_row('Top',2)[i]]+self.get_row('Right',i)[1::])
- faces['Left'].append(self.get_row('Left',i)[0:2]+[self.get_row('Bottom',0)[i]] )
- faces['Top'] = self.Faces['Top'][0:2] + [self.get_column('Left',2)[::-1]]
- faces['Bottom'] = [self.get_column('Right',0)[::-1]] + self.Faces['Bottom'][1::]
- else:
- faces['Front'].append(self.get_column('Front',2-i))
- faces['Right'].append([self.get_row('Bottom',0)[2-i]]+self.get_row('Right',i)[1::])
- faces['Left'].append(self.get_row('Left',i)[0:2]+[self.get_row('Top',2)[2-i]])
- faces['Top']=self.Faces['Top'][0:2] + [self.get_column('Right',0)]
- faces['Bottom']=[self.get_column('Left',2)]+self.Faces['Bottom'][1::]
- self.Faces = faces
- def Rotate_Up(self):
- ''' Rotate the whole cube 90 degrees up'''
- Faces = {i:[] for i in self.Faces}
- Faces['Top'] = self.Faces['Front']
- Faces['Front'] = self.Faces['Bottom']
- Faces['Back']=[ i[::-1] for i in self.Faces['Top'] ][::-1]
- Faces['Bottom'] =[ i[::-1] for i in self.Faces['Back']][::-1]
- for r in range(3):
- Faces['Right'].append( self.get_column('Right',r)[::-1] )
- Faces['Left'].append( self.get_column('Left',2-r))
- self.Faces = Faces
- def Rotate_Down(self):
- ''' Rotate the whole cube 90 degrees down'''
- for i in range(3):self.Rotate_Up()
- def Rotate_Right(self):
- ''' Rotate the whole cube 90 degrees to the right'''
- Faces = {i:[] for i in self.Faces}
- Faces['Right']=self.Faces['Front']
- Faces['Back']=self.Faces['Right']
- Faces['Left']=self.Faces['Back']
- Faces['Front']=self.Faces['Left']
- for r in range(3):
- Faces['Top'].append(self.get_column('Top',2-r))
- Faces['Bottom'].append(self.get_column('Bottom',r)[::-1])
- self.Faces=Faces
- def Rotate_Left(self):
- ''' Rotate the whole cube 90 degrees to the left'''
- Faces = {i:[] for i in self.Faces}
- Faces['Right'] = self.Faces['Back']
- Faces['Front'] = self.Faces['Right']
- Faces['Left'] = self.Faces['Front']
- Faces['Back'] = self.Faces['Left']
- for r in range(3):
- Faces['Top'].append(self.get_column('Top',r)[::-1])
- Faces['Bottom'].append(self.get_column('Bottom',2-r))
- self.Faces = Faces
- def U(self,clockwise=True):
- ''' Rotate the upper face of the cube clockwise or anticlockwise'''
- TempCube = Cube( self.Faces.values() )
- TempCube.Rotate_Down()
- TempCube.F(clockwise)
- TempCube.Rotate_Up()
- self.Faces = TempCube.Faces
- def D(self,clockwise=True):
- '''Rotate the bottom face of the cube clock/anti wise'''
- TempCube = Cube( self.Faces.values() )
- TempCube.Rotate_Up()
- TempCube.F(clockwise)
- TempCube.Rotate_Down()
- self.Faces = copy.copy(TempCube.Faces)
- def B(self,clockwise=True):
- '''Rotate the back face of the cube clock/anti wise'''
- TempCube = Cube( self.Faces.values() )
- TempCube.Rotate_Right()
- TempCube.Rotate_Right()
- TempCube.F(clockwise)
- TempCube.Rotate_Right()
- TempCube.Rotate_Right()
- self.Faces = TempCube.Faces
- def L(self,clockwise=True):
- ''' Rotate the left face of the cube clockwise or anticlockwise'''
- TempCube = Cube( self.Faces.values() )
- TempCube.Rotate_Right()
- TempCube.F(clockwise)
- TempCube.Rotate_Left()
- self.Faces = TempCube.Faces
- def R(self,clockwise=True):
- ''' Rotate the right face of the cube clockwise or anticlockwise'''
- TempCube = Cube( self.Faces.values() )
- TempCube.Rotate_Left()
- TempCube.F(clockwise)
- TempCube.Rotate_Right()
- self.Faces = TempCube.Faces
- def eeval(self,Move):
- Move += ' '
- Dir , Orientation = Move[0] , Move[1]!="'"
- if Dir == 'F':
- self.F(Orientation)
- elif Dir == 'R':
- self.R(Orientation)
- elif Dir == 'L':
- self.L(Orientation)
- elif Dir == 'U':
- self.U(Orientation)
- elif Dir == 'D':
- self.D(Orientation)
- elif Dir == 'B':
- self.B(Orientation)
- else:
- raise ValueError #Wrong Input!
- def Eval(self,moves):
- for move in moves:
- self.eeval(move)
- A=Cube(Array)
- A.D()
- A.D()
- A.U()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement