Skip to content

Commit

Permalink
Fix orientation in TwoCycleSolver
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishunt committed May 16, 2013
1 parent bc8215b commit 51abf68
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
64 changes: 56 additions & 8 deletions lib/cube_solver/two_cycle_solver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,46 @@ def solved?

def solve!
@solution ||= begin
solution = solution_for(:edge)
solution << solution_for(:corner)

solution = permutation_solution
solution << orientation_solution
solution.flatten.compact.join(' ').strip
end
end

private

def solution_for(cubie)
def permutation_solution
[].tap do |solution|
solution << permutation_solution_for(:edge)
solution << permutation_solution_for(:corner)
end
end

def orientation_solution
[].tap do |solution|
solution << orientation_solution_for(:edge)
solution << orientation_solution_for(:corner)
end
end

def permutation_solution_for(cubie)
[].tap do |solution|
until cube.public_send("has_#{cubie}s_permuted?")
solution << swap("#{cubie}".to_sym, next_location_for(cubie)) << "\n"
solution << swap(cubie.to_sym, next_location_for(cubie)) << "\n"
end
end
end

def orientation_solution_for(cubie)
[].tap do |solution|
until cube.public_send("has_#{cubie}s_oriented?")
solution << rotate(cubie.to_sym) << "\n"
end
end
end

def next_location_for(cubie)
buffer_cubie = send("buffer_#{cubie}")
buffer_cubie = send("permutation_buffer_#{cubie}")

if cube.public_send("#{cubie}_permuted?", buffer_cubie)
cube.public_send("unpermuted_#{cubie}_locations").first
Expand All @@ -44,6 +65,12 @@ def next_location_for(cubie)
end
end

def next_unoriented_location_for(cubie)
cube.public_send("unoriented_#{cubie}_locations").tap do |locations|
locations.delete(0)
end.first
end

def swap(type, location)
setup = CubeSolver::Algorithms::PLL::Setup.fetch(type).fetch(location)
swap = send("#{type}_swap_algorithm")
Expand All @@ -52,6 +79,19 @@ def swap(type, location)
%w(setup swap undo).map { |operation| format operation, eval(operation) }
end

def rotate(type)
setup = CubeSolver::Algorithms::OLL::Setup.fetch(type).fetch(
next_unoriented_location_for(type)
)

rotate = send("#{type}_rotate_algorithm")
undo = CubeSolver::Algorithms.reverse(setup)

%w(setup rotate undo).map do |operation|
format operation, eval(operation)
end
end

def format(operation, algorithm)
"#{operation}\t(#{cube.perform! algorithm})\n" unless algorithm.empty?
end
Expand All @@ -64,11 +104,19 @@ def corner_swap_algorithm
CubeSolver::Algorithms::PLL::Y
end

def buffer_edge
def edge_rotate_algorithm
CubeSolver::Algorithms::OLL::I
end

def corner_rotate_algorithm
CubeSolver::Algorithms::OLL::H
end

def permutation_buffer_edge
cube.edges[1]
end

def buffer_corner
def permutation_buffer_corner
cube.corners[0]
end
end
Expand Down
16 changes: 15 additions & 1 deletion spec/cube_solver/two_cycle_solver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@
it_should_behave_like 'a cube that can be solved'
end

context 'when two edges have incorrect orientation' do
before { cube.perform! CubeSolver::Algorithms::OLL::I }

it_should_behave_like 'a cube that can be solved'
end

context 'when two corners have incorrect orientation' do
before { cube.perform! CubeSolver::Algorithms::OLL::H }

it_should_behave_like 'a cube that can be solved'
end

[
"U2 L' F2 U' B D' R' F2 U F' R L2 F2 L' B L R2 B' D R L' U D' R2 F'",
"L U2 R' B2 U D R' U R' B2 L F R2 L' B' R' U2 L2 R2 U F' L' U' L U'",
"F' U2 D2 F2 R' D2 B L2 F L2 D2 F U2 F B L U' D' R' F D F' L2 F' B2"
"F' U2 D2 F2 R' D2 B L2 F L2 D2 F U2 F B L U' D' R' F D F' L2 F' B2",
"R2 U' D2 B L2 U' R D2 L2 U2 F2 D' B' L B' D L' B2 L' F' L' B2 F2 U' L2",
"L2 R D' U2 R L' B2 U' L2 D2 B2 D' R2 L' B' U2 B R2 F2 R' D' F2 D L U",
].each do |scramble|
context "with scramble (#{scramble})" do
before { cube.perform! scramble }
Expand Down

0 comments on commit 51abf68

Please sign in to comment.