• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Solve your Rubik's Cube with an easy two-cycle solution

Rubik's Cube

Gem Version Travis CI

Solve your Rubik's Cube with an easy two-cycle solution

Description

Can you solve the Rubik's Cube? Do you want to learn how? GREAT!

We will teach you a very simple two-cycle solution for solving the Rubik's Cube. It's so simple that I used this exact solution to solve the cube blindfolded in the 2005 Rubik's World Championship with a time of 5 minutes and 40 seconds. After you've mastered this solution, you will average less than 2 minutes and will have enough understanding to progress to the Fridrich CFOP method. You'll be solving the Rubik's Cube in less than 20 seconds in no time. WOW!

![](https://raw.github.com/chrishunt/rubiks-cube/master/img/video_thumbnail.jpg)

Sections

  1. Usage
  2. Setting Rubik's Cube State
  3. Turning a Rubik's Cube
  4. Solving a Rubik's Cube
  5. Algorithms
  6. Installation
  7. Contributing
  8. License

Usage

require 'rubiks_cube'

cube = RubiksCube::Cube.new
cube.solved? #=> true

scramble = "U D B2 U B D2 B2 F' R' U2 F U' L2 F L2 B2 L2 R' U R' U' D R2 F2 B2"

cube.perform! scramble
cube.solved? #=> false

solution = RubiksCube::TwoCycleSolution.new(cube)

solution.length #=> 458

puts solution.pretty

  # Setup:  L2
  # Fix:    R U R' U' R' F R2 U' R' U' R U R' F'
  # Undo:   L2
  #
  # Setup:  M2 D L2
  # Fix:    R U R' U' R' F R2 U' R' U' R U R' F'
  # Undo:   L2 D' M2
  #
  # Setup:  U' F' U
  # Fix:    R U R' U' R' F R2 U' R' U' R U R' F'
  # Undo:   U' F U
  # ...

Setting Rubik's Cube State

If you have a scrambled Rubik's Cube sitting in front of you right now, you may not know how to get it back to the solved position. That's OK! We can set the Rubik's Cube state manually and learn how to solve it.

Starting with the F face, enter the color of each sticker from top-left to bottom-right. You may use any characters you like to describe the color of your cube, but make sure all six colors are unique. Whitespace is not important.

The cube state must be entered in the following order:

  1. F: Front Face
  2. R: Right Face
  3. B: Back Face
  4. L: Left Face
  5. U: Up Face (top)
  6. D: Down Face (bottom)

See the examples below for more help.

Examples

Entering cube state manually can be confusing at first. Here are some examples to help you out.

Solved Cube

A Rubik's Cube is solved by default.

require 'rubiks_cube'

cube = RubiksCube::Cube.new

cube.state = <<-STATE
  G G G G G G G G G
  Y Y Y Y Y Y Y Y Y
  B B B B B B B B B
  W W W W W W W W W
  R R R R R R R R R
  O O O O O O O O O
STATE

cube.solved? #=> true

Slightly Scrambled Cube

Our cube is now slightly scrambled.

require 'rubiks_cube'

cube = RubiksCube::Cube.new

cube.state = <<-STATE
  G G Y G G R G G G
  R R Y Y Y Y Y Y Y
  B B B B B B B B W
  W W W W W W O W W
  R R R R R G R Y G
  O O O O O O B O O
STATE

cube.solved? #=> false

Fully Scrambled Cube

Here's a fully scrambled cube. This looks hard to solve. We can start our cube in this state if we like and have the solver tell us a solution.

Here's the scramble we used to mix the cube if you'd like to try it yourself:

U D B2 U B D2 B2 F' R' U2 F U' L2 F L2 B2 L2 R' U R' U' D R2 F2 B2
require 'rubiks_cube'

cube = RubiksCube::Cube.new

cube.state = <<-STATE
  R G O R G O O W G
  G W W B Y W Y Y Y
  G B B G B W R Y B
  Y G Y B W G R Y W
  O R R O R O G Y W
  B R O R O O W B B
STATE

cube.solved? #=> false

Turning a Rubik's Cube

Each Rubik's Cube face ( L, R, F, B, D, U ) can be turned clockwise manually by calling the appropriate method on the cube. For example, if we'd like to turn the right face twice, the down face once, and the back face three times:

require 'rubiks_cube'

cube = RubiksCube::Cube.new

cube.r.r.d.b.b.b

Most people will prefer to use standard Rubik's Cube algorithm notation for turning the cube. Here's the same example with with cube notation:

require 'rubiks_cube'

cube = RubiksCube::Cube.new

cube.perform! "R2 D B'"

Performing face turns on the cube changes the state.

require 'rubiks_cube'

cube = RubiksCube::Cube.new

cube.perform!(
  "U D2 F2 L' R' D' B' U' D L D U2 B' L2 F2 R' U D F2 B' R' F2 U F2 B"
)

cube.solved? #=> false

cube.state
  # "DB UF RB RF LB DL UR RD FD UB LF UL FDL DFR UBR BDR UFL LDB FUR LBU"

Solving a Rubik's Cube

We've provided a simple two-cycle solution to help you solve the Rubik's Cube. This solution is very inefficient, but wonderful for humans. Using the two-cycle solution, you can quickly learn how to solve the Rubik's Cube without using the computer.

Two Cycle Solution

The two-cycle solution is a popular solution used to solve the Rubik's Cube blindfolded. It requires little memorization and takes little time to learn. Solutions usually range from 400-600 turns, but most of those turns are quickly executed. We can easily achieve times of less than 4 minutes with this solution.

The two-cycle solution solves the cube by swapping two cubies at a time until all cubies are in the correct location. This is the permutation step. After the cubies are permuted, we then rotate two cubies at a time (in their current location) until all are oriented correctly. This is the orientation step. We call this the two-cycle solution because everything is done in pairs.

You can use the rubiks_cube gem to learn the two-cycle solution. For each step, we provide setup moves, the fixing algorithm (either changing permutation or orientation), and the undo moves. Pay close attention to how the cube moves and you will be solving by yourself in no time.

See Usage for an example of the TwoCycleSolution

How It Works: Permutation

The permutation step is completed using only two algorithms. One swaps two edges and the other swaps two corners. The diagram above shows which edges and corners are swapped.

When a solution is calculated, we present a 'setup' algorithm (which gets the cubies into a position where they can be swapped), then we present one of the two swapping algorithms, followed by an 'undo' algorithm that reverses the setup move.

How It Works: Orientation

The orientation step is completed using only two algorithms. One flips two edges and the other rotates two corners (one clockwise and the other counter-clockwise). The diagram above shows which edges and corners are rotated.

When a solution is calculated, we present a 'setup' algorithm (which gets the cubies into a position where they can be rotated), then we present one of the two rotation algorithms, followed by an 'undo' algorithm that reverses the setup move.

Algorithms

All algorithms can be found in RubiksCube::Algorithms

Algorithm Notation

Rubik's Cube algorithm notation is easy to understand, but may look confusing at first. Each face is represented by a letter:

  • L: Left Face
  • R: Right Face
  • F: Front Face
  • B: Back Face
  • U: Up Face (top)
  • D: Down Face (bottom)

When we see a letter in an algorithm, then we turn that face 90 degrees clockwise. To determine which direction is clockwise, rotate the cube so that you are looking at the face, then make the turn.

Faces may be followed by one of two modifiers:

  • ': Rotate the face 90 degrees counter-clockwise
  • 2: Rotate the face 180 degrees (two turns)

For example, if we want to apply the algorithm F2 B D' R, then we would take these steps:

  1. Rotate F face 180 degrees (two turns)
  2. Rotate B face 90 degrees clockwise
  3. Rotate D face 90 degrees counter-clockwise
  4. Rotate R face 90 degrees clockwise

M Slice

There is one special algorithm notation that does not map to a face. This is called the M slice. The M slice is the middle vertical layer of the Rubik's Cube.

When you see M, then rotate this slice 90 degrees clockwise. To figure out which direction is clockwise, look at the L face.

For example, if we want to apply the algorithm M2 F M2, then we would take these steps:

  1. Rotate M slice 180 degrees (two turns)
  2. Rotate F face 90 degrees clockwise
  3. Rotate M slice 180 degrees (two turns)

Installation

$ gem install rubiks_cube

Contributing

Please see the Contributing Document

License

Copyright (C) 2013 Chris Hunt, MIT License

More Repositories

1

git-pissed

gitting pissed about your code
HTML
835
star
2

dot-files

Dot-files for Linux, Mac OS X, Windows
Vim Script
603
star
3

github-auth

SSH key management for GitHub users
Ruby
384
star
4

desktop

A decent way to change your desktop image
Ruby
250
star
5

color-schemes

Color schemes I've used for an extended period of time and like
Vim Script
239
star
6

favcount

Enhance your useless favicon with FavCount technology
JavaScript
183
star
7

negfix8

Automate the process of scanning negative film
Shell
56
star
8

marked

Themes for Marked, the best live markdown previewer on Mac OS.
CSS
21
star
9

hi

hi, I'm here to debug your HTTP
Ruby
20
star
10

healthyhacker

Welcome to the Healthy Hacker where we talk about programming, puzzles, memory, fitness, diet, and everything else that you, a healthy hacker, find interesting.
JavaScript
16
star
11

tree

Example binary search tree implementation in Java
Java
13
star
12

cards

Memorize a shuffled deck of playing cards
CSS
12
star
13

snoop

Snoop on content, be notified when it changes.
Ruby
8
star
14

alfred

Alfred Workflows
Ruby
6
star
15

exercises

You have one hour to complete this exercise.
Ruby
6
star
16

status-board

Your very own status board API
Ruby
6
star
17

mute

Muting test suites since 2014
Ruby
5
star
18

practice-problems

Fun practice problems
Ruby
4
star
19

zw2fit

Convert zwo workouts to ANT+ FIT
Ruby
4
star
20

gorbypuff

gorbypuff.cat
JavaScript
3
star
21

cane-hashcheck

Create Cane violations for pre-Ruby 1.9 hash syntax
Ruby
3
star
22

helpme

Help me figure this out
Ruby
3
star
23

parse-tree

Example tab tree parser for #ruby on FreeNode
Ruby
2
star
24

maze

Depth-first maze generation
Ruby
2
star
25

wod

workout of the day
JavaScript
2
star
26

chrishunt.github.com

chrishunt.co
JavaScript
2
star
27

latex-makefile

Makefile for LaTex documents.
2
star
28

cube-timer

Rubik's Cube Timer: Generate scrambles and take averages
Ruby
1
star
29

diffie-chat

Diffieโ€“Hellman key exchange
Java
1
star
30

euler

Project Euler
Ruby
1
star
31

roster-runner

Code replacements for fast sports captioning - rosterrunner.com
Ruby
1
star
32

some-skillz

You need to gem install some_skills
Ruby
1
star
33

tortoise

Tortoise is a Logo interpreter for ruby
Ruby
1
star
34

memory

Remember that time you memorized a shuffled deck of cards?
CSS
1
star
35

follower

Keep track of who starts and stops following you on Twitter
Ruby
1
star