{"id":20139307,"url":"https://github.com/kkvanonymous/rubiks-cube","last_synced_at":"2025-09-21T07:30:51.104Z","repository":{"id":129125217,"uuid":"365999890","full_name":"KKVANONYMOUS/Rubiks-Cube","owner":"KKVANONYMOUS","description":"A simple Rubiks Cube solver that gives the optimal way to solve the cube.","archived":false,"fork":false,"pushed_at":"2021-07-05T04:44:51.000Z","size":157,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-13T21:47:39.224Z","etag":null,"topics":["cfop","rubiks-cube","rubiks-cube-solver"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KKVANONYMOUS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-10T10:13:20.000Z","updated_at":"2024-09-02T17:59:08.000Z","dependencies_parsed_at":"2023-04-19T03:37:23.422Z","dependency_job_id":null,"html_url":"https://github.com/KKVANONYMOUS/Rubiks-Cube","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KKVANONYMOUS%2FRubiks-Cube","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KKVANONYMOUS%2FRubiks-Cube/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KKVANONYMOUS%2FRubiks-Cube/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KKVANONYMOUS%2FRubiks-Cube/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KKVANONYMOUS","download_url":"https://codeload.github.com/KKVANONYMOUS/Rubiks-Cube/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233723968,"owners_count":18720110,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cfop","rubiks-cube","rubiks-cube-solver"],"created_at":"2024-11-13T21:44:45.297Z","updated_at":"2025-09-21T07:30:45.732Z","avatar_url":"https://github.com/KKVANONYMOUS.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rubiks-Cube\n- [Rubiks-Cube](#rubiks-cube)\n  * [Description](#description)\n  * [Theory](#theory)\n    + [How the program takes Cube Input?](#how-the-program-takes-cube-input)\n    + [Structures used to store the cube state](#structures-used-to-store-the-cube-state)\n    + [Algorithms involved in solving the cube](#algorithms-involved-in-solving-the-cube)\n  * [Usage](#usage)\n    + [Notations](#notations)\n    + [Installation](#installation)\n    + [Giving Input](#giving-input)\n    + [Solving the cube](#solving-the-cube)\n    + [Demo Input](#demo-input)\n      - [Shuffle function](#shuffle-function)\n  * [Team](#team)\n \n## Description\nThe [Rubik’s Cube](https://en.wikipedia.org/wiki/Rubik%27s_Cube) is an infamous puzzle designed in 1974 by a Hungarian professor, known as Ernő Rubik. The puzzle consists of a large cube being split into a 3x3 orientation of smaller cubes, with each side being able to rotate 360 degrees. Each side is assigned a colour, and the cube is considered **`solved`** when each side is made up of just one colour.\n\u003cbr\u003e\nEvery time you rotate a side, it breaks the colours apart, scrambling the puzzle. Once the cube is fully scrambled the aim is to try and piece all the colours together until it is back in its solved form.\u003cbr\u003e\u003cbr\u003e\nThere are various methods to solve Rubik's Cube but here we will make use of `CFOP Algorithm` to solve the cube in a very optimized manner.\n\u003cbr\u003e\u003cbr\u003e\nSounds _interesting_,right?\n\u003cbr\u003e\u003cbr\u003e\nLet's dig deep into the various aspects involved in solving the cube\n## Theory\n### How the program takes Cube Input?\n   - Images of all the cube faces are given as input to `CubeInput.py` file (See [Giving Input](#giving-input) to know how to take cube images) where it reads all the images one by one and stores the image data in the form of numpy array. To crop the unnecessary part of image, we have used `selectROI()` function. \n   - The croped image is then resized and pass to `face_color()` function where the different color blocks are masked out using their hsv values and `inRange()` function of opencv. This function returns a black-white image of each block where the particular color is white and rest is all black.\n   - This image is passed to `mask()` function where we have find contours and then iterated over each contour and rejected small contours on the basis of area.We have initiliased a 2-d zero array in which we store the color states of each face. Using some maths and using centres of each block ,we have filled the 2-d array.\n   - The blocks are given color according to the manner specified in [Notations](#notations) section.\n \n### Structures used to store the cube state\n   - **arr** is a 2D vector which consists of all 6 colours and its index represents its neighbours. i.e. in arr[i][j], i denotes the colour code and j represent the color code of its neighbors.\n\n   - **matrixes[6][9]** is a vector of struct **faceMatrix** consisting of two vectors v1 and v2 where v1 stores its current position of that block and v2 stores its        current position according to the centre, (for example: matrixes[1][1] looks for current position of 2 sided red-yellow block and v2 represent in which center red color is faced).\n\n### Algorithms involved in solving the cube\nAs discussed above `CFOP Algorithm` is used for solving the cube. The various steps involved in this algorithm are as follows:\n - **Making White Cross** :- For making white `makeWhiteCross()` function is used.There are total 4!=24 (solving in any sequence of red, green, blue and orange) ways to make white cross. We check for all the possible ways and then the most optimal way (that takes minimum number of steps) is applied to make the white cross.\n    \n - **F2L** :- The next step is to make the first two layer (i.e- middle layer of red, green, blue and orange and all four white corner pieces) which is implemented using the `implementF2L()` function based on the [F2L Algo](https://ruwix.com/the-rubiks-cube/advanced-cfop-fridrich/first-two-layers-f2l/).\n Similar to the previous step, here again we loop through all the possible ways and implement the most optimal one.\n  \n - **Making Yellow Cross** :- First we check if the yellow cross is already made or not by using the `checkYellowCross()` function. If it's not, we call the           `createYellowCross()` function to make the  yellow cross by looking at all possible cases.\n\n - **OLL** :- When the first two layers (F2L) are solved we need to orient the last layer (OLL) so the upper face of the Rubik's Cube is all yellow. We don't care if the side colors don't match, we are going to permute the last layer (PLL) in the next step. The function `oll()` implements the above algorithm by first counting the number of yellow corners in the current cube state and then applying the corresponding algorithm for orienting the layer.\n\n  - **PLL** :- In this final step we make use of the `setCorners` function to set the corners of the last layer and then call `setEdges` to finally set the edges.\n\n_Hurray our cube is solved!_ :)\n\n## Usage\n### Notations\nEvery rotation of the cube is represented by a unique `color code` lying between `0-11 (both inclusive)` which are mentioned below:\n```cpp\n          COLOR CODES\n -----------------------------------\n Color  |  Clockwise  | Anticlockwise\n ------ |-------------|-------------\n white  |     0       |     6\n red    |     1       |     7\n green  |     2       |     8\n blue   |     3       |     9\n orange |     4       |     10\n yellow |     5       |     11\n```\nFor example, `color code 5` means that the `yellow face` needs to be rotated only `once`.\n\u003cbr\u003e\nSimilar logic can be applied for other codes as well.\n### Installation\n- Clone the repository\n```bash\n$ git clone https://github.com/KKVANONYMOUS/Rubiks-Cube.git\n```\n or download zip file\n- Change your current directory to the repo's root.\n```bash\n$ cd Rubiks-Cube\n```\n\n### Giving Input\n- Click the images of all the 6 faces in the following specified pattern:\u003cbr\u003e\nConsider we are first taking the image of cube with red centre,\u003cbr\u003e\nNow keep the current face towards you, i.e. along the x-axis, faces with white and yellow centre along the z-axis with the white centre one facing the negative z-axis and finally the blue and green centre faces along the y-axis with green centre at your right side (i.e. negative y-axis).\n\n   Follow similar kind of pattern for all the remaining faces.\n- Name all  the images in the format- `1.jpeg`,`2.jpeg` and so on and save it in the same directory in which `CubeInput.py` file is present.\n- Run the `CubeInput.py` file using:\n   ```bash\n   $ python3 CubeInput.py\n   ```\n- This python file will return the states of all the faces in `color.txt` file as shown in  [Sample_Input_For_CFOP.txt](https://github.com/KKVANONYMOUS/Rubiks-Cube/blob/master/demo/Sample_Input_For_CFOP.txt). Copy paste the content of this file and give this as an input to `CFOF.cpp` file.\n### Solving the cube\nTo solve the cube, run the `CFOF.cpp` file by using\n```bash\ng++ CFOP.cpp -o CFOP\n```\nand give the content of `color.txt` file obtained in [Giving Input](#giving-input) section as an input to this program.\n\nAfter the completion of each step involved in `CFOP Algortihm` the program will output the number of steps and their respective orders used to implement it.\n\nFor example, the following snippet shows the steps involved in making the white cross\n```cpp\nTotal Steps for WHITE CROSS: 7 //No of Steps\n10 2 4 3 4 3 3 // Order of steps\n```\nPlease refer to [Notations](#notations) for cube notations.\n### Demo Input\nThe repo contains a [demo](https://github.com/KKVANONYMOUS/Rubiks-Cube/tree/master/demo) directory with [images](https://github.com/KKVANONYMOUS/Rubiks-Cube/tree/master/demo/images) folder for sample input for the `CubeInput.py` program which will generate the corresponding face matrix, to be given as input to `CFOP.cpp` program.\nOr else the user can directly give the face matrix present in [Sample_Input_For_CFOP.txt](https://github.com/KKVANONYMOUS/Rubiks-Cube/blob/master/demo/Sample_Input_For_CFOP.txt) file as input to `CFOP.cpp` file.\n\u003cbr\u003e\u003cbr\u003e\nFor more info regarding giving input to the program you can refer to [Giving Input](#giving-input) section.\n#### Shuffle function\nThis function gives additional option to shuffle the cube in order to test the code for different cases. Just give the cube rotation input in the form of numbers ranging from 0-11 as decribed in [Notations](#notations) and press 12 when you are done with the shuffiling.\n## Team\n- Kunal Kumar Verma - [@kkvanonymous](https://github.com/KKVANONYMOUS)\n- Ayushi Gupta - [@Ayushi-Gupta7](https://github.com/Ayushi-Gupta7)\n- Chamma Sarraf - [@codeit-dotcom](https://github.com/codeit-dotcom)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkkvanonymous%2Frubiks-cube","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkkvanonymous%2Frubiks-cube","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkkvanonymous%2Frubiks-cube/lists"}