cubeofrubik package

Submodules

cubeofrubik.RubiksCube module

class cubeofrubik.RubiksCube.RubiksCube

Bases: RubiksCubeInterface

Implementation of the Rubik’s Cube.

The cube is initialized with the standard colors configuration and in a solved state. The cube is represented by a dictionary of faces. The keys are the face names and the values are lists of lists of colors. The faces are: F, B, U, D, L, R. The colors are an enumeration: GREEN, BLUE, WHITE, YELLOW, ORANGE, RED.

Its exposed methods are:
  • draw(): draws the cube.

  • move(turns): moves the cube according to the turns string.

  • scramble(): scrambles the cube.

  • is_solved(): returns True if the cube is in a solved state.

  • get_color(position): returns the color of the cube at the given position.

  • find_position(*colors): returns the position of a piece with some given colors.

  • solve(): solves the cube and returns the steps with the solution.

  • get_size(): returns the size of the cube.

  • set_color(position, color): sets the color of the cube at the given position.

  • set_all_colors(color_dict): sets all the colors of the cube, using a dictionary.

  • is_solvable(): returns True if the cube is solvable.

draw(print_emojis=True) None

Draws the cube.

Parameters

print_emojis – if True, prints emojis; otherwise, prints letters representing colors

find_position(*colors: str) str

Finds position of a piece in the cube.

Parameters

*colors – 1 to 3 elements, with the colors representing piece to be found: ‘G’, ‘R’, ‘O’, ‘W’, ‘B’, ‘Y’

Returns

Position of that piece. It can be (center) F, B, U, D, R, L; (edge) FU, FD, FR, FL, UF, DF, RF, LF, BU, BD, BR, BL, UB, DB, RB, LB, RU, UR, UL, LU, LD, DL, DR, RD; (corner) FUR, FRU, URF, UFR, RUF, RFU, FUL, FLU, LUF, LFU, ULF, UFL, BUR, BRU, URB, UBR, RBU, RUB, BUL, BLU, LUB, LBU, UBL, ULB, FDL, FLD, LDF, LFD, DFL, DLF, FDR, FRD, RDF, RFD, DFR, DRF, BDL, BLD, LDB, LBD, DBL, DLB, BDR, BRD, RBD, RDB, DBR, DRB.

Raises

ValueError – if colors not found

get_color(position: str) str

Gets the color in a given position, according to Singmaster notation.

Positions, according to Singmaster notation:

          11 12 13
          14 U  16
          17 18 19
21 22 23  01 02 03 41 42 43  51 52 53
24 L  26  04 F  06 44 R  46  54 B  56
27 28 29  07 08 09 47 48 49  57 58 59
          31 32 33
          34 D  36
          37 38 39
Parameters

position – any valid position; all valid positions are listed above

Returns

Color contained in the position. It can be ‘G’, ‘R’, ‘O’, ‘W’, ‘B’ or ‘Y’

get_size() int

Returns the size of the cube.

is_solvable() bool

Checks if the cube is solvable.

is_solved() bool

Checks if the cube is solved.

move(turns: str) None

Makes a sequence of turns.

Allowed turns:
     F: Front;  B: Back;  U: Up;  D: Down;  L: Left;  R: Right;
     f: Front 2 layers;  b: Back 2 layers;
     u: Up 2 layers;  d: Down 2 layers;
     l: Left 2 layers;  r: Right 2 layers;
     x: rotation;  y: rotation;  z: rotation;
     M: Middle;  E: Equator;  S: Standing
Allowed modifiers:
     p: counterclockwise; ′: counterclockwise; 2: make 2 turns
Parameters

turns – sequence of letters denoting the movements/turns to be executed, according to Singmaster notation. If followed by a ‘p’ or a prime symbol (′), turn it anticlockwise; otherwise, turn it clockwise. If followed by a ‘2’, execute the operation twice.

Raises

ValueError – Unrecognized move is requested

Examples

>>> valid_turns = ["F′", "", "FFF", "FpF′", "x2l2lll′", "FBUDLRLLpL′L2fulxMMEyzSS′S2"]
>>> invalid_turns = ["F'", "A", "m", "Fpp", "pF", "S22", "S3" "FfuUY", "F ", "F2′"]
>>> for turn in valid_turns:
...     cube.move(turn)
scramble(steps=20, wide_moves=False, slice_moves=False, cube_rotations=False) str

