Skip to main content

Library for simulating rubik's cubes of different sizes (N x N), and for solving 2 x 2 and 3 x 3 cubes with kociemba algorithm.

Project description

Description

Library for Rubik's Cube manipulation. Creator and customizer for cubes of any size, and solver for 2x2 and 3x3 cubes (Herbert Kociemba's algorithms)

Usage

Creating instance of cube

You can create Rubik's cube, Pocket cube and NxN cube, where N means positive number.

import supercube

rubiks_cube = supercube.RubiksCube() # creates 3X3 cube
pocket_cube = supercube.PocketCube() # creates 2x2 cube
n_cube = supercube.NCube(7) # creates NxN cube (in this case N is 7)

rubiks_cube.show()
rubiks_cube.show(colors=True)

pocket_cube.show()
pocket_cube.show(colors=True)

n_cube.show()
n_cube.show(colors=True)

Output:

             ┌────────────┐
             │  1   2   3 │
             │  4   5   6 │
             │  7   8   9 │
┌────────────┼────────────┼────────────┬────────────┐
│ 10  11  12 │ 19  20  21 │ 28  29  30 │ 37  38  39 │ 
│ 13  14  15 │ 22  23  24 │ 31  32  33 │ 40  41  42 │ 
│ 16  17  18 │ 25  26  27 │ 34  35  36 │ 43  44  45 │ 
└────────────┼────────────┼────────────┴────────────┘
             │ 46  47  48 │
             │ 49  50  51 │
             │ 52  53  54 │
             └────────────┘
⬛⬛⬛ 🟪🟪🟪
⬛⬛⬛ 🟪🟪🟪
⬛⬛⬛ 🟪🟪🟪

🟧🟧🟧 🟩🟩🟩 🟥🟥🟥 🟦🟦🟦 
🟧🟧🟧 🟩🟩🟩 🟥🟥🟥 🟦🟦🟦 
🟧🟧🟧 🟩🟩🟩 🟥🟥🟥 🟦🟦🟦 

⬛⬛⬛ 🟨🟨🟨
⬛⬛⬛ 🟨🟨🟨
⬛⬛⬛ 🟨🟨🟨


         ┌────────┐
         │  1   2 │
         │  3   4 │
┌────────┼────────┼────────┬────────┐
│  5   6 │  9  10 │ 13  14 │ 17  18 │ 
│  7   8 │ 11  12 │ 15  16 │ 19  20 │ 
└────────┼────────┼────────┴────────┘
         │ 21  22 │
         │ 23  24 │
         └────────┘
⬛⬛ 🟪🟪
⬛⬛ 🟪🟪

🟧🟧 🟩🟩 🟥🟥 🟦🟦 
🟧🟧 🟩🟩 🟥🟥 🟦🟦 

⬛⬛ 🟨🟨
⬛⬛ 🟨🟨


                                    ┌───────────────────────────────────┐
                                    │   1    2    3    4    5    6    7 │
                                    │   8    9   10   11   12   13   14 │
                                    │  15   16   17   18   19   20   21 │
                                    │  22   23   24   25   26   27   28 │
                                    │  29   30   31   32   33   34   35 │
                                    │  36   37   38   39   40   41   42 │
                                    │  43   44   45   46   47   48   49 │
