Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you represent a Rubik's Cube in code?

If you were developing software to solve a Rubik's Cube, how would you represent the cube?

like image 326
Mendokusai Avatar asked Feb 01 '09 04:02

Mendokusai


People also ask

How would you describe a Rubik's cube?

Rubik's Cube consists of 26 small cubes that rotate on a central axis; nine coloured cube faces, in three rows of three each, form each side of the cube. When the cube is twisted out of its original arrangement, the player must then return it to the original configuration—one among 43 quintillion possible ones.


2 Answers

This ACM Paper describes several alternative ways that it has used to represent a rubik's cube and compares them against eachother. Sadly, I don't have an account to get the full text but the description states:

Seven alternative representations of Rubik's Cube are presented and compared: a 3-by-3-by-3 array of 3-digit integers; a 6-by-3-by-3 array of literals; a 5-by-12 literal matrix; an ll-by-ll sparse literal matrix; a 54-element vector; a 4-dimension array; and a 3-by-3-by-3 nested array. APL functions are given for orientation moves and quarter-turns plus several useful tools for solving the cube.

Also, this RubiksCube.java file contains a pretty clean representation along with the relevant code for rotating the sections (if you are looking for actual code). It uses a cell and faces array.

like image 194
mmcdole Avatar answered Nov 09 '22 21:11

mmcdole


One way would be to focus on the visual appearance.

A cube has six faces and each face is a three-by-three array of squares. So

Color[][][] rubik = new Color[6][3][3]; 

Then each move is a method that permutes a specific set of colored squares.

like image 44
joel.neely Avatar answered Nov 09 '22 21:11

joel.neely