Scrambles the cube.

Parameters
  • steps – number of random moves to be executed on the cube.

  • wide_moves – allow moves f-b-u-d-l-r to be executed.

  • slice_moves – allow moves M-E-S to be executed.

  • cube_rotations – allow rotations x-y-z to be executed.

Returns

Sequence of scrambles executed on the cube

set_all_colors(color_dict: dict) None

Sets all colors on the cube.

Parameters

color_dict

dictionary that sets colors on all 54 cube positions;

keys: 54 (6 + 6*8) positions, according to Singmaster notation:

’F’, ‘B’, ‘U’, ‘D’, ‘L’, ‘R’, ‘01’, …, ‘04’, ‘06’, …, ‘09’,’11’, …, ‘59’

values: one of the 6 standard colors on the cube:

’G’, ‘R’, ‘O’, ‘W’, ‘B’, ‘Y’

set_color(position: str, color: str) None

Sets a color on the cube.

Parameters
  • position – position on the cube according to Singmaster notation. The position can be ‘F’, ‘B’, ‘U’, ‘D’, ‘L’, ‘R’, ‘01’, …, ‘04’, ‘06’, …, ‘09’,’11’, …, or ‘59’

  • color – one of the 6 standard colors on the cube. The color can be ‘G’, ‘R’, ‘O’, ‘W’, ‘B’, or ‘Y’

solve(method='kociemba', change_state=True) str

Solves the cube.

Parameters
  • method – method to be used to solve the cube. It can be ‘lbl’ or ‘kociemba’

  • change_state – if True, changes the cube state to the solved state. Otherwise, just returns the solution.

Returns

list of moves that solve the cube; None if the cube is not solvable

cubeofrubik.RubiksCubeAlgorithms module

class cubeofrubik.RubiksCubeAlgorithms.Kociemba(original_cube: RubiksCubeInterface)

Bases: RubiksCubeInterface

Implementation of Herbert Kociemba’s two-phase algorithm.

It uses muodov’s Kociemba package, with additional checks, and using the cubeofrubik’s interface.

Example

>>> from cubeofrubik import RubiksCube
>>> cube = RubiksCube()
>>> cube.scramble()
>>> solution = Kociemba(cube).solve()
solve() str

Solves the cube using the Kociemba algorithm.

This method doesn’t change the state of the original_cube passed to the Kociemba constructor, as it just calls getter methods it exposes.

Returns

The moves to solve the cube, using Kociemba algorithm.

class cubeofrubik.RubiksCubeAlgorithms.LayerByLayer(original_cube: RubiksCubeInterface)

Bases: RubiksCubeInterface

Implementation of the layer-by-layer solving algorithm.

Example

>>> from cubeofrubik import RubiksCube
>>> cube = RubiksCube()
>>> cube.scramble()
>>> solution = LayerByLayer(cube).solve()
solve() str

Solves the cube using the layer-by-layer algorithm.

This method doesn’t change the state of the original_cube passed to the LayerByLayer constructor, as it operates only on an internal copy of it.

Returns

The moves to solve the cube, using LBL algorithm.

cubeofrubik.RubiksCubeInterface module

class cubeofrubik.RubiksCubeInterface.RubiksCubeInterface

Bases: object

Interface of Rubik’s Cube Model.

draw(print_emojis: bool) None

Draws the cube.

Parameters

print_emojis – if True, prints emojis; otherwise, prints letters representing colors

find_position(*colors: str) str

Finds position of a piece in the cube.

Parameters

*colors – 1 to 3 elements, with the colors representing piece to be found: ‘G’, ‘R’, ‘O’, ‘W’, ‘B’, ‘Y’

Returns

