Class: PuzzleGenerator::Chain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/puzzle_generator/chain.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(position = [0, 0], direct = Up, invoke = ) ⇒ Chain

Returns a new instance of Chain.



8
9
10
11
12
13
14
15
16
# File 'lib/puzzle_generator/chain.rb', line 8

def initialize position = [0, 0], direct = Up, invoke = DefaultOption[:invoke]
  @direct = direct
  x, y = position
  @data = case @direct
            when Up   ; ([x]*invoke).zip((y...y+invoke).to_a)
            when Right; (x...x+invoke).to_a.zip([y]*invoke)
            when Left ; (x-invoke+1..x).to_a.zip([y]*invoke)
          end
end

Instance Method Details

#<=>(rhs) ⇒ Object



17
# File 'lib/puzzle_generator/chain.rb', line 17

def <=> rhs; @data <=> rhs.instance_variable_get('@data'); end

#==(rhs) ⇒ Object Also known as: eql?



18
# File 'lib/puzzle_generator/chain.rb', line 18

def == rhs; (self <=> rhs) == 0; end

#[](index) ⇒ Object



22
# File 'lib/puzzle_generator/chain.rb', line 22

def [] index; @data[index]; end

#bound_ok?(max_width, max_height) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/puzzle_generator/chain.rb', line 25

def bound_ok? max_width, max_height
  not @data.find{ |i|
    i.first >= max_width ||
    i.last >= max_height ||
    i.first < 0          ||
    i.last < 0
  }
end

#each(&block) ⇒ Object



21
# File 'lib/puzzle_generator/chain.rb', line 21

def each &block; @data.each &block; self end

#hashObject



20
# File 'lib/puzzle_generator/chain.rb', line 20

def hash; @data.hash; end

#to_aObject



23
# File 'lib/puzzle_generator/chain.rb', line 23

def to_a; @data.clone; end

#up?Boolean

Returns:

  • (Boolean)


24
# File 'lib/puzzle_generator/chain.rb', line 24

def up?; @direct == Up; end