Python rubik's cube solver

Overview

py-rubik_solver

Python solver for a rubik's cube

This program makes a 3D representation of a rubiks cube and solves it step by step.

solving the cube image

Usage

To use this program you need to execute the following commands

  • For 3D visualizations:

    python visualizer.py

  • For statistics:

    python stats.py

Requirements

To use this program you need to install python 3.8.10 or later (although it will probably work on python 3.7) You will also need a recent version of numpy and vpython 7 or later, those can be installed with:

pip install numpy vpython

Implementation

This project is separated in different files, each implementing a different functionality. The content and functionality of each of these files is the following:

configs.py

This file contains general configuration parameters mostly related to the visual representation of the cube:

  • The default colors
  • The number of fps
  • The time taken to reproduce each move
  • Time to wait between moves
  • Speed factor

cube.py

This file contains the Cube class, which implements a data structure for storing the pieces of the cube and some functions for rotating the faces of the cube. It also implements the possibility to shuffle the cube on creation and the possibility of recording a list of moves made in the cube, this is used for generating a solution.

The main functions implemented in this class are:

  • move(move, n=1, record=True): where move should be a string representing the face to move and n is the number of 90 degree rotations to perform (2 is half turn and 3 or -1 is a turn to the other side). The codes used for the move are:

    • "U", "F", "R", "B", "L", "D" for individual faces.
    • "UD", "FB", "RL" for the middle faces.
    • "UU", "FF", "RR" for rotations of the whole cube along this axis.
  • rotate(axis, n=1): this has the same effect as using move with "UU", "FF", "RR" but these moves are never recorded.

  • is_solved(): checks whether the cube equals the solved cube. Keep in mind that this function will return False even if the cube is solved but faces a different way.

  • copy(): creates a deep_copy of the cube. The copy is completely independent of the original cube.

cube_3d.py

This file implements the Cube3D class, which directly inherits from the Cube class. This class overrides the __init__ and move functions to first create all the cubes necessary to represent the rubiks cube in 3D and then animate them each time any face is moved.

cube_solver.py

This file implements the CubeSolver class, which acts as an abstract class for all the other solving algorithms. It only takes care of taking some measures for statistics.

simple_solver.py

This is the first solving algorithm implemented, it's the usual beginer algorithm for anyone learning how to solve the rubiks cube. It's implemented on a really naive way, and it's far from optimal in terms of the number of steps of the solution. It was just a proof of concept and my goal is to implement a better, more efficient version of this class in the future.

In my personal computer this algorithm takes 1.78 ms on average to compute a solution, and the solutions have 205.6 steps on average. Again these results are far from good, but this was just a proof of concept.

The process of the algorithm is separated in different steps, which are:

  • solve_first_cross: solves the cross on the UP face
  • solve_first_corners: solves the corners on the UP face
  • solve_second_row: solves the second "crown" or the second row
  • solve_second_cross: creates a cross on the DOWN face
  • orientate_2nd_cross: positions correctly the pieces inside the cross on the DOWN face
  • solve_second_corners: positions correctly the corners in the DOWN face
  • orientate_2nd_corners: rotates correctly the corners in the DOWN face
  • reorient_cube: rotates the whole cube so that the UP face is facing up and the FRONT face if facing front

stats.py

This file is used to compute some statistics of the cube solutions. At this point this file is used to compute:

  • The average time taken to generate a solution
  • The average number of steps of the generated solutions
  • Some data of the solving process

Keep in mind the data computed will probably change in the future.

util.py

In this file we store different lists and dictionaries used in the project such as a solved cube structure, a list of the directions, a function for generating random moves, ...

visualizer.py

This file is used to launch a 3D representation of the solving process of the cube. It also contains a function to check the progress of the solving algorithm.

Notes

In the future I'm planing to make more solving algorithms as well as an implementation for a physical robot that solves a given cube.

Use this code as you wish, just let me know if you do, I'll love to hear what you are up to!

If you have any doubts/comments/suggestions/anything please let my know via email at [email protected] or at the email in my profile.

You might also like...
Textboxes_plusplus implementation with Tensorflow (python)

TextBoxes++-TensorFlow TextBoxes++ re-implementation using tensorflow. This project is greatly inspired by slim project And many functions are modifie

Textboxes implementation with Tensorflow (python)

tb_tensorflow A python implementation of TextBoxes Dependencies TensorFlow r1.0 OpenCV2 Code from Chaoyue Wang 03/09/2017 Update: 1.Debugging optimize

