Class: TwistyPuzzles::AxisFaceAndDirectionMove

Inherits:
AbstractMove
  • Object
show all
Defined in:
lib/twisty_puzzles/axis_face_and_direction_move.rb

Overview

Intermediate base class for all types of moves that have an axis face and a direction, i.e. cube moves and rotations.

Direct Known Subclasses

CubeMove, Rotation

Constant Summary

Constants inherited from AbstractMove

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractMove

#<=>, check_move_metric, #decide_meaning, #eql?, #equivalent?, #equivalent_internal?, #hash, #identity?, #inverse, #join_with_cancellation, #move_count, #prepend_inner_m_slice_move, #prepend_slice_move, #puzzles, #slice_move?, #swap

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

#initialize(axis_face, direction) ⇒ AxisFaceAndDirectionMove

Returns a new instance of AxisFaceAndDirectionMove.

Raises:

  • (TypeError)


10
11
12
13
14
15
16
17
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 10

def initialize(axis_face, direction)
  raise TypeError, "Unsuitable axis face #{axis_face}." unless axis_face.is_a?(Face)
  raise TypeError unless direction.is_a?(CubeDirection)

  super()
  @axis_face = axis_face
  @direction = direction
end

Instance Attribute Details

#axis_faceObject (readonly)

Returns the value of attribute axis_face.



19
20
21
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 19

def axis_face
  @axis_face
end

#directionObject (readonly)

Returns the value of attribute direction.



19
20
21
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 19

def direction
  @direction
end

Instance Method Details

#can_swap?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 41

def can_swap?(other)
  super || same_axis?(other)
end

#canonical_directionObject



37
38
39
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 37

def canonical_direction
  @axis_face.canonical_axis_face? ? @direction : @direction.inverse
end

#identifying_fieldsObject



33
34
35
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 33

def identifying_fields
  [@axis_face, @direction]
end

#mirror(normal_face) ⇒ Object



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

def mirror(normal_face)
  if normal_face.same_axis?(@axis_face)
    fields = replace_once(
      replace_once(identifying_fields, @direction, @direction.inverse),
      @axis_face, @axis_face.opposite
    )
    self.class.new(*fields)
  else
    inverse
  end
end

#rotate_by(rotation) ⇒ Object

Raises:

  • (TypeError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 53

def rotate_by(rotation)
  raise TypeError unless rotation.is_a?(Rotation)

  if same_axis?(rotation)
    self
  else
    rotation_neighbors = rotation.axis_face.neighbors
    face_index = rotation_neighbors.index(@axis_face)
    raise unless face_index

    new_axis_face =
      rotation_neighbors[(face_index + rotation.direction.value) % rotation_neighbors.length]
    fields = replace_once(identifying_fields, @axis_face, new_axis_face)
    self.class.new(*fields)
  end
end

#same_axis?(other) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 29

def same_axis?(other)
  @axis_face.same_axis?(other.axis_face)
end

#swap_internal(other) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/twisty_puzzles/axis_face_and_direction_move.rb', line 45

def swap_internal(other)
  if same_axis?(other)
    [other, self]
  else
    super
  end
end

#translated_direction(other_axis_face) ⇒ Object



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

def translated_direction(other_axis_face)
  case @axis_face
  when other_axis_face then @direction
  when other_axis_face.opposite then @direction.inverse
  else raise ArgumentError
  end
end