11"""Rubik Cube implementation"""
2- from typing import Dict , List , Tuple , Union
2+ from typing import Dict , List , Optional , Tuple , Union
33import random
44import numpy as np
55from magiccube .cube_base import Color , CubeException , Face
@@ -12,14 +12,16 @@ class Cube:
1212 """Rubik Cube implementation"""
1313
1414 __slots__ = ("size" , "_store_history" , "_cube_face_indexes" , "_cube_piece_indexes" ,
15- "_cube_piece_indexes_inv" , "cube " , "_history" )
15+ "_cube_piece_indexes_inv" , "_cube " , "_history" )
1616
17- def __init__ (self , size : int = 3 , state = None , hist = True ):
17+ def __init__ (self , size : int = 3 , state : Optional [ str ] = None , hist : Optional [ bool ] = True ):
1818
1919 if size <= 1 :
2020 raise CubeException ("Cube size must be >= 2" )
2121
2222 self .size = size
23+ """Cube size"""
24+
2325 self ._store_history = hist
2426
2527 # record the indexes of every cube face
@@ -68,7 +70,7 @@ def reset(self):
6870 for y in range (self .size )]
6971 for z in range (self .size )
7072 ]
71- self .cube = np .array (initial_cube , dtype = np .object_ )
73+ self ._cube = np .array (initial_cube , dtype = np .object_ )
7274 self ._history = []
7375
7476 def set (self , image : str ):
@@ -127,7 +129,7 @@ def set(self, image: str):
127129 _z = self .size - 1
128130 self .get_piece ((_x , _y , _z )).set_piece_color (2 , color )
129131
130- def get (self , face_order = None ):
132+ def get (self , face_order : Optional [ List [ Face ]] = None ):
131133 """
132134 Get the cube state as a string with the colors of every cube face in the following order: UP, LEFT, FRONT, RIGHT, BACK, DOWN.
133135
@@ -142,15 +144,15 @@ def get(self, face_order=None):
142144 res += self .get_face_flat (face )
143145 return "" .join ([x .name for x in res ])
144146
145- def scramble (self , num_steps : int = 50 , wide = None ) -> List [CubeMove ]:
147+ def scramble (self , num_steps : int = 50 , wide : Optional [ bool ] = None ) -> List [CubeMove ]:
146148 """Scramble the cube with random moves.
147149 By default scramble only uses wide moves to cubes with size >=4."""
148150
149151 movements = self .generate_random_moves (num_steps = num_steps , wide = wide )
150152 self .rotate (movements )
151153 return movements
152154
153- def generate_random_moves (self , num_steps : int = 50 , wide = None ) -> List [CubeMove ]:
155+ def generate_random_moves (self , num_steps : int = 50 , wide : Optional [ bool ] = None ) -> List [CubeMove ]:
154156 """Generate a list of random moves (but don't apply them).
155157 By default scramble only uses wide moves to cubes with size >=4."""
156158
@@ -187,7 +189,7 @@ def get_face(self, face: Face) -> List[List[Color]]:
187189 face_indexes = self ._cube_face_indexes [face .value ]
188190 res = []
189191 for line in face_indexes :
190- line_color = [self .cube [index ].get_piece_color (
192+ line_color = [self ._cube [index ].get_piece_color (
191193 face .get_axis ()) for index in line ]
192194 res .append (line_color )
193195 return res
@@ -205,15 +207,15 @@ def get_all_faces(self) -> Dict[Face, List[List[Color]]]:
205207
206208 def get_piece (self , coordinates : Coordinates ) -> CubePiece :
207209 """Get the CubePiece at a given coordinate"""
208- return self .cube [coordinates ]
210+ return self ._cube [coordinates ]
209211
210212 def get_all_pieces (self ) -> Dict [Coordinates , CubePiece ]:
211213 """Return a dictionary of coordinates:CubePiece"""
212- res = [self .cube [x ] for x in self ._cube_piece_indexes ]
214+ res = [self ._cube [x ] for x in self ._cube_piece_indexes ]
213215
214216 res = {
215217 (xi , yi , zi ): piece
216- for xi , x in enumerate (self .cube )
218+ for xi , x in enumerate (self ._cube )
217219 for yi , y in enumerate (x )
218220 for zi , piece in enumerate (y )
219221 if xi == 0 or xi == self .size - 1
@@ -278,10 +280,10 @@ def _rotate_once(self, move: CubeMove) -> None:
278280 slice (None ) if i != axis else slices for i in range (3 ))
279281 rotation_axes = tuple (i for i in range (3 ) if i != axis )
280282
281- plane = self .cube [rotation_plane ]
283+ plane = self ._cube [rotation_plane ]
282284 rotated_plane = np .rot90 (plane , direction , axes = rotation_axes )
283- self .cube [rotation_plane ] = rotated_plane
284- for piece in self .cube [rotation_plane ].flatten ():
285+ self ._cube [rotation_plane ] = rotated_plane
286+ for piece in self ._cube [rotation_plane ].flatten ():
285287 if piece is not None :
286288 piece .rotate_piece (axis )
287289
@@ -365,7 +367,7 @@ def undo(self, num_moves: int = 1) -> None:
365367 self ._history .pop ()
366368
367369 def __repr__ (self ):
368- return str (self .cube )
370+ return str (self ._cube )
369371
370372 def __str__ (self ):
371373 printer = CubePrintStr (self )
0 commit comments