┌───────────────────────────────────┼───────────────────────────────────┼───────────────────────────────────┬───────────────────────────────────┐
│  50   51   52   53   54   55   56 │  99  100  101  102  103  104  105 │ 148  149  150  151  152  153  154 │ 197  198  199  200  201  202  203 │ 
│  57   58   59   60   61   62   63 │ 106  107  108  109  110  111  112 │ 155  156  157  158  159  160  161 │ 204  205  206  207  208  209  210 │ 
│  64   65   66   67   68   69   70 │ 113  114  115  116  117  118  119 │ 162  163  164  165  166  167  168 │ 211  212  213  214  215  216  217 │ 
│  71   72   73   74   75   76   77 │ 120  121  122  123  124  125  126 │ 169  170  171  172  173  174  175 │ 218  219  220  221  222  223  224 │ 
│  78   79   80   81   82   83   84 │ 127  128  129  130  131  132  133 │ 176  177  178  179  180  181  182 │ 225  226  227  228  229  230  231 │ 
│  85   86   87   88   89   90   91 │ 134  135  136  137  138  139  140 │ 183  184  185  186  187  188  189 │ 232  233  234  235  236  237  238 │ 
│  92   93   94   95   96   97   98 │ 141  142  143  144  145  146  147 │ 190  191  192  193  194  195  196 │ 239  240  241  242  243  244  245 │ 
└───────────────────────────────────┼───────────────────────────────────┼───────────────────────────────────┴───────────────────────────────────┘
                                    │ 246  247  248  249  250  251  252 │
                                    │ 253  254  255  256  257  258  259 │
                                    │ 260  261  262  263  264  265  266 │
                                    │ 267  268  269  270  271  272  273 │
                                    │ 274  275  276  277  278  279  280 │
                                    │ 281  282  283  284  285  286  287 │
                                    │ 288  289  290  291  292  293  294 │
                                    └───────────────────────────────────┘
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪
⬛⬛⬛⬛⬛⬛⬛ 🟪🟪🟪🟪🟪🟪🟪

🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 
🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 
🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 
🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 
🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 
🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 
🟧🟧🟧🟧🟧🟧🟧 🟩🟩🟩🟩🟩🟩🟩 🟥🟥🟥🟥🟥🟥🟥 🟦🟦🟦🟦🟦🟦🟦 

⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨
⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨
⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨
⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨
⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨
⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨
⬛⬛⬛⬛⬛⬛⬛ 🟨🟨🟨🟨🟨🟨🟨

You can also define your own state (number of elements of specific colors is not limited)

pocket_cube.define_state("RYWRRWOBGWOBWRRGGGGGGGGG")
pocket_cube.show()
pocket_cube.show(colors=True)

Output:

         ┌────────┐
         │ 13  21 │
         │  1  14 │
┌────────┼────────┼────────┬────────┐
│ 15   2 │  9   3 │  4  16 │ 11  12 │ 
│  5  17 │  6  18 │ 16  10 │ 12  12 │ 
└────────┼────────┼────────┴────────┘
         │ 12  12 │
         │ 12  12 │
         └────────┘
⬛⬛ 🟥🟨
⬛⬛ 🟪🟥

🟥🟪 🟩🟪 🟪🟥 🟩🟩 
🟧🟦 🟧🟦 🟥🟩 🟩🟩 

⬛⬛ 🟩🟩
⬛⬛ 🟩🟩

You can get your current state using method get_state, the return will be 3d array.

print(rubiks_cube.get_state())

Output:

[[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]], [[28, 29, 30], [31, 32, 33], [34, 35, 36]], [[37, 38, 39], [40, 41, 42], [43, 44, 45]], [[46, 47, 48], [49, 50, 51], [52, 53, 54]]]

Moves

Basic moves

You can move cube using method move

rubiks_cube.move("L B' F2 U R2 F L2 U B2 R2 F D L' D L' D2 R' U' B2 F' L R'")
rubiks_cube.show()

Output:

             ┌────────────┐
             │ 30   4  36 │
             │ 31   5  38 │
             │ 27  49  10 │
┌────────────┼────────────┼────────────┬────────────┐
│  3  24  48 │ 34  17   1 │ 39   2  54 │ 43  11  37 │ 
│ 26  14  40 │ 33  23  53 │ 44  32  51 │ 35  41  47 │ 
│ 46  22  52 │ 45  42   7 │ 19   8   9 │ 28   6  25 │ 
└────────────┼────────────┼────────────┴────────────┘
             │ 16  13  12 │
             │ 15  50  20 │
             │ 18  29  21 │
             └────────────┘

Advanced moves

