Class: TwistyPuzzles::ColorScheme

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

Overview

A color scheme that assigns a color to each face.

Defined Under Namespace

Classes: CornerMatcher

Constant Summary collapse

RESERVED_COLORS =
%i[transparent unknown oriented].freeze

Constants included from CubeConstants

TwistyPuzzles::CubeConstants::ALPHABET_SIZE, TwistyPuzzles::CubeConstants::CHIRALITY_FACE_SYMBOLS, TwistyPuzzles::CubeConstants::FACE_NAMES, TwistyPuzzles::CubeConstants::FACE_SYMBOLS, TwistyPuzzles::CubeConstants::OPPOSITE_FACE_SYMBOLS, TwistyPuzzles::CubeConstants::SKEWB_STICKERS

Instance Method Summary collapse

Methods included from Utils::ArrayHelper

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

Methods included from CubeConstants

#chirality_canonical_face_symbol, #opposite_face_symbol, #valid_chirality?

Constructor Details

#initialize(face_symbols_to_colors) ⇒ ColorScheme

Returns a new instance of ColorScheme.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/twisty_puzzles/color_scheme.rb', line 17

def initialize(face_symbols_to_colors)
  check_face_symbols_to_colors(face_symbols_to_colors)

  num_uniq_colors = face_symbols_to_colors.values.uniq.length
  unless num_uniq_colors == FACE_SYMBOLS.length
    raise ArgumentError, "Got #{num_uniq_colors} unique colors " \
                         "#{face_symbols_to_colors.values.uniq}, " \
                         "but needed #{FACE_SYMBOLS.length}."
  end

  @face_symbols_to_colors = face_symbols_to_colors
  @colors_to_face_symbols = face_symbols_to_colors.invert
end

Instance Method Details

#color(face_symbol) ⇒ Object



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

def color(face_symbol)
  @face_symbols_to_colors[face_symbol]
end

#colorsObject



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

def colors
  @face_symbols_to_colors.to_a.sort_by(&:first).map(&:last)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


31
32
33
# File 'lib/twisty_puzzles/color_scheme.rb', line 31

def eql?(other)
  self.class.equal?(other.class) && colors == other.colors
end

#face_symbol(color) ⇒ Object



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

def face_symbol(color)
  @colors_to_face_symbols[color]
end

#hashObject



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

def hash
  @hash ||= [self.class, colors].hash
end

#opposite_color(color) ⇒ Object



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

def opposite_color(color)
  color(opposite_face_symbol(face_symbol(color)))
end

#ordered_colorsObject

Colors in the order of the face symbols.



96
97
98
# File 'lib/twisty_puzzles/color_scheme.rb', line 96

def ordered_colors
  FACE_SYMBOLS.map { |s| color(s) }
end

#part_for_colors(part_type, colors) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/twisty_puzzles/color_scheme.rb', line 49

def part_for_colors(part_type, colors)
  raise ArgumentError unless part_type.is_a?(Class) && (part_type < Part)

  part_type.for_face_symbols(colors.map { |c| face_symbol(c) })
end

#solved_cube_state(cube_size) ⇒ Object



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

def solved_cube_state(cube_size)
  stickers =
    ordered_colors.map do |c|
      Array.new(cube_size) { Array.new(cube_size) { c } }
    end
  CubeState.from_stickers(cube_size, stickers)
end

#solved_skewb_stateObject



100
101
102
# File 'lib/twisty_puzzles/color_scheme.rb', line 100

def solved_skewb_state
  SkewbState.for_solved_colors(@face_symbols_to_colors.dup)
end

#turned(top_color, front_color) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/twisty_puzzles/color_scheme.rb', line 63

def turned(top_color, front_color)
  raise ArgumentError if top_color == front_color
  raise ArgumentError if opposite_color(top_color) == front_color
  raise ArgumentError unless colors.include?(top_color)
  raise ArgumentError unless colors.include?(front_color)

  # NOTE: The reason that this is so complicated is that we want it to still work if the
  # chirality corner gets exchanged.

  # Do the obvious and handle opposites of the top and front color so we have no
  # assumptions that the chirality corner contains U and F.
  turned_face_symbols_to_colors =
    obvious_turned_face_symbols_to_colors(top_color, front_color)

  # Now find the corner that gets mapped to the chirality corner. We know
  # two of its colors and the position of the missing color.
  chirality_corner_source, unknown_index =
    chirality_corner_source_and_unknown_index(turned_face_symbols_to_colors)

  add_missing_mappings(turned_face_symbols_to_colors, chirality_corner_source, unknown_index)

  ColorScheme.new(turned_face_symbols_to_colors)
end