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.



15
16
17
18
19
# File 'lib/puzzle_generator/map.rb', line 15

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.



13
14
15
# File 'lib/puzzle_generator/map.rb', line 13

def chains
  @chains
end

Instance Method Details

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

NOTICE!! this is not view (reference)



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puzzle_generator/map.rb', line 24

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



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

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

#break_chains(map, result_map) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puzzle_generator/map.rb', line 49

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



38
39
40
41
42
43
# File 'lib/puzzle_generator/map.rb', line 38

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

#eachObject



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

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

#each_columnObject



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

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

#each_column_with_indexObject



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

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

#each_with_index_2dObject



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

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



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

def result_map; @data; end

#to_aObject



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

def to_a; @data.clone; end