Table of Contents
This project solves the Rubik's Cube.
The following actions are possible:
- Read and write to a file the state of the Rubik's Cube
- Check whether the current configuration of the Rubik's Cube is solvable or not
- Output to any
std::ostream
the state of the Rubik's Cube - Spin the facelets of the Rubik's Cube
- Generate the solvable state of the Rubik's Cube
- Solve the Rubik's Cube [now with the visualization]
Several efficient algorithms to solve Rubik's Cube are already developed. In the project, I implemented the heuristic approach.
First things first, the facelet and the cubie are shown in the picture below.
To input the state of the Rubik's Cube into the program, you give it a 6-element array of 9 uint
s. Specify the colors as follow: BLUE
=0, ORANGE
=1, GREEN
=2, RED
=3, WHITE
=4, YELLOW
=5.
The program outputs the state of the Rubik's Cube as the 6 strings of 9 elements. To decode the string use the mapping: B
=BLUE
; O
=ORANGE
; G
=GREEN
; R
=RED
; W
=WHITE
;Y
=YELLOW
;
To check the correctness of the Rubik's Cube's state program validates four conditions: Edge Permutation Parity, Edge Orientation Parity, Corner Permutation Parity, Corner Orientation Parity as specified in this paper. You can check if the cube is solvable using void RubiksCube::validate_cube()
method.
To output the cube's current state use void RubiksCube::print_cube(std::ostream &out = std::cout)
function.
To arbitrarily rotate the cube's facelets use void RubiksCube::rotate(uint8_t cmd)
. The argument cmd
can take 18 values:
0
=rotate the upper facelet clockwise,
1
=rotate the upper facelet counterclockwise,
2
=rotate the down facelet clockwise,
3
=rotate the down facelet counterclockwise,
4
=rotate the right facelet clockwise,
5
=rotate the right facelet counterclockwise,
6
=rotate the left facelet clockwise,
7
=rotate the left facelet counterclockwise,
8
=rotate the front facelet clockwise,
9
=rotate the front facelet counterclockwise,
10
=rotate the back facelet clockwise,
11
=rotate the back facelet counterclockwise,
12
=x
(reference the picture below),
13
=x'
,
14
=y
,
15
=y'
,
16
=z
,
17
=z'
.
You can shuffle the cube with the void RubiksCube::shuffle()
function. What it does is generate some number of rotations.
You can get the sequence of rotations to solve the cube with the void RubiksCube::solve()
function.
To use this Rubik's Cube solver, follow the simple steps below.
- Clone the repo
git clone https://github.com/MariaMozgunova/rubiks_cube.git
- Include the Rubik's Cube solver into your code
#include "rubiks_cube/cube.h"
- Include the visualization into your code
#include "rubiks_cube/graphics.h"
See main.cpp
for an example of usage.
First of all, I decided to use GLUT
to visualize my Solver. However, when a lot of my attempts to install it failed, I decided that something was going wrong. It is then that I discovered you could use vcpkg
to easily install any package with VisualStudio.
Next, I realized that the modern way to work with graphics is through glad
and glfw3
. Thus, I installed glad
and glfw3
with vcpkg
... and here began my journey of exploring OpenGL. Btw, I can recommend Learn OpenGL tutorial for those who are learning how to work with glad
and glfw3
.
First things first, I tried to draw a square composed of two triangles and spin one of them.
After that, I was curious whether I could rotate multiple triangles simultaneously. I built four squares out of eight triangles.
Subsequently, I started building each facelet cubie by cubie.
The next step was to color the cube.
Rotating the whole cube worked alright.
However, trying to rotate the particular facelet was a disaster.
After dealing with the above issue, I managed to make visualization into my code. Watch the video solving the cube below!
I also managed to decrease by two the number of moves required to solve the Rubik's Cube compared to my initial implementation.
I should consider filling in the cube as now you can see all the insides while it is rotating.
Distributed under the MIT License. See LICENSE
for more information.
Maria Mozgunova - Twitter - @MariaMozgunova - [email protected]
Project Link: https://github.com/MariaMozgunova/rubiks_cube