Class: TwistyPuzzles::Rotation

Inherits:
AxisFaceAndDirectionMove show all
Defined in:
lib/twisty_puzzles/rotation.rb

Overview

A rotation of a Skewb or cube.

Constant Summary collapse

ALL_ROTATIONS =
Face::ELEMENTS.product(CubeDirection::ALL_DIRECTIONS).map { |f, d| new(f, d) }
NON_ZERO_ROTATIONS =
Face::ELEMENTS.product(CubeDirection::NON_ZERO_DIRECTIONS).map { |f, d| new(f, d) }
LEFT =
new(Face::U, CubeDirection::BACKWARD)
RIGHT =
new(Face::U, CubeDirection::FORWARD)

Constants inherited from AbstractMove

AbstractMove::AXES, AbstractMove::MOVE_METRICS, AbstractMove::SLICE_FACES, AbstractMove::SLICE_NAMES

Instance Attribute Summary

Attributes inherited from AxisFaceAndDirectionMove

#axis_face, #direction

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AxisFaceAndDirectionMove

#can_swap?, #canonical_direction, #identifying_fields, #initialize, #mirror, #rotate_by, #same_axis?, #swap_internal, #translated_direction

Methods inherited from AbstractMove

#<=>, #can_swap?, check_move_metric, #decide_meaning, #direction, #eql?, #equivalent?, #hash, #identifying_fields, #identity?, #inverse, #join_with_cancellation, #mirror, #prepend_inner_m_slice_move, #rotate_by, #swap, #swap_internal

Methods included from Utils::ArrayHelper

#apply_permutation, #check_types, #find_only, #only, #replace_once, #rotate_out_nils, #turned_equals?

Methods included from Utils::StringHelper

#camel_case_to_snake_case, #format_time, #simple_class_name, #snake_case_class_name

Constructor Details

This class inherits a constructor from TwistyPuzzles::AxisFaceAndDirectionMove

Class Method Details

.around_corner(corner, skewb_direction) ⇒ Object

Returns an algorithm consisting of two rotations that are equivalent to rotating the puzzle around a corner. Takes a Skewb direction as an argument (even for cubes) because rotating around is like a Skewb move given that it’s modulo 3.

Raises:

  • (TypeError)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/twisty_puzzles/rotation.rb', line 33

def self.around_corner(corner, skewb_direction)
  raise TypeError unless corner.is_a?(Corner)
  raise TypeError unless skewb_direction.is_a?(SkewbDirection)

  direction = translated_direction(skewb_direction)

  Algorithm.new(
    [
      Rotation.new(corner.faces[skewb_direction.value], direction),
      Rotation.new(corner.faces[0], direction)
    ]
  )
end

.translated_direction(direction) ⇒ Object

Translates a Skewb direction into a cube direction.



20
21
22
23
24
25
26
27
# File 'lib/twisty_puzzles/rotation.rb', line 20

def self.translated_direction(direction)
  case direction
  when SkewbDirection::ZERO then CubeDirection::ZERO
  when SkewbDirection::FORWARD then CubeDirection::FORWARD
  when SkewbDirection::BACKWARD then CubeDirection::BACKWARD
  else raise
  end
end

Instance Method Details

#alternativeObject

Returns an alternative representation of the same rotation



60
61
62
# File 'lib/twisty_puzzles/rotation.rb', line 60

def alternative
  Rotation.new(@axis_face.opposite, @direction.inverse)
end

#equivalent_internal?(other, _cube_size) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/twisty_puzzles/rotation.rb', line 64

def equivalent_internal?(other, _cube_size)
  [self, alternative].include?(other)
end

#move_count(_cube_size, _metric = :htm) ⇒ Object



99
100
101
# File 'lib/twisty_puzzles/rotation.rb', line 99

def move_count(_cube_size, _metric = :htm)
  0
end

#prepend_fat_m_slice_move(_other, _cube_size) ⇒ Object

rubocop:enable Metrics/AbcSize



83
84
85
# File 'lib/twisty_puzzles/rotation.rb', line 83

def prepend_fat_m_slice_move(_other, _cube_size)
  nil
end

#prepend_fat_move(other, cube_size) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/twisty_puzzles/rotation.rb', line 87

def prepend_fat_move(other, cube_size)
  return unless compatible_fat_move?(other)

  Algorithm.move(
    FatMove.new(other.axis_face.opposite, other.direction, other.inverted_width(cube_size))
  )
end

#prepend_rotation(other, _cube_size) ⇒ Object

rubocop:disable Metrics/AbcSize



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/twisty_puzzles/rotation.rb', line 69

def prepend_rotation(other, _cube_size)
  if same_axis?(other)
    direction = translated_direction(other.axis_face)
    Algorithm.move(Rotation.new(other.axis_face, direction + other.direction))
  elsif @direction.double_move? && other.direction.double_move?
    used_axis_priorities = [@axis_face, other.axis_face].map(&:axis_priority)
    # Note that there are two solutions, but any works.
    remaining_face =
      Face::ELEMENTS.find { |f| !used_axis_priorities.include?(f.axis_priority) }
    Algorithm.move(Rotation.new(remaining_face, CubeDirection::DOUBLE))
  end
end

#prepend_slice_move(_other, _cube_size) ⇒ Object



95
96
97
# File 'lib/twisty_puzzles/rotation.rb', line 95

def prepend_slice_move(_other, _cube_size)
  nil
end

#puzzlesObject



51
52
53
# File 'lib/twisty_puzzles/rotation.rb', line 51

def puzzles
  [Puzzle::SKEWB, Puzzle::NXN_CUBE]
end

#slice_move?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/twisty_puzzles/rotation.rb', line 55

def slice_move?
  false
end

#to_sObject



47
48
49
# File 'lib/twisty_puzzles/rotation.rb', line 47

def to_s
  "#{AXES[@axis_face.axis_priority]}#{canonical_direction.name}"
end