Class: RubiksCube::Cube

Inherits:
Object
  • Object
show all
Defined in:
lib/rubiks_cube/cube.rb

Overview

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

Constant Summary collapse

SOLVED_STATE =
%w(
  UF UR UB UL FL FR BR BL DF DR DB DL UFL URF UBR ULB DLF DFR DRB DBL
)

Instance Method Summary collapse

Constructor Details

#initialize(state = nil) ⇒ Cube

Returns a new instance of Cube.



8
9
10
11
12
# 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

Instance Method Details

#==(other) ⇒ Object



20
21
22
# File 'lib/rubiks_cube/cube.rb', line 20

def ==(other)
  state == other.state
end

#bObject



90
91
92
93
94
# 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) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rubiks_cube/cube.rb', line 44

def corner_permuted?(corner)
  cubie_permuted? :corners, corner
end

#cornersObject



32
33
34
# File 'lib/rubiks_cube/cube.rb', line 32

def corners
  @state[12..-1]
end

#dObject



79
80
81
82
# File 'lib/rubiks_cube/cube.rb', line 79

def d
  turn [8, 11, 10, 9], [16, 19, 18, 17]
  self
end

#edge_permuted?(edge) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubiks_cube/cube.rb', line 40

def edge_permuted?(edge)
  cubie_permuted? :edges, edge
end

#edgesObject



28
29
30
# File 'lib/rubiks_cube/cube.rb', line 28

def edges
  @state[0..11]
end

#fObject



84
85
86
87
88
# 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

#lObject



68
69
70
71
72
# 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

#mObject



96
97
98
99
100
# File 'lib/rubiks_cube/cube.rb', line 96

def m
  turn [0, 2, 10, 8]
  rotate [0, 2, 10, 8]
  self
end

#perform!(algorithm) ⇒ Object



57
58
59
60
# File 'lib/rubiks_cube/cube.rb', line 57

def perform!(algorithm)
  algorithm.split.each { |move| perform_move! move }
  algorithm
end

#permuted_location_for(cubie) ⇒ Object



48
49
50
51
52
53
54
55
# 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

#rObject



62
63
64
65
66
# 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?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rubiks_cube/cube.rb', line 36

def solved?
  state == SOLVED_STATE.join(' ')
end

#stateObject



24
25
26
# File 'lib/rubiks_cube/cube.rb', line 24

def state
  @state.join ' '
end

#state=(stickers) ⇒ Object



14
15
16
17
18
# File 'lib/rubiks_cube/cube.rb', line 14

def state=(stickers)
  @state = build_state_from_string(
    StickerStateTransform.new(stickers).to_cube
  )
end

#uObject



74
75
76
77
# File 'lib/rubiks_cube/cube.rb', line 74

def u
  turn [0, 1, 2, 3], [12, 13, 14, 15]
  self
end