class RubiksCube::Cube

Standard 3x3x3 Rubik’s Cube with normal turn operations (l, r, u, d, f, b)

Constants

SOLVED_STATE

Public Class Methods

new(state = nil) click to toggle source
# File lib/rubiks_cube/cube.rb, line 8
def initialize(state = nil)
  @state = build_state_from_string(
    state.to_s.empty? ? SOLVED_STATE.join(' ') : state
  )
end

Public Instance Methods

==(other) click to toggle source
# File lib/rubiks_cube/cube.rb, line 20
def ==(other)
  state == other.state
end
b() click to toggle source
# File lib/rubiks_cube/cube.rb, line 90
def b
  turn [2, 6, 10, 7], [14, 18, 19, 15]
  rotate [2, 6, 10, 7], [14, 15, 15, 18, 18, 19]
  self
end
corner_permuted?(corner) click to toggle source
# File lib/rubiks_cube/cube.rb, line 44
def corner_permuted?(corner)
  cubie_permuted? :corners, corner
end
corners() click to toggle source
# File lib/rubiks_cube/cube.rb, line 32
def corners
  @state[12..-1]
end
d() click to toggle source
# File lib/rubiks_cube/cube.rb, line 79
def d
  turn [8, 11, 10, 9], [16, 19, 18, 17]
  self
end
edge_permuted?(edge) click to toggle source
# File lib/rubiks_cube/cube.rb, line 40
def edge_permuted?(edge)
  cubie_permuted? :edges, edge
end
edges() click to toggle source
# File lib/rubiks_cube/cube.rb, line 28
def edges
  @state[0..11]
end
f() click to toggle source
# File lib/rubiks_cube/cube.rb, line 84
def f
  turn [0, 4, 8, 5], [12, 16, 17, 13]
  rotate [0, 4, 8, 5], [12, 13, 13, 16, 16, 17]
  self
end
l() click to toggle source
# File lib/rubiks_cube/cube.rb, line 68
def l
  turn [3, 7, 11, 4], [12, 15, 19, 16]
  rotate [12, 12, 15, 16, 19, 19]
  self
end
m() click to toggle source
# File lib/rubiks_cube/cube.rb, line 96
def m
  turn [0, 2, 10, 8]
  rotate [0, 2, 10, 8]
  self
end
perform!(algorithm) click to toggle source
# File lib/rubiks_cube/cube.rb, line 57
def perform!(algorithm)
  algorithm.split.each { |move| perform_move! move }
  algorithm
end
permuted_location_for(cubie) click to toggle source
# File lib/rubiks_cube/cube.rb, line 48
def permuted_location_for(cubie)
  while (location = SOLVED_STATE.index cubie.state) == nil
    cubie = cubie.rotate
  end

  location -= 12 if location >= 12
  location
end
r() click to toggle source
# File lib/rubiks_cube/cube.rb, line 62
def r
  turn [1, 5, 9, 6], [13, 17, 18, 14]
  rotate [13, 14, 14, 17, 17, 18]
  self
end
solved?() click to toggle source
# File lib/rubiks_cube/cube.rb, line 36
def solved?
  state == SOLVED_STATE.join(' ')
end
state() click to toggle source
# File lib/rubiks_cube/cube.rb, line 24
def state
  @state.join ' '
end
state=(stickers) click to toggle source
# File lib/rubiks_cube/cube.rb, line 14
def state=(stickers)
  @state = build_state_from_string(
    StickerStateTransform.new(stickers).to_cube
  )
end
u() click to toggle source
# File lib/rubiks_cube/cube.rb, line 74
def u
  turn [0, 1, 2, 3], [12, 13, 14, 15]
  self
end

Private Instance Methods

build_state_from_string(state) click to toggle source
# File lib/rubiks_cube/cube.rb, line 116
def build_state_from_string(state)
  state.split.map { |state| RubiksCube::Cubie.new state }
end
cubie_permuted?(type, cubie) click to toggle source
# File lib/rubiks_cube/cube.rb, line 120
def cubie_permuted?(type, cubie)
  send(type).index(cubie) == permuted_location_for(cubie)
end
incorrect_orientation_locations_for(type) click to toggle source
# File lib/rubiks_cube/cube.rb, line 130
def incorrect_orientation_locations_for(type)
  send("#{type}s").each_with_index.map do |cubie, location|
    oriented_state = SOLVED_STATE.fetch(
      if type == :corner
        location + 12
      else
        location
      end
    )

    location unless cubie.state == oriented_state
  end.compact
end
incorrect_permutation_locations_for(type) click to toggle source
# File lib/rubiks_cube/cube.rb, line 124
def incorrect_permutation_locations_for(type)
  send("#{type}s").each_with_index.map do |cubie, location|
    location unless location == permuted_location_for(cubie)
  end.compact
end
perform_move!(move) click to toggle source
# File lib/rubiks_cube/cube.rb, line 164
def perform_move!(move)
  operation = "#{move[0].downcase}"

  case modifier = move[-1]
    when "'"
      3.times { send operation }
    when "2"
      2.times { send operation }
    else
      send operation
  end
end
rotate(*sequences) click to toggle source
# File lib/rubiks_cube/cube.rb, line 158
def rotate(*sequences)
  sequences.each do |cubies|
    cubies.each { |cubie| @state[cubie].rotate! }
  end
end
turn(*sequences) click to toggle source
# File lib/rubiks_cube/cube.rb, line 144
def turn(*sequences)
  sequences.each do |sequence|
    location = sequence.shift
    first_cubie = @state.fetch(location)

    sequence.each do |next_location|
      @state[location] = @state.fetch(next_location)
      location = next_location
    end

    @state[location] = first_cubie
  end
end