A Rubik’s cube scripting example
I have recently came to an idea of a Rubic’s cube as a nice scripting example.
Rubik® and Rubik’s Cube® are registered trademarks throughout the world of Seven Towns Limited. Seven Towns Limited is the exclusive licensee of copyright in the Rubik’s Cube puzzle. This post regards to Unity3d cube mechanism scripting example, not the game itself.
My idea of scripting the cube movement is based on selecting the relevant rows of cubes, placing them in rotation container, rotating container and putting it back from the container to the cube.
In my example I made use of the fact that the same script can run cubes of different sizes and you can also set it here to any integer starting from 2.
People cope with with Rubic’s with at various levels of ease and the script lets them to train on small sizes to slowly work out the algoritmh and then move to bigger sizes.
After setting the size you need to reset the cube in order to apply the new size.
You can adjust the view of the cube by dragging the mouse over the application window and scrolling the mouse in order to zoom in/out.
All the meshes are created dynamically and uv-mapped, and there is a possibility to add to the difficulty level by texturing the individual tiles or the whole sides of the cube. When the tiles are individually mappped (with arrows in this case), then – for the inside tiles – not only the color but also the rotation matters. When the sides are mappped with picture, then – for the inside tiles – not only the color and rotation, but also the strict position matters.
The settings and textures can be customized by image and xml data held in separate data folder.
In the web version there are also two php scripts for communication with mysql databases for reading and storing the scores.
In the standalone versions the scores are stored on the local computer in PlayerPrefs.
The above download is obviously targeted at Unity3d enthusiast interested in this game setup. In order to try to play the game just click the picture on the top so that opens the link to the WebPlayer or download any of the standalone versions below:
The deployments contains a customizable xml file pointing to the texture. By the modification of this you can easily put your own logo or custom graphics on the cube.
I have also utilised an interesting Unity3d ability to pick a localization based on the user’s system language with the Application.systemLanguage method. The default GUI language is English, but on the detection of Polish, the application reads that from the attached XML file.
The script could of course be also applied to other geometric regular and non-regular forms like pyramids.
This is a cube mechanism script. A far more challenging would be to create a general solving algoritmh for n-cube, that would be able to find the optimal solution in minimal number of steps. I have found some considerations on that subject here: http://kociemba.org/cube.htm
and there: http://www.wrongway.org