Skip to content

MariaMozgunova/rubiks_cube

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT License LinkedIn

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. How I was struggling to implement visualization
  5. What's next?
  6. License
  7. Contact
  8. Acknowledgments

About The Project

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]

(back to top)

Explaining the concepts

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. facelet and cubie

To input the state of the Rubik's Cube into the program, you give it a 6-element array of 9 uints. 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'.

explain x, y, z rotations and their primes

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.

(back to top)

Getting Started

To use this Rubik's Cube solver, follow the simple steps below.

Installation

  1. Clone the repo git clone https://github.com/MariaMozgunova/rubiks_cube.git
  2. Include the Rubik's Cube solver into your code #include "rubiks_cube/cube.h"
  3. Include the visualization into your code #include "rubiks_cube/graphics.h"

(back to top)

Usage

See main.cpp for an example of usage.

(back to top)

How I was struggling to implement visualization

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.

spin single triangle

After that, I was curious whether I could rotate multiple triangles simultaneously. I built four squares out of eight triangles.

rotate multiple figures at once

Subsequently, I started building each facelet cubie by cubie.

first two squares

two opposite sides

only up and down left

got a skeleton for the cube

The next step was to color the cube.

color the cube

Rotating the whole cube worked alright.

rotate the whole cube

However, trying to rotate the particular facelet was a disaster.

cubie is missing

After dealing with the above issue, I managed to make visualization into my code. Watch the video solving the cube below!

visualization of the entire solution

I also managed to decrease by two the number of moves required to solve the Rubik's Cube compared to my initial implementation.

What's next?

I should consider filling in the cube as now you can see all the insides while it is rotating.

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Maria Mozgunova - Twitter - @MariaMozgunova - mariiamozgunova@gmail.com

Project Link: https://github.com/MariaMozgunova/rubiks_cube

(back to top)

Acknowledgments

(back to top)