Class: TwistyPuzzles::SkewbNotation

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

Overview

Class that represents one notation for Skewb moves, e.g. Sarahs notation or fixed corner notation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, move_corner_pairs) ⇒ SkewbNotation

rubocop:disable Metrics/AbcSize

Raises:

  • (TypeError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twisty_puzzles/skewb_notation.rb', line 14

def initialize(name, move_corner_pairs)
  raise TypeError unless name.is_a?(String)

  super()
  check_move_corner_pairs(move_corner_pairs)
  @name = name
  @move_to_corner = move_corner_pairs.to_h.freeze
  @corner_to_move = move_corner_pairs.flat_map do |m, c|
    c.rotations.map { |e| [e, m] }
  end.to_h.freeze
  @move_strings = move_corner_pairs.map(&:first).freeze
  @non_zero_moves =
    move_corner_pairs.map(&:last).product(SkewbDirection::NON_ZERO_DIRECTIONS).map do |c, d|
      SkewbMove.new(c, d)
    end.freeze
  freeze
end

Instance Attribute Details

#move_stringsObject (readonly)

Returns the value of attribute move_strings.



56
57
58
# File 'lib/twisty_puzzles/skewb_notation.rb', line 56

def move_strings
  @move_strings
end

#nameObject (readonly)

Returns the value of attribute name.



56
57
58
# File 'lib/twisty_puzzles/skewb_notation.rb', line 56

def name
  @name
end

#non_zero_movesObject (readonly)

Returns the value of attribute non_zero_moves.



56
57
58
# File 'lib/twisty_puzzles/skewb_notation.rb', line 56

def non_zero_moves
  @non_zero_moves
end

Class Method Details

.fixed_cornerObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/twisty_puzzles/skewb_notation.rb', line 60

def self.fixed_corner
  @fixed_corner ||= new(
    'fixed corner', [
      ['U', Corner.for_face_symbols(%i[U L B])],
      ['R', Corner.for_face_symbols(%i[D R B])],
      ['L', Corner.for_face_symbols(%i[D F L])],
      ['B', Corner.for_face_symbols(%i[D B L])]
    ]
  )
end

.rubiksObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/twisty_puzzles/skewb_notation.rb', line 82

def self.rubiks
  @rubiks ||= new(
    'rubiks', [
      ['F', Corner.for_face_symbols(%i[U R F])],
      ['R', Corner.for_face_symbols(%i[U B R])],
      ['B', Corner.for_face_symbols(%i[U L B])],
      ['L', Corner.for_face_symbols(%i[U F L])],
      ['f', Corner.for_face_symbols(%i[D F R])],
      ['r', Corner.for_face_symbols(%i[D R B])],
      ['b', Corner.for_face_symbols(%i[D B L])],
      ['l', Corner.for_face_symbols(%i[D L F])]
    ]
  )
end

.sarahObject



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

def self.sarah
  @sarah ||= new(
    'sarah', [
      ['F', Corner.for_face_symbols(%i[U R F])],
      ['R', Corner.for_face_symbols(%i[U B R])],
      ['B', Corner.for_face_symbols(%i[U L B])],
      ['L', Corner.for_face_symbols(%i[U F L])]
    ]
  )
end

Instance Method Details

#algorithm_to_string(algorithm) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/twisty_puzzles/skewb_notation.rb', line 105

def algorithm_to_string(algorithm)
  reversed_rotations = []
  num_tail_rotations = CancellationHelper.num_tail_rotations(algorithm)
  alg_string = algorithm.moves[0...algorithm.length - num_tail_rotations].map do |m|
    move_to_string(m, reversed_rotations)
  end.join(' ')
  new_tail_rotations = reversed_rotations.reverse! +
                       algorithm.moves[algorithm.length - num_tail_rotations..]
  cancelled_rotations = Algorithm.new(new_tail_rotations).cancelled(3)
  cancelled_rotations.empty? ? alg_string : "#{alg_string} #{cancelled_rotations}"
end

#check_corner_coverage(corners) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity



46
47
48
49
50
51
52
53
54
# File 'lib/twisty_puzzles/skewb_notation.rb', line 46

def check_corner_coverage(corners)
  corner_closure = corners + corners.map(&:diagonal_opposite)
  Corner::ELEMENTS.each do |corner|
    unless corner_closure.any? { |c| c.turned_equals?(corner) }
      raise ArgumentError,
            "Turns around corner #{corner} cannot be represented in notation #{name}."
    end
  end
end

#check_move_corner_pairs(move_corner_pairs) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity

Raises:

  • (ArgumentError)


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

def check_move_corner_pairs(move_corner_pairs)
  move_corner_pairs.each do |m|
    raise ArgumentError unless m.length == 2
    raise TypeError unless m[0].is_a?(String)
    raise TypeError unless m[1].is_a?(Corner)
  end
  raise ArgumentError if move_corner_pairs.map(&:first).uniq.length != move_corner_pairs.length

  check_corner_coverage(move_corner_pairs.map(&:last))
end

#corner(move) ⇒ Object



101
102
103
# File 'lib/twisty_puzzles/skewb_notation.rb', line 101

def corner(move)
  @move_to_corner[move] || (raise ArgumentError)
end

#to_sObject



97
98
99
# File 'lib/twisty_puzzles/skewb_notation.rb', line 97

def to_s
  @name
end