Module: PuzzleGenerator::MapUtils

Included in:
ChainedMap, ColoredMap
Defined in:
lib/puzzle_generator/misc.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#check_answer_correctness(result_map = @result_map) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puzzle_generator/misc.rb', line 38

def check_answer_correctness result_map = @result_map
  map = Map.new @option.merge(:data => result_map.deep_clone)
  drop_blocks map # because of answer is stripped

  @chained = true
  while @chained
    @chained = false
    destory_chains map
    drop_blocks    map
  end
  @chained = nil

  map.all?{ |i| i == 0 }
end

#check_down_chain(map, x, y) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/puzzle_generator/misc.rb', line 112

def check_down_chain map, x, y
  return nil if map[x, y] == 0
  down = y - @option[:invoke] - 1
  return nil if down < 0
  # chain = map[x, down..y]
  # chain if chain.all?{ |i| i == map[x, y] }
  do_check_chain map[x, 0..y].reverse, map[x, y]
end

#check_left_chain(map, x, y) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/puzzle_generator/misc.rb', line 86

def check_left_chain map, x, y
  # this should be rewrited
  return nil if map[x, y] == 0
  left = x - @option[:invoke] + 1
  return nil if left < 0
  # chain = map[left..x, y]
  # chain if chain.all?{ |i| i == map[x, y] }
  # map[0..x, y].inject([]){ |result, value| result << value if value == map[x, y] }
  do_check_chain map[0..x, y].reverse, map[x, y]
end

#check_right_chain(map, x, y) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/puzzle_generator/misc.rb', line 96

def check_right_chain map, x, y
  return nil if map[x, y] == 0
  right = x + @option[:invoke] - 1
  return nil if right >= @option[:width]
  # chain = map[x..right, y]
  # chain if chain.all?{ |i| i == map[x, y] }
  do_check_chain map[x...@option[:width], y], map[x, y]
end

#check_up_chain(map, x, y) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/puzzle_generator/misc.rb', line 104

def check_up_chain map, x, y
  return nil if map[x, y] == 0
  up = y + @option[:invoke] - 1
  return nil if up >= @option[:height]
  # chain = map[x, y..up]
  # chain if chain.all?{ |i| i == map[x, y] }
  do_check_chain map[x, y...@option[:height]], map[x, y]
end

#combine_map(result, map) ⇒ Object



32
33
34
35
36
37
# File 'lib/puzzle_generator/misc.rb', line 32

def combine_map result, map
  result.zip(map.to_a).map{ |columns|
    orig, last = columns
    orig.combine last
  }
end

#destory_chains(map) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puzzle_generator/misc.rb', line 52

def destory_chains map
  map.each_column_with_index{ |column, x|
    column.each_with_index{ |value, y|
      next if value == 0

      left  = check_left_chain  map, x, y
      right = check_right_chain map, x, y
      up    = check_up_chain    map, x, y
      down  = check_down_chain  map, x, y

      # left.fill  0 unless left.nil?
      # right.fill 0 unless right.nil?
      # up.fill    0 unless up.nil?
      # down.fill  0 unless down.nil?
      left.size.times{ |offset|
        map[x-offset, y] = 0
      } unless left.nil?
      right.size.times{ |offset|
        map[x+offset, y] = 0
      } unless right.nil?
      up.size.times{ |offset|
        map[x, y+offset] = 0
      } unless up.nil?
      down.size.times{ |offset|
        map[x, y-offset] = 0
      } unless down.nil?

      @chained ||= left || right || up || down
    }
  }
end

#do_check_chain(target, target_color) ⇒ Object

def check_chain target_color, result, value; result << value if value == target_color; end



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/puzzle_generator/misc.rb', line 121

def do_check_chain target, target_color
  # target.inject([], &method(:check_chain).curry[target_color])
  chain = target.inject([]){ |result, value|
    if value == target_color
      result << value
    else
      break result
    end
  }
  chain.size >= @option[:invoke] ? chain : nil
end

#drop_blocks(map) ⇒ Object



83
84
85
# File 'lib/puzzle_generator/misc.rb', line 83

def drop_blocks map
  map.each_column{ |column| column.map!{ |i| i==0 ? nil : i }.compact!.pad!(@option[:height], 0) }
end

#make_map_arrayObject



26
# File 'lib/puzzle_generator/misc.rb', line 26

def make_map_array; (Array.new(@option[:width])).map{ [0]*@option[:height] }; end

#resolve_map(result_map = @result_map, maps = @maps) ⇒ Object



27
28
29
30
31
# File 'lib/puzzle_generator/misc.rb', line 27

def resolve_map result_map = @result_map, maps = @maps
  result_map.replace maps.inject(make_map_array){ |result, map|
    combine_map result, map
  }
end