Skip to content

Instantly share code, notes, and snippets.

@immmdreza
Created July 9, 2020 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save immmdreza/f5bbd2f2db31a4321906168fb9a153bc to your computer and use it in GitHub Desktop.
Save immmdreza/f5bbd2f2db31a4321906168fb9a153bc to your computer and use it in GitHub Desktop.
from typing import List
from enum import Enum, auto
class RotationDir(Enum):
X_ROTATE = auto()
Y_ROTATE = auto()
Z_ROTATE = auto()
class Rotation:
def __init__(self, direction : RotationDir, times : int):
self.Direction = direction
self.Times = times
class FaceName(Enum):
FRONT = auto()
TOP = auto()
BOTTOM = auto()
LEFT = auto()
RIGHT = auto()
BACK = auto()
class Face:
def __init__(self, faceId : FaceName, strOnIt : str):
self.FaceId = faceId
self.StrOnIt = strOnIt
class Cube:
"""
Main cube class to create cube objects, then rotate them.
"""
def __init__(self, faces : List[Face]):
if faces.__len__() < 6 :
raise Exception('Cube should have 6 faces!')
elif faces.__len__() > 6:
faces = faces[:6]
self.Faces = faces
if not self.__ValidFaces():
raise Exception('Faces are not valid!')
def __ValidFaces(self) -> bool:
for x in FaceName:
i = 0
for y in self.Faces :
if y.FaceId == x:
i += 1
if i != 1 : return False
return True
def __FindFace(self, faceId : FaceName) -> Face:
for x in self.Faces:
if x.FaceId == faceId:
return x
def X_Rotate(self):
f = self.__FindFace(FaceName.FRONT)
t = self.__FindFace(FaceName.TOP)
ba = self.__FindFace(FaceName.BACK)
bt = self.__FindFace(FaceName.BOTTOM)
f.StrOnIt, t.StrOnIt, ba.StrOnIt, bt.StrOnIt = bt.StrOnIt, f.StrOnIt, t.StrOnIt, ba.StrOnIt
def Y_Rotate(self):
r = self.__FindFace(FaceName.RIGHT)
t = self.__FindFace(FaceName.TOP)
l = self.__FindFace(FaceName.LEFT)
bt = self.__FindFace(FaceName.BOTTOM)
r.StrOnIt, t.StrOnIt, l.StrOnIt, bt.StrOnIt = t.StrOnIt, l.StrOnIt, bt.StrOnIt, r.StrOnIt
def Z_Rotate(self):
r = self.__FindFace(FaceName.RIGHT)
f = self.__FindFace(FaceName.FRONT)
l = self.__FindFace(FaceName.LEFT)
ba = self.__FindFace(FaceName.BACK)
r.StrOnIt, f.StrOnIt, l.StrOnIt, ba.StrOnIt = f.StrOnIt, l.StrOnIt, ba.StrOnIt, r.StrOnIt
def Complex_Rotate(self, rotations : List[Rotation]) -> int:
"""Make several rotations in several directions depending on a list of Rotations
Returns(int) total rotation count
"""
i = 0
for r in rotations:
for _ in range(r.Times):
if r.Direction == RotationDir.X_ROTATE:
self.X_Rotate()
elif r.Direction == RotationDir.Y_ROTATE:
self.Y_Rotate()
else:
self.Z_Rotate()
i += 1
return i
def __str__(self):
result = str('')
for x in FaceName:
f = self.__FindFace(x)
result += "\n{} -> {}".format(f.FaceId, f.StrOnIt)
return result
#-------- End of classes definations --------#
c1 = Cube([Face(FaceName.TOP, "top"),
Face(FaceName.BACK, "back"),
Face(FaceName.LEFT, "left"),
Face(FaceName.FRONT, "front"),
Face(FaceName.BOTTOM, "bottom"),
Face(FaceName.RIGHT, "right")])
print(c1)
c = c1.Complex_Rotate(rotations= [Rotation(RotationDir.Z_ROTATE, 1),
Rotation(RotationDir.X_ROTATE, 1), Rotation(RotationDir.Y_ROTATE, 1)])
print(c1)
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment