Class: TwistyPuzzles::StickerCycleFactory

Inherits:
Object
  • Object
show all
Includes:
Utils::ArrayHelper
Defined in:
lib/twisty_puzzles/sticker_cycle_factory.rb

Overview

Factory for sticker cycles given part cycles. TODO: Deprecate

Instance Method Summary collapse

Methods included from Utils::ArrayHelper

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

Constructor Details

#initialize(cube_size, incarnation_index) ⇒ StickerCycleFactory

Returns a new instance of StickerCycleFactory.



13
14
15
16
17
18
19
20
21
22
# File 'lib/twisty_puzzles/sticker_cycle_factory.rb', line 13

def initialize(cube_size, incarnation_index)
  CubeState.check_cube_size(cube_size)
  unless incarnation_index.is_a?(Integer) && incarnation_index >= 0
    raise ArgumentError, "Invalid incarnation index #{incarnation_index}."
  end

  @cube_size = cube_size
  @incarnation_index = incarnation_index
  @cache = {}
end

Instance Method Details

#check_type_consistency(parts) ⇒ Object

Raises:

  • (TypeError)


37
38
39
40
41
# File 'lib/twisty_puzzles/sticker_cycle_factory.rb', line 37

def check_type_consistency(parts)
  return unless parts.any? { |p| p.class != parts.first.class }

  raise TypeError, "Cycles of heterogenous piece types #{parts.inspect} are not supported."
end

#construct(parts) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twisty_puzzles/sticker_cycle_factory.rb', line 43

def construct(parts)
  raise ArgumentError, 'Cycles of length smaller than 2 are not supported.' if parts.length < 2

  unless @incarnation_index < parts.first.num_incarnations(@cube_size)
    raise ArgumentError, "Incarnation index #{@incarnation_index} for cube size " \
                         "#{@cube_size} is not supported for #{parts.first.inspect}."
  end

  check_types(parts, Part)
  check_type_consistency(parts)
  part_coordinates = parts.map { |p| coordinates(p) }
  cycles = part_coordinates.transpose.map { |c| StickerCycle.from_coordinates(@cube_size, c) }
  StickerCycles.new(@cube_size, cycles)
end

#coordinates(part) ⇒ Object



24
25
26
# File 'lib/twisty_puzzles/sticker_cycle_factory.rb', line 24

def coordinates(part)
  @cache[part] ||= Coordinate.solved_positions(part, @cube_size, @incarnation_index)
end

#multi_twist(parts) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/twisty_puzzles/sticker_cycle_factory.rb', line 28

def multi_twist(parts)
  unless parts.all? { |p| p.is_a?(Corner) || p.is_a?(Edge) }
    raise TypeError, 'Twists are only supported for edges and corners.'
  end

  cycles = parts.map { |p| StickerCycle.from_coordinates(@cube_size, coordinates(p)) }
  StickerCycles.new(@cube_size, cycles)
end