Class: TwistyPuzzles::CubeState

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

Overview

Represents the state (i.e. the sticker positions) of a cube.

Constant Summary

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

Constants included from CubePrintHelper

TwistyPuzzles::CubePrintHelper::COLOR_MODES, TwistyPuzzles::CubePrintHelper::FACE_SYMBOL_INFOS, TwistyPuzzles::CubePrintHelper::SKEWB_FACE_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CubeConstants

#chirality_canonical_face_symbol, #opposite_face_symbol, #valid_chirality?

Methods included from Utils::ArrayHelper

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

Methods included from CubePrintHelper

#color_character, #color_symbol, #cube_string, #empty_name, #face_lines, #ll_string, #maybe_reverse, #pad_lines, #simple_face_lines, #skewb_ascii_art, #skewb_ascii_art_line, #skewb_face_lines, #skewb_string, #zip_concat_lines

Constructor Details

#initialize(native) ⇒ CubeState

Returns a new instance of CubeState.

Raises:

  • (TypeError)


52
53
54
55
56
# File 'lib/twisty_puzzles/cube_state.rb', line 52

def initialize(native)
  raise TypeError unless native.is_a?(Native::CubeState)

  @native = native
end

Instance Attribute Details

#nativeObject (readonly)

Returns the value of attribute native.



62
63
64
# File 'lib/twisty_puzzles/cube_state.rb', line 62

def native
  @native
end

Class Method Details

.check_cube_size(cube_size) ⇒ Object

Raises:

  • (TypeError)


17
18
19
20
# File 'lib/twisty_puzzles/cube_state.rb', line 17

def self.check_cube_size(cube_size)
  raise TypeError unless cube_size.is_a?(Integer)
  raise ArgumentError, 'Cubes of size smaller than 2 are not supported.' if cube_size < 2
end

.create_stickers_hash(stickers) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/twisty_puzzles/cube_state.rb', line 37

def self.create_stickers_hash(stickers)
  FACE_SYMBOLS.zip(stickers).to_h do |face_symbol, face_stickers|
    face = Face.for_face_symbol(face_symbol)
    face_hash = {
      stickers: face_stickers,
      # Note that in the ruby code, x and y are exchanged s.t. one can say bla[x][y],
      # but the C code does the more logical thing,
      # so we have to swap coordinates here.
      x_base_face_symbol: face.coordinate_index_base_face(1).face_symbol,
      y_base_face_symbol: face.coordinate_index_base_face(0).face_symbol
    }
    [face_symbol, face_hash]
  end
end

.from_stickers(cube_size, stickers) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twisty_puzzles/cube_state.rb', line 22

def self.from_stickers(cube_size, stickers)
  CubeState.check_cube_size(cube_size)
  unless stickers.length == FACE_SYMBOLS.length
    raise ArgumentError, "Cubes must have #{FACE_SYMBOLS.length} sides."
  end

  unless stickers.all? { |p| p.length == cube_size && p.all? { |q| q.length == cube_size } }
    raise ArgumentError,
          "All sides of a #{cube_size}x#{cube_size} must be #{cube_size}x#{cube_size}."
  end

  stickers_hash = create_stickers_hash(stickers)
  new(Native::CubeState.new(cube_size, stickers_hash))
end

Instance Method Details

#[](coordinate) ⇒ Object



112
113
114
# File 'lib/twisty_puzzles/cube_state.rb', line 112

def [](coordinate)
  @native[coordinate.native]
end

#[]=(coordinate, color) ⇒ Object



116
117
118
# File 'lib/twisty_puzzles/cube_state.rb', line 116

def []=(coordinate, color)
  @native[coordinate.native] = color
end

#colored_to_sObject



108
109
110
# File 'lib/twisty_puzzles/cube_state.rb', line 108

def colored_to_s
  cube_string(self, :color)
end

#dupObject



58
59
60
# File 'lib/twisty_puzzles/cube_state.rb', line 58

def dup
  CubeState.new(@native.dup)
end

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

Returns:

  • (Boolean)


70
71
72
# File 'lib/twisty_puzzles/cube_state.rb', line 70

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

#equal_modulo_rotations?(other) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/twisty_puzzles/cube_state.rb', line 86

def equal_modulo_rotations?(other)
  rotation_algorithms.any? { |r| r.apply_temporarily_to(self) { |state| state == other } }
end

#hashObject



76
77
78
# File 'lib/twisty_puzzles/cube_state.rb', line 76

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

#nObject



64
65
66
# File 'lib/twisty_puzzles/cube_state.rb', line 64

def n
  @native.cube_size
end

#rotation_algorithmsObject



80
81
82
83
84
# File 'lib/twisty_puzzles/cube_state.rb', line 80

def rotation_algorithms
  Rotation::ALL_ROTATIONS.combination(2).reject do |r0, r1|
    r0.inverse == r1
  end.map { |rs| Algorithm.new(rs) } # rubocop:disable Style/MultilineBlockChain
end

#sticker_array(face) ⇒ Object

TODO: Get rid of this backwards compatibility artifact

Raises:

  • (TypeError)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/twisty_puzzles/cube_state.rb', line 91

def sticker_array(face)
  raise TypeError unless face.is_a?(Face)

  @native.sticker_array(
    face.face_symbol,
    # Note that in the ruby code, x and y are exchanged s.t. one can say bla[x][y],
    # but the C code does the more logical thing,
    # so we have to swap coordinates here.
    face.coordinate_index_base_face(1).face_symbol,
    face.coordinate_index_base_face(0).face_symbol
  )
end

#stickersObject



68
# File 'lib/twisty_puzzles/cube_state.rb', line 68

def stickers; end

#to_sObject



104
105
106
# File 'lib/twisty_puzzles/cube_state.rb', line 104

def to_s
  cube_string(self, :nocolor)
end