Skip to content

A Rubik's cube solver that uses the Webcam to solve your cube!

Notifications You must be signed in to change notification settings

thatc0der/3x3x3-Rubiks-Cube-Solver

Repository files navigation

3x3x3-Rubiks-Cube-Solver

This project took around 7 months to get to fully working. I developed the solving algorithm for the cube which gets you an average of around 25 move solution. The goal was to write a solver that could solve the cube in less than 28 moves every time. The image processing took around a month to get working and can be improved for better detection.

System requirements:

Linux, Mac, or Windows
Java 1.6 and higher
IDE for Java Eclipse Luna or higher 1GB of disk space

How to install OpenCV in Eclipse

  1. Navigate to OpenCV

  2. Click download

  3. Extract it
    If Linux:
           Navigate in Terminal to where you extacted it and run this $ mkdir build && cd build && cmake .. && make
           This will build OpenCV so that you can use it. This will take a while... 10-20 minutes to complete building
           Once complete jump move to step 4.
    If Windows:
           OpenCV the file where you downloaded OpenCV run the .exe.
           You should see a build folder
           If this file is there you can move to step 4.
    If Mac:
           I found this tutorial on how to install OpenCV on mac and it worked for me.
           Once complete you should end up with a build folder
           You can now move to step 4.

  4. Run Eclipse
           Look for Windows in the toolbar on the top of Eclipse then choose Preferences
           Scroll down to Java then click User Libraries
           As shown. NOTE you shouldn't see opencv-3.3.0 We are going to make that. Also JavaCV is irrelevant.
    here
           Then press New and name it OpenCV-3.3.0 or something like that.

  5. Add OpenCV jar to User Library
           After pressing New make sure you have the library you created selected.
           Then press Add External Jars
           Then navigate into your OpenCV folder to the build/bin/ folder.
           You will find opencv-330.jar. Select it and add it to your library

    It your library should look like this

  6. Add Native Library Location
           Now we have to add the native OpenCV libraries to our OpenCV library.
           Click on Native Libary location and the libaries are located it in your dowloaded OpenCV folder in /build/lib
           Congrats you now have OpenCV configured and ready!
           It should look something like this

Clone and Setup the Project

Make sure you have Eclipse open.
We are going to clone the github repo inside Eclipse.

  1. Clone the repo:
           Right-click inside Package Explorer
           Select Import
           Click the Git folder
           Then click Projects from Git
           Click Clone URI
           Enter the URI : https://github.com/HaginCodes/3x3x3-Rubiks-Cube-Solver.git
           Make sure you are signed in with GitHub
           Press Next until you see 3 options listing:
           ..1. Import Existing Eclipse Projects
           Make sure this is selected and press Finish
           This will build the project.
           This could take a minute or two finish building

  2. Add your OpenCV library:
           Right-click on the project name
           Select Build Path and select Add Libraries
           Then click User Library
           Select your OpenCV library
           Click Finish

Running the program to solve your rubik's cube.

You are now ready to run the program. When running the program make sure to be running MainFrame.java You can now click Run and a window showing the camera will pop up. This is crucial. When running the program good lighting is helps success a lot. Avoid having lighting changes between pictures.

To take a picture of a side press SPACE.
To end the program press X.
When you scan the cube you follow this order.
TOP, LEFT, FRONT, RIGHT, BACK BOTTOM
Here is a sample video showing how it should be done.

Youtube video

If you follow the video and track how I move the cube to take photos of it in the video exactly you will get a solution to your cube. If you get a successful solution you will see this printed in the console:

 your cube: 

       OYG
       WYO
       YWG
   YBB RRR WWW OOB
   GBR BRB YGR YOR
   RBW BGO BOY OGG
       RWW
       OWG
       YYG
  Calculating...
       WWW
       WWW
       WWW
   OOO GGG RRR BBB
   OOO GGG RRR BBB
   OOO GGG RRR BBB
       YYY
       YYY
       YYY
  Your solution :) 
  R2 U' R B2 U' R2 U' B R2 U' L U R2 U' D L D B D B' D' L2 F2 L F2 R' D2 R 
  Number of moves: 28

On top is your scrambled cube and make sure you are holding it in that orientation before you try applying the solution

The top of the flat cube is the first 3 rows of the cube. That is the TOP.

 OYG
 WYO
 YWG 

The left side of the cube is most left 3 rows in the middle chunk. That is the LEFT

 YBB
 GBR
 RBW 

The front side is:

 RRR
 BRB
 BGO 

And so on.

Here is an image showing the correct orientation.
cube image example

It matches the cube printed in the console

     OYG
     WYO
     YWG
 YBB RRR WWW OOB
 GBR BRB YGR YOR
 RBW BGO BOY OGG
     RWW
     OWG
     YYG

New to cube Notation? Don't understand what this R2 U' R B2 U' R2 U' means? This website explains it very well.

Conclusion

Congrats! You now hopefully have been able to solve your Rubik's cube!
If you are having difficulties to let me know and I will be happy to help out.

Want to contribute to the project?

Feel free to add on to this solver, help with the success rate of the image processing, make the GUI better. I am willing to work with you.

Special thanks to Daniel Walton for inspiring me to create the image processing part and helping me understand how to go about it and to David Gilday, who helped me understand the concepts in writing my own solving algorithm and the source of my inspiration to do this.

About

A Rubik's cube solver that uses the Webcam to solve your cube!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages