Class: TwistyPuzzles::SkewbMoveParser

Inherits:
AbstractMoveParser show all
Defined in:
lib/twisty_puzzles/skewb_move_parser.rb

Overview

Parser for Skewb moves.

Constant Summary collapse

MOVE_TYPE_CREATORS =
MoveTypeCreator.new(%i[axis_face cube_direction], Rotation),
  MoveTypeCreator.new(%i[axis_corner skewb_direction], SkewbMove)
].freeze
FIXED_CORNER_INSTANCE =
SkewbMoveParser.new(SkewbNotation.fixed_corner)
SARAH_INSTANCE =
SkewbMoveParser.new(SkewbNotation.sarah)
RUBIKS_INSTANCE =
SkewbMoveParser.new(SkewbNotation.rubiks)

Instance Method Summary collapse

Methods inherited from AbstractMoveParser

#parse_move, #parse_named_captures

Constructor Details

#initialize(notation) ⇒ SkewbMoveParser

Returns a new instance of SkewbMoveParser.

Raises:

  • (TypeError)


18
19
20
21
22
23
# File 'lib/twisty_puzzles/skewb_move_parser.rb', line 18

def initialize(notation)
  raise TypeError unless notation.is_a?(SkewbNotation)

  super()
  @notation = notation
end

Instance Method Details

#move_type_creatorsObject



45
46
47
# File 'lib/twisty_puzzles/skewb_move_parser.rb', line 45

def move_type_creators
  MOVE_TYPE_CREATORS
end

#parse_move_part(name, value) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/twisty_puzzles/skewb_move_parser.rb', line 63

def parse_move_part(name, value)
  case name
  when 'axis_name' then CubeMoveParser::INSTANCE.parse_axis_face(value)
  when 'cube_direction' then CubeMoveParser::INSTANCE.parse_direction(value)
  when 'skewb_move' then @notation.corner(value)
  when 'skewb_direction' then parse_skewb_direction(value)
  else raise
  end
end

#parse_part_key(name) ⇒ Object



59
60
61
# File 'lib/twisty_puzzles/skewb_move_parser.rb', line 59

def parse_part_key(name)
  name.sub('name', 'face').sub('skewb_move', 'axis_corner')
end

#parse_skewb_direction(direction_string) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/twisty_puzzles/skewb_move_parser.rb', line 49

def parse_skewb_direction(direction_string)
  if AbstractDirection::POSSIBLE_DIRECTION_NAMES[0].include?(direction_string)
    SkewbDirection::FORWARD
  elsif AbstractDirection::POSSIBLE_DIRECTION_NAMES[-1].include?(direction_string)
    SkewbDirection::BACKWARD
  else
    raise ArgumentError
  end
end

#regexpObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/twisty_puzzles/skewb_move_parser.rb', line 29

def regexp
  @regexp ||=
    begin
      skewb_direction_names =
        AbstractDirection::POSSIBLE_SKEWB_DIRECTION_NAMES.flatten
      move_part = "(?:(?<skewb_move>[#{@notation.move_strings.join}])" \
                  "(?<skewb_direction>[#{skewb_direction_names.join}]?))"
      rotation_direction_names =
        AbstractDirection::POSSIBLE_DIRECTION_NAMES.flatten
      rotation_direction_names.sort_by! { |e| -e.length }
      rotation_part = "(?:(?<axis_name>[#{AbstractMove::AXES.join}])" \
                      "(?<cube_direction>#{rotation_direction_names.join('|')}))"
      Regexp.new("#{move_part}|#{rotation_part}")
    end
end