Class: PuzzleGenerator::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable, DisplayMap
Defined in:
lib/puzzle_generator/map.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMap

#display_map

Constructor Details

#initialize(option = {}) ⇒ Map

Returns a new instance of Map.



21
22
23
24
25
# File 'lib/puzzle_generator/map.rb', line 21

def initialize option = {}
  @option = DefaultOption.merge option
  @data = @option[:data] || (Array.new(@option[:width])).map{ [0]*@option[:height] }
  @chains = @option[:chains] || []
end

Instance Attribute Details

#chainsObject (readonly)

Returns the value of attribute chains.



19
20
21
# File 'lib/puzzle_generator/map.rb', line 19

def chains
  @chains
end

Instance Method Details

#[](x, y = nil) ⇒ Object

NOTICE!! this is not view (reference)



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puzzle_generator/map.rb', line 30

def [] x, y = nil
  if y.nil?
    @data[x]
  else
    if x.kind_of? Range
      @data[x].transpose[y]
    else
      @data[x][y]
    end
  end
end

#[]=(x, y, v) ⇒ Object



41
# File 'lib/puzzle_generator/map.rb', line 41

def []= x, y, v; @data[x][y] = v; end

#break_chains(map, result_map) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puzzle_generator/map.rb', line 55

def break_chains map, result_map
  @temp_map = map
  @temp_result_map = result_map
  result = []
  map.each_column_with_index{ |column, ix|
    column.each_with_index{ |y, iy| result << [ix, iy] if y != 0 }
  } # e.g., [[0, 0], [1, 1]]
  result = transform result,
    :with_belows,
    :with_directs,
    :to_chains,
    :strip_duplicated_chain,
    :strip_out_bounded_chain,
    :strip_overflow_chain,
    :strip_floating_chain

  @temp_result_map = nil
  @temp_map = nil
  result
end

#clone_with_mapObject



44
45
46
47
48
49
# File 'lib/puzzle_generator/map.rb', line 44

def clone_with_map
  Map.new @option.merge(
    :data => @data.transpose.map{ |row| row.map{ |i| yield i } }.transpose,
    :chains => chains
  )
end

#eachObject



51
# File 'lib/puzzle_generator/map.rb', line 51

def each; @data.flatten.each{ |i| yield i }; end

#each_columnObject



43
# File 'lib/puzzle_generator/map.rb', line 43

def each_column; @data.each{ |column| yield column }; end

#each_column_with_indexObject



42
# File 'lib/puzzle_generator/map.rb', line 42

def each_column_with_index; @data.each_with_index{ |column, index| yield column, index }; end

#each_with_index_2dObject



52
53
54
# File 'lib/puzzle_generator/map.rb', line 52

def each_with_index_2d
  @data.transpose.each_with_index{ |row, y| row.each_with_index{ |i, x| yield i, x, y } }
end

#result_mapObject



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

def result_map; @data; end

#to_aObject



50
# File 'lib/puzzle_generator/map.rb', line 50

def to_a; @data.clone; end