Textboxes : Image Text Detection Model : python package  (tensorflow)
Textboxes : Image Text Detection Model : python package (tensorflow)

shinTB Abstract A python package for use Textboxes : Image Text Detection Model implemented by tensorflow, cv2 Textboxes Paper Review in Korean (My Bl

python ocr using tesseract/ with EAST opencv detector

pytextractor python ocr using tesseract/ with EAST opencv text detector Uses the EAST opencv detector defined here with pytesseract to extract text(de

Python-based tools for document analysis and OCR

ocropy OCRopus is a collection of document analysis programs, not a turn-key OCR system. In order to apply it to your documents, you may need to do so

A Tensorflow model for text recognition (CNN + seq2seq with visual attention) available as a Python package and compatible with Google Cloud ML Engine.
A Tensorflow model for text recognition (CNN + seq2seq with visual attention) available as a Python package and compatible with Google Cloud ML Engine.

Attention-based OCR Visual attention-based OCR model for image recognition with additional tools for creating TFRecords datasets and exporting the tra

Detect text blocks and OCR poorly scanned PDFs in bulk. Python module available via pip.
Detect text blocks and OCR poorly scanned PDFs in bulk. Python module available via pip.

doc2text doc2text extracts higher quality text by fixing common scan errors Developing text corpora can be a massive pain in the butt. Much of the tex

~1000 book pages + OpenCV + python = page regions identified as paragraphs, lines, images, captions, etc.
~1000 book pages + OpenCV + python = page regions identified as paragraphs, lines, images, captions, etc.

cosc428-structor I had an open-ended Computer Vision assignment to complete, and an out-of-copyright book that I wanted to turn into an ebook. Convent

POT : Python Optimal Transport

This open source Python library provide several solvers for optimization problems related to Optimal Transport for signal, image processing and machine learning.

Owner
Pablo QB
I'm a student of the double degree on Computer Engineering and Mathematics at UAM university. Here I upload some of my personal proyects just for fun.
Pablo QB
Rubik's Cube in pygame with OpenGL

Rubik Rubik's Cube in pygame with OpenGL The script show on the screen a Rubik Cube buit with OpenGL. Then I have also implemented all the possible mo

Gabro 2 Apr 15, 2022
A Screen Translator/OCR Translator made by using Python and Tesseract, the user interface are made using Tkinter. All code written in python.

About An OCR translator tool. Made by me by utilizing Tesseract, compiled to .exe using pyinstaller. I made this program to learn more about python. I

Fauzan F A 41 Dec 30, 2022
The world's simplest facial recognition api for Python and the command line

Face Recognition You can also read a translated version of this file in Chinese 简体中文版 or in Korean 한국어 or in Japanese 日本語. Recognize and manipulate fa

Adam Geitgey 47k Jan 7, 2023
A Python wrapper for Google Tesseract

Python Tesseract Python-tesseract is an optical character recognition (OCR) tool for python. That is, it will recognize and "read" the text embedded i

Matthias A Lee 4.6k Jan 6, 2023
A Python wrapper for the tesseract-ocr API

tesserocr A simple, Pillow-friendly, wrapper around the tesseract-ocr API for Optical Character Recognition (OCR). tesserocr integrates directly with

Fayez 1.7k Dec 31, 2022
Simple SDF mesh generation in Python

Generate 3D meshes based on SDFs (signed distance functions) with a dirt simple Python API.

Michael Fogleman 1.1k Jan 8, 2023
Image processing in Python

scikit-image: Image processing in Python Website (including documentation): https://scikit-image.org/ Mailing list: https://mail.python.org/mailman3/l

Image Processing Toolbox for SciPy 5.2k Dec 30, 2022
Image augmentation library in Python for machine learning.

Augmentor is an image augmentation library in Python for machine learning. It aims to be a standalone library that is platform and framework independe

Marcus D. Bloice 4.8k Jan 4, 2023
A simple document layout analysis using Python-OpenCV

Run the application: python main.py *Note: For first time running the application, create a folder named "output". The application is a simple documen

Roinand Aguila 109 Dec 12, 2022
Python library to extract tabular data from images and scanned PDFs

Overview ExtractTable - API to extract tabular data from images and scanned PDFs The motivation is to make it easy for developers to extract tabular d

Org. Account 165 Dec 31, 2022