Position of that piece. It can be (center) F, B, U, D, R, L; (edge) FU, FD, FR, FL, UF, DF, RF, LF, BU, BD, BR, BL, UB, DB, RB, LB, RU, UR, UL, LU, LD, DL, DR, RD; (corner) FUR, FRU, URF, UFR, RUF, RFU, FUL, FLU, LUF, LFU, ULF, UFL, BUR, BRU, URB, UBR, RBU, RUB, BUL, BLU, LUB, LBU, UBL, ULB, FDL, FLD, LDF, LFD, DFL, DLF, FDR, FRD, RDF, RFD, DFR, DRF, BDL, BLD, LDB, LBD, DBL, DLB, BDR, BRD, RBD, RDB, DBR, DRB.

Raises

ValueError – if colors not found

get_color(position: str) str

Gets the color in a given position, according to Singmaster notation.

Positions, according to Singmaster notation:

          11 12 13
          14 U  16
          17 18 19
21 22 23  01 02 03 41 42 43  51 52 53
24 L  26  04 F  06 44 R  46  54 B  56
27 28 29  07 08 09 47 48 49  57 58 59
          31 32 33
          34 D  36
          37 38 39
Parameters

position – any valid position; all valid positions are listed above

Returns

Color contained in the position. It can be ‘G’, ‘R’, ‘O’, ‘W’, ‘B’ or ‘Y’

get_size() int

Returns the size of the cube.

is_solvable() bool

Checks if the cube is solvable.

is_solved() bool

Checks if the cube is solved.

move(turns: str) None

Makes a sequence of turns.

Allowed turns:
     F: Front;  B: Back;  U: Up;  D: Down;  L: Left;  R: Right;
     f: Front 2 layers;  b: Back 2 layers;
     u: Up 2 layers;  d: Down 2 layers;
     l: Left 2 layers;  r: Right 2 layers;
     x: rotation;  y: rotation;  z: rotation;
     M: Middle;  E: Equator;  S: Standing
Allowed modifiers:
     p: counterclockwise; ′: counterclockwise; 2: make 2 turns
Parameters

turns – sequence of letters denoting the movements/turns to be executed, according to Singmaster notation. If followed by a ‘p’ or a prime symbol (′), turn it anticlockwise; otherwise, turn it clockwise. If followed by a ‘2’, execute the operation twice.

Raises

ValueError – Unrecognized move is requested

Examples

>>> valid_turns = ["F′", "", "FFF", "FpF′", "x2l2lll′", "FBUDLRLLpL′L2fulxMMEyzSS′S2"]
>>> invalid_turns = ["F'", "A", "m", "Fpp", "pF", "S22", "S3" "FfuUY", "F ", "F2′"]
>>> for turn in valid_turns:
...     cube.move(turn)
scramble(steps: int, wide_moves: bool, slice_moves: bool, cube_rotations: bool) str

Scrambles the cube.

Parameters
  • steps – number of random moves to be executed on the cube.

  • wide_moves – allow moves f-b-u-d-l-r to be executed.

  • slice_moves – allow moves M-E-S to be executed.

  • cube_rotations – allow rotations x-y-z to be executed.

Returns

Sequence of scrambles executed on the cube

set_all_colors(color_dict: dict) None

Sets all colors on the cube.

Parameters

color_dict

dictionary that sets colors on all 54 cube positions;

keys: 54 (6 + 6*8) positions, according to Singmaster notation:

’F’, ‘B’, ‘U’, ‘D’, ‘L’, ‘R’, ‘01’, …, ‘04’, ‘06’, …, ‘09’,’11’, …, ‘59’

values: one of the 6 standard colors on the cube:

’G’, ‘R’, ‘O’, ‘W’, ‘B’, ‘Y’

set_color(position: str, color: str) None

Sets a color on the cube.

Parameters
  • position – position on the cube according to Singmaster notation. The position can be ‘F’, ‘B’, ‘U’, ‘D’, ‘L’, ‘R’, ‘01’, …, ‘04’, ‘06’, …, ‘09’,’11’, …, or ‘59’

  • color – one of the 6 standard colors on the cube. The color can be ‘G’, ‘R’, ‘O’, ‘W’, ‘B’, or ‘Y’

solve(method: str, change_state: bool) str

Solves the cube.

Parameters
  • method – method to be used to solve the cube. It can be ‘lbl’ or ‘kociemba’

  • change_state – if True, changes the cube state to the solved state. Otherwise, just returns the solution.

Returns

list of moves that solve the cube; None if the cube is not solvable

Module contents