Advanced moves are also supported. For each basic move there are:

  • wide moves (e.g. Fw, 4Fw', 5Fw2) - for wide move default value is 2 (when there is no number before move)
  • specific layer moves (e.g 3U, 5D')
n_cube.move("3Bw 3R2 D' 3Bw 2Rw' 3Fw R2 3Rw' 2B' 3R 2Fw2 3U2 L' 2Dw B")
n_cube.show()

Output:

                                    ┌───────────────────────────────────┐
                                    │  43   36  176  267  260  191  190 │
                                    │ 140  160  167  174  283  188  195 │
                                    │  54  279  278  277  276   40  101 │
                                    │ 126   23   24   25  271   72  102 │
                                    │  52   89   82   75   68   16  231 │
                                    │  51  107   65  158  157  156  155 │
                                    │  50  106   64  151  150  149  148 │
┌───────────────────────────────────┼───────────────────────────────────┼───────────────────────────────────┬───────────────────────────────────┐
│  99  183   29  169   15    8    1 │ 203   63  217   28   35   42   49 │ 105  112   78   46   45  287  147 │ 252  259   96   95  133   55   56 │ 
│  93  184  285  170  110  237  244 │ 289   62   30   27   34   41   48 │ 104  111   79   39   38  286  146 │ 251  258   59  125   61  139  281 │ 
│ 290  181   31   26   33  118  119 │ 162  132  117  124  131  138  145 │ 250   12  264   67  180  177   47 │ 103  185  215  222  229  236  243 │ 
│ 291   60   32   74   81   88  144 │ 249  223  116  123  130  137  193 │ 273  272  171  172  269  284   71 │ 224  186  214  221  228  235  242 │ 
│ 292  187   80  173  166  159  152 │  21  216  115  122  129  136  192 │ 266  265  178  179  262  208  143 │ 248  255  213  220  227  234  241 │ 
│ 293   58  257  207  206  205  153 │  14   13   87   11  275  282  142 │ 247  209  114  121  128  135   44 │ 100   86  212  219  226  233  240 │ 
│ 288   57  201  200  199  198  294 │ 196  189   94  175  168  161  154 │   7  202  113  120  127  134  141 │ 246  253  280   22  225   85   92 │ 
└───────────────────────────────────┼───────────────────────────────────┼───────────────────────────────────┴───────────────────────────────────┘
                                    │ 239  232  274  218  211  204  197 │
                                    │   6  254   20  256  163    9    2 │
                                    │   5  261   19  263  164   69   70 │
                                    │   4  268   18  270  165   76   77 │
                                    │   3   10   17   73   66   83   84 │
                                    │ 210   37  230  109  108   90   91 │
                                    │ 245  238  182   53  194   97   98 │
                                    └───────────────────────────────────┘

Scrambling

You can scramble cube by using method scramble(number_of_moves=30)(default value is 30)

print(rubiks_cube.scramble())
rubiks_cube.show()

Output:

B' F' D' B2 F' R' F' L B2 B2 U F D' D2 F' U2 F' R R U' B U L D2 U2 R' U' D D2 F2 
             ┌────────────┐
             │ 36  51  21 │
             │ 26   5  44 │
             │ 10  33  19 │
┌────────────┼────────────┼────────────┬────────────┐
│ 43  47   1 │ 39  40   7 │ 12  53  28 │  9  35  54 │ 
│ 42  14   8 │ 20  23  49 │ 17  32  38 │  2  41  13 │ 
│ 18  15  16 │ 52  24  48 │ 27   4   3 │ 37   6  46 │ 
└────────────┼────────────┼────────────┴────────────┘
             │ 45  31  34 │
             │ 22  50  11 │
             │ 25  29  30 │
             └────────────┘

You can also scramble big cubes

print(n_cube.scramble())
n_cube.show()

Output:

U' 2Bw 2Rw2 3Uw 3L 2B2 3Dw2 2B 2Bw' 2L 3Fw' D2 2Rw 3R 2Rw' 2Bw 2R 3Rw' 2Fw' 3Fw 3B2 L2 2F' 3R2 R' 3Uw2 2Bw 3F' B' B 
                                    ┌───────────────────────────────────┐
                                    │ 294  134  292   71  113    6  197 │
                                    │ 289  205   59   11   87  254  161 │
                                    │  54  181  215   32   68  138  119 │
                                    │  46  174  271   25  214  137  267 │
                                    │ 248  118  264  171  227  257  211 │
                                    │ 195   90  208   27   20   13  253 │
                                    │  98  104   84    4  217    2  148 │
┌───────────────────────────────────┼───────────────────────────────────┼───────────────────────────────────┬───────────────────────────────────┐
│ 196  244   29  102  143  287  246 │ 141   48  127  200   64  202   49 │ 105   97  168   95  162  204    7 │ 154  198   70  224  241   91  239 │ 
│ 140  237   89   88  110  282  281 │  93   41   30   60   65   62  293 │ 240  233   69  158  114  286  247 │ 142    9  128  125  185  184  183 │ 
│ 133  212  166   73  117  108   52 │  15  136  229   26   80  206    3 │ 201   61  164  165   82  275  199 │   5   83  262  222  178  177  176 │ 
│ 291  219  263   74   75  284   28 │ 151  186  179  123   67  223  120 │  77   76  220  172  277  170  249 │ 144   39  130  221  124  235  242 │ 
│ 243  236  180  116  213  132  152 │  21   10   17   24  276  230  182 │ 225  226  129  173   19  163  250 │ 145   40  131  269   66   34  290 │ 
│  14  160  261  109  157  139  106 │  63   37   16   23  285   86   57 │ 210  111   12  256  187  156  189 │ 232  188  234  207  255  209  153 │ 
│  43    8  260   53   47  100   50 │ 203   42   35  126  194   36  288 │ 245  238  231  218  274  146  147 │ 190  191  192  193  101  112   56 │ 
└───────────────────────────────────┼───────────────────────────────────┼───────────────────────────────────┴───────────────────────────────────┘
                                    │   1  149  150  169  280   55   92 │
                                    │  44  135   79   72   38   58   85 │
                                    │ 103  283  115   81  278  216   78 │
                                    │  22  268  122  270  228  121  175 │
                                    │  96  167   31   18   33  279   94 │
                                    │  51  107  159  272  265  258  251 │
                                    │  99  155   45  273  266  259  252 │
                                    └───────────────────────────────────┘

Solving

Only Rubik's cube and Pocket cube can be solved. Cubes are solved with Herbert Kociemba's algorithm.

print(rubiks_cube.scramble())
rubiks_cube.show()

print(rubiks_cube.solve())
rubiks_cube.show()

Output:

D U2 D B L D2 R D B' L D2 L2 D' U U U' F B' B B R' D U D' F2 B F2 U U B 
             ┌────────────┐
             │ 18   8  48 │
             │ 33   5  44 │
             │  9  17  39 │
┌────────────┼────────────┼────────────┬────────────┐
│ 25  40  21 │ 28  49  10 │  1  53  34 │ 27  20  46 │ 
│ 26  14   6 │ 29  23  35 │ 51  32  13 │ 42  41  47 │ 
│  3   4  12 │  7  22  43 │ 54  24  45 │ 16  38  30 │ 
└────────────┼────────────┼────────────┴────────────┘
             │ 19  15  36 │
             │ 11  50  31 │
             │ 37   2  52 │
             └────────────┘
U F' L B2 L2 B D L2 F' D2 F' B2 U' R2 D' B2 U F2 D2 L2 F2
             ┌────────────┐
             │  1   2   3 │
             │  4   5   6 │
             │  7   8   9 │
┌────────────┼────────────┼────────────┬────────────┐
│ 10  11  12 │ 19  20  21 │ 28  29  30 │ 37  38  39 │ 
│ 13  14  15 │ 22  23  24 │ 31  32  33 │ 40  41  42 │ 
│ 16  17  18 │ 25  26  27 │ 34  35  36 │ 43  44  45 │ 
└────────────┼────────────┼────────────┴────────────┘
             │ 46  47  48 │
             │ 49  50  51 │
             │ 52  53  54 │
             └────────────┘

Pocket cube solving procedure is the same.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

super_rubik_cube-1.0.0.tar.gz (5.1 MB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page